I've been attempting to send a request with an authorization header using Angular to communicate with a Spring backend.
export class TokenInterceptor implements HttpInterceptor{
constructor(public sharedService : SharedService){}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const jwtToken = this.sharedService.getJwtToken();
if(jwtToken){
req = this.addToken(req, jwtToken)
}
return next.handle(req)
}
addToken(req: HttpRequest<any>, jwtToken: any){
return req.clone({
headers: req.headers.append('Authorization', 'Bearer ' + jwtToken)
});
}
}
This is what my interceptor function looks like. Interestingly, when I try to console.log() the authorization header before returning the next().handle() method, I can see the correct token within the request. However, the issue arises when the backend receives a null Authorization header instead.
Within my backend, there is a doFilterInternal() method which filters any incoming requests and extracts the Authentication header. Despite enabling CORS on my backend, the problem persists.
@Override
public void addCorsMappings(CorsRegistry corsRegistry){
corsRegistry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedMethods("*")
.allowedHeaders("*")
.exposedHeaders("Authorization")
.allowCredentials(true)
.maxAge(3600L);
}