我使用Laravel作为后端和JWT库(https://github.com/tymondesigns/jwt-auth)集成在其中。在后端,在每个请求中,laravel以响应头中附加的新令牌响应,如
Authorization: Bearer <token>
这里我在angular js Framework中实现前端,并且我使用拦截器拦截每个请求,
angular.module('Authentication')
.factory('httpRequestInterceptor',['$cookieStore','$location',
function($cookieStore,$location) {
return {
request: function(config) {
var currenUser = $cookieStore.get("globals");
if(currenUser)
{
var token = currenUser.currentUser.token;
if(token)
{
config.headers['Authorization'] = 'Bearer ' + token;
}
}else{
$location.path('/login');
}
console.log(config);
return config;
} ,
'response': function(response) {
console.log(response.headers()); // no authorization header i get,
//but when i console, in the network i can see the authorization header token
//here i have to access the response header
//but i can't get authorization bearer from header
//if i get the value of token i can replace the currant cookie token value with this response token
console.log(response);
return response;
}
}
}]);
php laravel 5.4 web.php
Route::group(['middleware' => ['jwt.auth', 'jwt.refresh']], function() {
Route::get('profile', [
'as' => 'auth.getAuthenticatedUser',
'uses' => 'AuthController@getAuthenticatedUser'
]);
Route::post('logout', 'Api\AuthController@logout');
});
第一次,令牌从身体部位的 jwt库传递。它存储在cookie中,在令牌通过Header传递的后续请求中。
服务器通过标头响应客户端生成的令牌,之后我必须替换cookie令牌值。
这是我得到的问题,我无法通过拦截器响应访问响应头信息。我怎样才能实现它?任何回应表示赞赏。感谢。