I think you're on the good track, but you probably have an issue about CORS and headers authorized in response by backend server.
Check with this code, the headers you received :
this.http.post('http://localhost:8090/login', body, { observe: 'response'}).subscribe(response => {
const keys = response.headers.keys();
const headers = keys.map(key =>
`${key}: ${response.headers.get(key)}`);
console.table(headers);
})
If you don't see any Authorization
header in console output, you should check your backend configuration.
For more details, see my answer on a question about CORS configuration.
You should at least :
- enable CORS with Spring Security
- add a default CORS mapping
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedOrigins("http://domain2.com")
.allowedMethods("PUT", "DELETE")
.allowedHeaders("header1", "header2", "header3")
.exposedHeaders("Authorization")
.allowCredentials(false).maxAge(3600);
}
Here, Authorization
header is exposed in the response.
Note
My suggestion is to restrict exposed headers to a specific endpoint, login
for example, and also to choose another header like 'x-auth-tokenfor example, and send only token (not with
Bearer`).
Because Authorization
with syntax Bearer TOKEN
is used for request from frontend to backend.