我如何获得基本授权 header 并使用 php 为 api 验证它
How can i get Basic authorization header and authenticate it for api by using php
如何获得基本授权 header 并使用 PHP 为 API 验证它。我创建了一个唯一的密钥,它被编码并存储在数据库中。我正在使用 Slim Framework 休息 API。
我可以在 Slim Framework 中使用 $request->headers();
获得 header。它 returns 指定了所有 header,但我如何使用我的数据库令牌检查该密钥。
有什么合适的方法吗?
我在 Slim Framework 3 中是这样做的:
我将中间件添加到所有请求并检查了特定字符串的授权 header,如果授权 header 未设置或与我的字符串不匹配,我只是 return 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;
});
如何获得基本授权 header 并使用 PHP 为 API 验证它。我创建了一个唯一的密钥,它被编码并存储在数据库中。我正在使用 Slim Framework 休息 API。
我可以在 Slim Framework 中使用 $request->headers();
获得 header。它 returns 指定了所有 header,但我如何使用我的数据库令牌检查该密钥。
有什么合适的方法吗?
我在 Slim Framework 3 中是这样做的:
我将中间件添加到所有请求并检查了特定字符串的授权 header,如果授权 header 未设置或与我的字符串不匹配,我只是 return 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;
});