Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在响应头中检索令牌时出现问题-Angular 6-后端Spring_Spring_Angular_Header - Fatal编程技术网

在响应头中检索令牌时出现问题-Angular 6-后端Spring

在响应头中检索令牌时出现问题-Angular 6-后端Spring,spring,angular,header,Spring,Angular,Header,开发环境:角度6.0.9-弹簧靴2.0.7-弹簧5.0.7 我有一个无聊的问题。我的angular应用程序无法看到请求头中的令牌。我的后端是spring,通过邮递员我通常可以得到代币。值得注意的是,在(chrome)浏览器中,状态代码为200 令牌生成器: @Override protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain c

开发环境:角度6.0.9-弹簧靴2.0.7-弹簧5.0.7

我有一个无聊的问题。我的angular应用程序无法看到请求头中的令牌。我的后端是spring,通过邮递员我通常可以得到代币。值得注意的是,在(chrome)浏览器中,状态代码为200

令牌生成器:

@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain,
        Authentication auth) throws IOException, ServletException {

    String login = ((UsuarioSS) auth.getPrincipal()).getUsername();
    String token = jwtUtil.generateToken(login);
    response.addHeader("Access-Control-Expose-Headers", "Authorization");
    response.addHeader("Authorization", "Bearer " + token);

}
服务登录:

export class LoginService {

  constructor(public http: HttpClient) { }

  authenticate(credential: Credential) {
    return this.http.post(`${API_URL}/sge/login`,credential,{observe:'response',responseType:'text'});
  }
}
组成部分:

 onSubmit(){
this.credential = this.loginForm.value;
this.loginService.authenticate(this.credential).subscribe(
  response => {
    console.log(response)
  },
  error => {console.log(error)}
)
控制台铬:

邮递员:


解决方案:我没有意识到定制的头文件是以LazyInit的形式出现的。在此之后,我可以使用令牌访问我的头。

您可以尝试
console.log(response.headers.get('Authorization'))
而不是
console.log(response)
?我也尝试了这个,得到了“null”