【问题标题】:SLIM Framework Route Authentication v2 vs v3SLIM 框架路由身份验证 v2 与 v3
【发布时间】:2016-09-05 21:40:55
【问题描述】:

我有一个使用 Slim v2 构建的 API,并且我通过中间件函数“authenticate”保护某些路由:

    /**
     * List marca novos
     * method GET
     * url /novos/marca/:idmarca
     */
    $app->get('/novos/marca/:idmarca', 'authenticate', function($idmarca) {
        $response = array();
        $db = new DbHandler('dbnovos');


        // fetching marca
        $marca = $db->getMarcaNovos($idmarca);

        $response["error"] = false;
        $response["marca"] = array();

        array_walk_recursive($marca, function(&$val) {
            $val = utf8_encode((string)$val);
        });

        array_push($response["marca"], $marca);

        echoRespnse(200, $response, "marcaoutput");
    })->via('GET', 'POST');

authenticate 函数检查是否发送了标头授权值(user_api_key)并针对数据库进行检查。

我正在尝试通过以下路径在 Slim v3 API 中获得相同的功能:

    /**
     * List marca novos
     * method GET
     * url /novos/marca/:idmarca
     */
    $app->get('/novos/marca/{idmarca}', function ($request, $response, $args) {

    $output = array();
    $db = new DbHandler('mysql-localhost');
    $marca = $db->getMarcaNovos($args['idmarca']);

    if ($marca != NULL) {
        $i = 0;
        foreach($marca as $m) {
            $output[$i]["id"] = $m['id'];
            $output[$i]["nome"] = utf8_encode($m['nome']);
            $i++;
        }

    } else {
        // unknown error occurred
        $output['error'] = true;
        $output['message'] = "An error occurred. Please try again";
    }

    // Render marca view
    echoRespnse(200, $response, $output, "marca");
})->add($auth);

这是我的中间件

/**
 * Adding Middle Layer to authenticate every request
 * Checking if the request has valid api key in the 'Authorization' header
 */
$auth = function ($request, $response, $next) {

$headers = $request->getHeaders();
$outcome = array();

// Verifying Authorization Header
if (isset($headers['Authorization'])) {
    $db = new DbHandler('mysql-localhost');

    // get the api key
    $api_key = $headers['Authorization'];
    // validating api key
    if (!$db->isValidApiKey($api_key)) {
        // api key is not present in users table
        $outcome["error"] = true;
        $outcome["message"] = "Access Denied. Invalid Api key";
        echoRespnse(401, $outcome, $output);
    } else {
        global $user_id;
        // get user primary key id
        $user_id = $db->getUserId($api_key);
        $response = $next($request, $response);
        return $response;
    }
} else {
    // api key is missing in header
    $outcome["error"] = true;
    $outcome["message"] = "Api key is missing";
    //echoRespnse(400, $response, $outcome);
    return $response->withStatus(401)->write("Not allowed here - ".$outcome["message"]);
}

};

但我总是收到错误消息:“此处不允许 - Api 密钥丢失” 基本上,是否设置了 $headers['Authorization'] 的测试失败。 $headers 数组结构是什么,或者如何获取通过标头传递的授权值?

【问题讨论】:

    标签: php authentication slim


    【解决方案1】:

    如果您发送的不是有效的 HTTP 基本授权标头,PHP 将无法访问它。您可以通过将以下重写规则添加到您的 .htaccess 文件来解决此问题。

    RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    

    【讨论】:

    • 我正在使用 postman chrome 应用程序测试端点调用。我只发送带有令牌作为值的授权密钥。在 v2 中以相同的方式工作,尝试添加您建议的规则和输出是相同的。
    猜你喜欢
    • 2017-05-18
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多