I'm trying to implement login functionality using angular and spring boot.
Im following spring tutorial https://spring.io/guides/tutorials/spring-security-and-angular-js/ But in my case my angular project is hosted on localhost:4200 and spring on localhost:8080
Now im sending a '/user' request to spring server. My angular code look like:
const headers = new HttpHeaders(credentials ? {
authorization: 'Basic ' + btoa(credentials.username + ':' + credentials.password)
} : {});
this.http.get('http://localhost:8080'+'/user', { headers: headers }).subscribe(response => {
if (response['name']) {
this.authenticated = true;
} else {
this.authenticated = false;
}
return callback && callback();
});
Now because of CORS it sends OPTIONS request which is successful with status 200. After this it is not sending actual GET request which should send credentials as Angular $http is sending OPTIONS instead of PUT/POST
My spring code looks like this:
@CrossOrigin(origins = "*", maxAge = 3600, allowedHeaders={"x-auth-token", "x-requested-with", "x-xsrf-token"})
@RequestMapping("/user")
public Principal user(Principal user) {
return user;
}
@Configuration
@Order(SecurityProperties.DEFAULT_FILTER_ORDER)
protected static class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic().and().cors().and().authorizeRequests().antMatchers("/index.html", "/", "/home", "/login").permitAll()
.anyRequest().authenticated().and().csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
}
}
Also i have added Spring Security in pom file.
Can you please help to find out why GET request is not triggered after OPTIONS request.