[英]How can i get Basic authorization header and authenticate it for api by using php
How can i get Basic authorization header and authenticate it for API by using PHP. 我如何获取基本授权标头并通过使用PHP对API进行身份验证。 I had created a unique key which is encoded and stored in database.
我创建了一个唯一的密钥,该密钥已编码并存储在数据库中。 i am using Slim Framework for rest API.
我正在使用Slim Framework for Rest API。
I can get the header by using $request->headers();
我可以使用
$request->headers();
获取标题$request->headers();
in Slim Framework. 在Slim Framework中。 It returns all the headers specified, but how do i check that key with my database token.
它返回指定的所有标头,但是如何使用数据库令牌检查该键。
Is there any proper way to do this? 有什么适当的方法吗?
I did it like this in Slim Framework 3: 我在Slim Framework 3中做到了这一点:
I added the Middleware to all of the requests and checked the Authorization header for certain string and if the Authorization header is not set or doesnt match my string i just return 400 HTTP code. 我将中间件添加到所有请求中,并检查了某些字符串的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.