im developping a back-end with the Spring framework. My frontend is written using the Angular framework. My GET requests are being blocked by the CORS policy.
When i make a request to a GET resource with the following angular function:
authenticate(credentials, callback) {
const headers = new HttpHeaders(credentials ? {
authorization : 'Basic ' + btoa(credentials.username + ':' + credentials.password)
} : {});
this.http.get("http://localhost:8080/user", {headers: headers}).subscribe(response => {
console.log(response);
console.log(this.authenticated);
if (response['name']) {
this.authenticated = true;
} else {
this.authenticated = false;
}
return callback && callback();
});
}
I get a failed request in my Network tab. Depending on my security config, i also sometimes get a 401 but in both cases i'll get the "blocked by cors" message in the console.
This is the Controller the request is being sent to:
@CrossOrigin(origins = "*", allowedHeaders = "*")
@RestController
public class AuthorizationController {
@CrossOrigin(origins = "*", allowedHeaders = "*")
@RequestMapping("/user")
public Principal user(Principal user) {
System.out.println(user);
return user;
}
}
Here is my Spring security configuration:
@Override
protected void configure(final HttpSecurity http) throws Exception {
http.httpBasic()
.and().authorizeRequests().antMatchers("/**").permitAll()
.anyRequest().authenticated();
}
Note: this would not be my preferred configuration. I'm using this because it's the only way i get something to work. With this configuration, my POST requests are working. However, the GET request i describe earlier on does not work with this configuration.
Any tips or help would be appreciated, let me know if you need any further information about my code.