如何从POST请求回复获取响应头

时间:2019-05-05 15:42:38

标签: angular api http header python-requests

如上所述。 以下代码无效。

    public login() {
  const username = "kamczi";
  const password = "password";
    return this.http.post<any>(environment.api_endpoint+'/login', { username, password} , {observe: 'response'}).subscribe(resp=>{
        this.handleAuthentication(resp.headers.get('Authorization'))
    })
  }
  public handleAuthentication(token: string): void {
    localStorage.setItem('access_token', token);
    console.log(token);
  }

Console.log显示空

我需要获取位于响应头中的授权头。

enter image description here

解决

我需要在Spring Boot安全配置中添加以下代码。

CorsConfiguration configuration = new CorsConfiguration();
configuration.applyPermitDefaultValues();
configuration.addExposedHeader("Authorization"); // this line helped

1 个答案:

答案 0 :(得分:1)

您的Angular代码工作正常。某些标头仅应被浏览器截获,并且用户无法在代码内访问它们。为此,您必须在响应中明确显示它们。

因此,当您从API发送响应时,您需要在响应中添加以下标头,而Authorization标头将可用。

'access-control-expose-headers' : 'Authorization'
相关问题