0

I have built a project in spring 5. Then I started to configure the login in angular(CORS) but it just gives me the error 401 in the options preflight. I am stuck with this one, if you have something to help let me know. Backend project: https://github.com/maiconpintoabreu/Spring5SecurityMongodbTest Thanks

Fixed with @Order and Cors configuration

@Order(4)  

public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

...

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
        .antMatchers("/**").hasAnyRole("ADMIN","USER")
        .antMatchers(HttpMethod.OPTIONS, "**").permitAll()
        .antMatchers("/resources/**").permitAll()
        .anyRequest().authenticated().and()
        .httpBasic().and().cors().and();           
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
  UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
  return source;
}

}

Community
  • 1
  • 1
Maicon Santana
  • 340
  • 1
  • 7
  • http://www.baeldung.com/spring-cors – Vikas Jun 01 '18 at 03:46
  • you have add @CrossOrigin(origins="http://localhost:4200") in your calling controller at class level or perticular method level – Rohit Kavthekar Jun 01 '18 at 06:53
  • Possible duplicate of https://stackoverflow.com/questions/42016126/cors-issue-no-access-control-allow-origin-header-is-present-on-the-requested – dur Jun 01 '18 at 06:54
  • Hi I add all the Cors configurations and it didn't work, the @CrossOrigin in the controller (but the problem is when I call /oauth/token, and I try to permit all for Options request as well. – Maicon Santana Jun 01 '18 at 07:55
  • Sorry guys I didn't realize the Order on configuration was so important, it fixed my problem, Thanks. – Maicon Santana Jun 01 '18 at 08:17

0 Answers0