I'm trying to pass a GET-request from my Angular front-end to my Spring boot back-end. I'm using JWT to authenticate everything and I'm using an HTTP-interceptor to add the token in the request header like this:
@Injectable()
export class Interceptor implements HttpInterceptor {
constructor(public auth: AuthService) { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const token: string = this.auth.getToken();
if(this.auth.isAuthenticated()) {
request = request.clone({
setHeaders : { Authorization : `${this.auth.getToken()}`,}
});
}
return next.handle(request);
}
}
I don't see the token being added in chrome's request headers: image link
And the back-end doesn't see it either, thus returning a 403. I suspect that this has something to do with CORS (I'm hosting both front-end and back-end on the same machine, and I'm using Chrome + CORS plugin).
So I tried allowing CORS requests to my back-end:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.cors().disable()
.authorizeRequests()
.antMatchers("/login").permitAll()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.anyRequest().authenticated().and().addFilterBefore(new JwTokenFilter(jwTokenProvider), UsernamePasswordAuthenticationFilter.class);
}
This still doesn't seem to work. Also I've tried using Postman to do the same GET-request and that seems to work just fine. Can anyone help me out here?