1

I don't know why i can't get value from header AUTHORIZATION as i see in Postman (return from server).

http://img110.xooimage.com/files/1/6/9/postman-567005e.png

I tried many things but i don't know why i still get a null value.

http://img110.xooimage.com/files/b/c/f/debug-5670075.png

Here is my code:

authentification-service.ts

login(u: User): Observable<HttpResponse<Response>> {

if ((u.username && u.password)) {
  this.user.setUsername(u.username);
  this.user.setPassword(u.password);
  // @ts-ignore
  const request = this.http.post<Response>(this.authUrl, this.user, {observe: 'response'}  )
    .pipe(
      tap((data: any) => {
        // @ts-ignore
        this.log(`Succès ${data.status} authentification succès`, 'success');
        this.navigateToCollection();
        console.log('data');
        console.log(data);
        console.log('data.headers.get(Authorization)');
        console.log(data.headers.get('Authorization'));
        return localStorage.setItem(TOKEN_KEY, data.headers.get('Authorization'));
      }),
      catchError(this.handleError<any>('login',
        console.log('je passe ici3')))
    );
  request.subscribe(value => {
     // console.log(value.headers.get('Authorization'));
     console.log(localStorage.getItem(TOKEN_KEY));
  });
  this.presentLoading(request);

  console.log('je passe ici4');
  return request;
} else {
  this.log('Renseignez les champs', 'error');
  alert('Erreur de login ou de mot de passe');
}

}

My token is send by my service authentification (spring boot) if you need more informations ask me.

I would like to know how to get this header value.

thank you for your time.

danronmoon
  • 3,814
  • 5
  • 34
  • 56
  • You can't get Authorization header from data. – fiveelements Aug 05 '19 at 12:25
  • `data` and `headers` are available in two different objects. – fiveelements Aug 05 '19 at 12:27
  • 1
    "Authorization header is not standard in the response, so the server has to explicitly inform to expose it. To do that the server must add the following header: Access-Control-Expose-Headers: authorization" Source: https://stackoverflow.com/questions/44032464/read-authorization-header-from-response – Flo Aug 05 '19 at 12:28
  • 1
    Or may be you have to do this at server side: `response.addHeader("access-control-expose-headers", "Authorization");` – fiveelements Aug 05 '19 at 12:29
  • 1
    Are you making a request to a different domain? If yes then it is a CORS request and you have to add the above-mentioned header in the response. – fiveelements Aug 05 '19 at 12:32
  • By the way, no real importance but `console.log` can take two parameters. First is a string free text, second the value you want to show. For example `console.log('data',data)`. So you can avoid to put 2 `console.log` (or maybe it's itended for visibility pupose) – Flo Aug 05 '19 at 12:32
  • i have a CORS filter in my gateway (Zuul) who already contains those parameters ? I made a mistake? https://ibb.co/zHVSZkc – Thibaut Chazalon Aug 05 '19 at 13:24

2 Answers2

3

You will have to expose the header from your backend(server) or pass it in the response body.

Access-Control-Expose-Headers:headerName; Add the following code in your springBoot application:

response.setHeader("Access-Control-Expose-Headers", "Authorization");

Otherwise you will get it as null, but you will be able to see it in POSTMAN.

Shweta Valunj
  • 148
  • 1
  • 11
0

I Resolve my problem, i'm using gateway but you can put this below your method "configure"

 @Bean
CorsConfigurationSource corsConfigurationSource() {
    CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowedOrigins(Arrays.asList("*"));
    configuration.setAllowedMethods(Arrays.asList("GET","POST", "PUT", "DELETE", "OPTIONS"));
    configuration.setAllowedHeaders(Arrays.asList("authorization",  "content-type"));
    configuration.setExposedHeaders(Arrays.asList("authorization"));
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", configuration);
    return source;
}