我正在为rest api中的auth创建中间件。我的api是使用slim php框架创建的,该框架为构建api提供了很好的特性。这个特性之一是中间件。
我需要检查中间件中的凭据,并用错误(带有json描述的http代码)响应用户。
但不幸的是,slim框架在我试图停止并用http代码响应时会给我一个异常。

<?php
require_once __DIR__.'/../Slim/Middleware.php';
class TokenAuth extends \Slim\Middleware {
    private $auth;
    const SECURED_URI_REGEX = "/^\/v\d\/store\/(orders|users|payment).*/";
    const TOKEN_PARAMETER = "token";
    const USER_EMAIL_PARAMETER = "user_email";
    public static $credentialsArray = array(TokenAuth::TOKEN_PARAMETER,TokenAuth::USER_EMAIL_PARAMETER);
    public function __construct() {
    }
    public function deny_access() {
       print Response::respondWithHttpStatus($app,401,true);
    }
    public function call() {
         $app = $this->app;
        $uri = $app->request->getResourceUri();
        if (preg_match(TokenAuth::SECURED_URI_REGEX, $uri)) {
             $tokenAuth = $app->request->headers->get('Authorization');
             if(isset($tokenAuth)) {
             $parsedCredentials = TokenAuth::parseAndValidateCredentials($tokenAuth);
             if (!$parsedCredentials) {
                 Response::respondWithHttpStatus($app,401,true);
             }
             else {
                $auth = new Authenticator($parsedCredentials[TokenAuth::USER_EMAIL_PARAMETER],$app);
                print $auth->userHasToken();
             }
         }
         else {
             Response::respondWithHttpStatus($app,400,true);
         }
        }
        else {
             $this->next->call();
        }

    }

respondwithhttpstatus方法使用slim框架方法$app->halt($code,$response);
在这种情况下,当我尝试执行这个方法时,我从
Slim Framework
The application could not run because of the following error:

Details

Type: Slim\Exception\Stop
File: /var/www/api/Slim/Slim.php
Line: 1022

如何处理这个问题。
我的目标是控制中间件中的用户凭据,如果出现错误,则使用适当的http代码和描述错误原因的json消息进行响应。
也许换个方式更好。
请建议。
一种可能的解决方法
   $app->response->setStatus(400);
   $app->response->headers->set('Content-Type', 'application/json');
   print Response::respondWithHttpStatus($app,400,false);

和响应函数
公共静态函数basicrespond($app,$code,$message,$halt){
    if(!isset($message) || empty($message)) {
        $message = Response::$RESPONSE_MAP[$code];
    }
    $response = json_encode($message);
    if($halt===true) {
        $app->halt($code, $response);
    }
    else {
        return $response;
    }
}

为了满足我的需求,抛出异常也是另一种解决方案,但在我的情况下,我不需要继续,只需设置头、代码,就不调用next-works了。

最佳答案

不能在中间件中使用halt
https://stackoverflow.com/a/10201595/2970321
只能在路由回调的上下文中调用halt。
相反,您可以使用php的400header手动生成exit响应:

header("HTTP/1.1 400 Access denied");
exit;

或者,
您可以定义一种新的异常类型:
class AuthException extends Exception {
    public function __construct() {
        $message = 'You must authenticate to access this resource.';
        $code = 400;
    }
}

在您的error路线中捕获此信息:
$app->error(function (\Exception $e) use ($app) {
    // Example of handling Auth Exceptions
    if ($e instanceof AuthException) {
        $app->response->setStatus($e->getCode());
        $app->response->setBody($e->getMessage());
    }
});

拒绝授权时抛出AuthException
throw new AuthException();

这基本上就是在Slim-Auth中实现的方法。

07-28 01:58
查看更多