I make an Angular/Java project and I can't get the JWT token that is in my response.
onLoggedin()
{
this.authService.login(this.user).subscribe((data)=> {
let jwToken = data.headers.get('Authorization');
this.authService.saveToken(jwToken);
this.router.navigate(['/']);
},(err)=>{ this.err = 1;
});
this method calls the following method in my service
login(user : User)
{
return this.http.post<User>(this.apiURL+'/login', user , {observe:'response'});
}
When I check the server response in my browser, I found the token in the header. Header response
But my token is null. It seems the "Authorization" attribute doesn't exist in my Angular result.
Or maybe a problem when I fix the CORS policy
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
UserDetailsService userDetailsService;
@Autowired
BCryptPasswordEncoder bCryptPasswordEncoder;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().configurationSource(corsConfigurationSource());
http.csrf().disable();
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.authorizeRequests().antMatchers("/login").permitAll();
http.authorizeRequests().antMatchers("/all").hasAuthority("ADMIN");
http.authorizeRequests().anyRequest().authenticated();
http.addFilter(new JWTAuthenticationFilter(authenticationManager()));
http.addFilterBefore(new JWTAuthorizationFilter(), UsernamePasswordAuthenticationFilter.class);
}
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration.applyPermitDefaultValues());
return source;
}
}