繁体   English   中英

我如何获取基本授权标头并通过使用php对api进行身份验证

[英]How can i get Basic authorization header and authenticate it for api by using php

我如何获取基本授权标头并通过使用PHP对API进行身份验证。 我创建了一个唯一的密钥,该密钥已编码并存储在数据库中。 我正在使用Slim Framework for Rest API。

我可以使用$request->headers();获取标题$request->headers(); 在Slim Framework中。 它返回指定的所有标头,但是如何使用数据库令牌检查该键。

有什么适当的方法吗?

我在Slim Framework 3中做到了这一点:

我将中间件添加到所有请求中,并检查了某些字符串的Authorization标头,如果未设置Authorization标头或与我的字符串不匹配,我只返回400 HTTP代码。

$app->add(function($request,$response,$next){
$authorization_header = $request->getHeader("Authorization");

if(empty($authorization_header) || ($authorization_header[0]!="test")){ //you can check the header for a certain string or you can check if what is in the Authorization header exists in your database

    return $response->withStatus(400);
}

$response = $next($request,$response);

return $response;

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM