【问题标题】:Problems retrieving token in response header - Angular 6 - backend Spring在响应标头中检索令牌时出现问题 - Angular 6 - 后端 Spring
【发布时间】:2018-12-20 17:38:01
【问题描述】:

开发。环境:Angular 6.0.9 - Spring Boot 2.0.7 - Spring 5.0.7

我有一个无聊的问题。我的 Angular 应用程序看不到请求标头中的令牌。我的后端是春天,邮递员我通常会得到令牌。值得注意的是,在(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)}
)

主机版:

控制台邮递员:

【问题讨论】:

  • 你能用console.log(response.headers.get('Authorization'))代替console.log(response)吗?
  • 我也试过这个并得到'null'

标签: spring angular header


【解决方案1】:

解决方案:我没有意识到自定义标头是以 LazyInit 的形式出现的。之后,我就可以使用令牌访问我的标头了。

【讨论】:

    猜你喜欢
    • 2020-09-27
    • 2017-09-28
    • 2020-11-30
    • 2019-12-21
    • 2021-03-31
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多