Far*_*han 5 http-headers laravel laravel-5 laravel-5.4
我有一个 Web 应用程序,我根据标头中发送的自定义令牌进行身份验证API_TOKEN。在我在源代码(laravel)中进行了所有代码挖掘之后,我不确定发生了什么
这是我的中间件
protected $AUTH_HEADER = 'API_TOKEN';
protected $_RESPONSE = array('status' => false, 'message' => '', 'data' => array());
public function handle($request, Closure $next, $guard = null)
{
$response = $this->_RESPONSE;
if($request->hasHeader($this->AUTH_HEADER)){
$api_token = $request->header($this->AUTH_HEADER);
try{
$request->user = \App\User::where(['api_token' => $api_token])->firstOrFail();
Auth::login($request->user);
$response = $next($request);
}catch(\Exception $exception){
$response['status'] = false;
$response['message'] = 'Invalid Token.';
}
}else{
$response['status'] = false;
$response['message'] = 'Unauthorized Request.';
}
// Lines ONLY I used for cross verification of availability of my header
// $response['data'] = getallheaders();
// $response['data'] = $_SERVER;
return $response;
}
Run Code Online (Sandbox Code Playgroud)
这是我的 POST 请求的屏幕截图,api.easyinventory.com是映射到我的应用程序的自定义虚拟主机
我的路线放置如下,默认情况下将放置在前缀api.php下的路线组下方api
Route::group(['prefix' => 'product'], function(){
Route::get('read', 'API\ProductController@read');
}
Run Code Online (Sandbox Code Playgroud)
说到问题,如果我打电话getallheaders(),我可以看到我的自定义标头,如下所示
但在 中$request,我无法得到它。对于这个问题上的任何线索,我将不胜感激。
我的努力包括跟踪这些标头实际SET在对象中的位置$request,我检查ServerBag.php了 Symfony 源代码
Symfony ServerBag 类方法 - getHeaders。
如果你看看这个函数getHeaders。它仅在数组中添加选择性标头headers,以Content作为起始字符串或以HTTP_. 我尝试传递自己的标头,HTTP_API_TOKEN但成功了:-(
你可以尝试使用全局助手吗request()
request()->header('API_TOKEN'); //<-- manually passing the string first, for test purposes
Run Code Online (Sandbox Code Playgroud)
编辑------------ 正如下面评论中提到的OP:
我们应该以驼峰格式访问我们的标头,因此将其发送为API-TOKEN并访问为request()->header('Api-Token');
| 归档时间: |
|
| 查看次数: |
6276 次 |
| 最近记录: |