I am trying to secure my Web Application via JWT token, but when I try to make a request from my Angular app (localhost:4200) to my Spring Boot app (localhost: 8080) I get the following error:
From the message alone I can see that it is a CORS
issue, the problem is that I've already enabled requests from different origin at my back-end, and here is the code for it:
UPDATE: I've added OPTIONS into allowedMethods()
, but the error remains the same.
@Configuration
public class AppConfiguration {
@Autowired
private Environment env;
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:4200")
.allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD","OPTIONS")
.allowedHeaders("Content-Type", "Date", "Total-Count", "loginInfo")
.exposedHeaders("Content-Type", "Date", "Total-Count", "loginInfo")
.maxAge(3600);
}
};
}
Here is the code from my Angular app as well :
baseUrl = 'http://localhost:8080/api/';
constructor(private http: Http) { }
private postaviHeadere() : RequestOptions{
let headers = new Headers();
console.log("***************** Set Headers *****************");
console.log('Getting token from local storage:');
console.log(localStorage.getItem('jwt_token'))
console.log("***********************************************");
headers.append('JWT_TOKEN', localStorage.getItem('JWT_TOKEN'));
let options = new RequestOptions({headers : headers});
console.log(options);
return options;
}
getUserByToken(): any {
return this.http.get(this.baseUrl + 'user/secured', this.postaviHeadere())
}