我的项目使用Laravel + JWT + dingo进行开发,JWT希望实现一个中间件刷新令牌,在测试过程中可以在线找到这样的中间件
public function handle($request, Closure $next){
$this->checkForToken($request);
try {
if ($this->auth->parseToken()->authenticate()) {
return $next($request);
}
throw new UnauthorizedHttpException('jwt-auth', 'sorry');
} catch (TokenExpiredException $exception) {
try {
$token = $this->auth->refresh();
Auth::guard('api')->onceUsingId($this->auth->manager()->getPayloadFactory()->buildClaimsCollection()->toPlainArray()['sub']);
} catch (JWTException $exception) {
throw new UnauthorizedHttpException('jwt-auth', $exception->getMessage());
}
}
return $this->setAuthenticationHeader($next($request), $token);
}
我在邮递员上测试了我的API,但无法获得标头授权 ,仅
Server →nginx/1.10.3 (Ubuntu)
Content-Type →application/json
Transfer-Encoding →chunked
Connection →keep-alive
WWW-Authenticate →jwt-auth
Cache-Control →no-cache, private
Date →Fri, 02 Nov 2018 09:55:42 GMT
函数setAuthenticationHeader是:
$response->headers->set('Authorization', 'Bearer '.$token);
为什么不起作用? 最后我用:
return $next($request)->withHeaders([
'Authorization'=> 'Bearer '.$token,
]);
我得到了想要的东西。