简体   繁体   English

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

[英]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.

相关问题 PHP cURL格式错误的授权标头REST API基本授权 - PHP cURL Malformed Authorization header REST API Basic Authorization API OneSignal With CURL 我收到此错误(请包括区分大小写的 header 授权:基本或 Bearer token="" 以及有效的 REST API 密钥 - API OneSignal With CURL i get this Error (Please include a case-sensitive header of Authorization: Basic or Bearer token="" with a valid REST API key 如何使用 guzzle 为基本身份验证发送正确的授权标头 - How to send a correct authorization header for basic authentication using guzzle PHP curl 使用基本授权将表单数据发布到 REST API - PHP curl to POST form data to REST API using basic authorization Cake PHP3 Rest API无法获得授权标头值 - Cake PHP3 Rest API can't get Authorization header value 如何使用PHP设置授权标头 - How to set Authorization header using PHP 本机PHP中具有Authorization Basic的Soap Header - Soap Header with Authorization Basic in native PHP 使用 php curl 获取 Header 登录 api 的授权值(在 json 正文中发送用户名和密码) - Get Header Authorization value of a login api (sent with username and password in json body) using php curl 我如何使我的PHP SoapClient通过摘要进行身份验证 - How can i get my PHP SoapClient to Authenticate with Digest 即使使用“代理授权:基本”,PHP也无法通过代理进行身份验证 - PHP cannot authenticate with proxy even with “Proxy-Authorization: Basic”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM