即使X-AUTH-TOKEN存在于头文件中,它也会返回null

时间:2015-11-19 13:45:40

标签: angularjs spring rest spring-boot

我使用angular JS登录我的应用程序。在服务器上,它返回一个X-AUTH-TOKEN。当我做一个JSON请求。标题响应看起来像这样......

Pragma: no-cache
Date: Thu, 19 Nov 2015 12:44:05 GMT
X-Content-Type-Options: nosniff
Server: Apache-Coyote/1.1
X-Frame-Options: DENY
Access-Control-Max-Age: 3600
Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE
Access-Control-Allow-Origin: chrome-extension://gmodihnfibbjdecbanmpmbmeffnmloel
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
X-AUTH-TOKEN: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJiaWxseWpvZSJ9.le0y_pOfQAyLJO4IJ4ZjQQqgqgiN2xtpLAzORawoDm4O4euHFh32LtjMrBUTkr8G2LeY_2bALe_rcm3LY_NlSg
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type, Accept, X-Requested-With, remember-me
Content-Length: 88
X-XSS-Protection: 1; mode=block
Expires: 0

如你所见。 X-AUTH-TOKEN在场。但是,当我尝试检索它并将其保存到本地存储...

  $http.post(loginUrl)
  .success(function(data, status,headers) {
    $scope.hello = data;
    var TOKEN = headers("X-AUTH-TOKEN");
    window.localStorage['X-AUTH-TOKEN-TRIVIA'] = headers("X-AUTH-TOKEN");
  })

它总是返回NULL。如果令牌明显在标题中,为什么不检索令牌?

我也使用过单引号。

  $http.post(loginUrl)
  .success(function(data, status,headers) {
    $scope.hello = data;
    var TOKEN = headers('X-AUTH-TOKEN');
    window.localStorage['X-AUTH-TOKEN-TRIVIA'] = headers('X-AUTH-TOKEN');
  })

1 个答案:

答案 0 :(得分:0)

想出来......感谢另一篇文章

Angular.js saying custom HTTP response header is null

问题在于服务器端。您必须添加Access-Control-Expose-Headers: X-Auth-Token

在我使用Spring Boot或Spring的情况下。您可能需要在CorsFilter中添加它

response.setHeader("Access-Control-Expose-Headers", "X-Auth-Token");