Witam,
od pewnego czasu bawię się springiem. Tworząc api trafiłem na dziwny błąd związany z CORS a mianowicie:
Posiadam taką metodę konfiguracji web security:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.headers().frameOptions().disable();
http.cors().and().csrf().disable().authorizeRequests()
.antMatchers(HttpMethod.POST, SecurityConstants.SIGN_UP_URL).permitAll()
.antMatchers(HttpMethod.GET, "/api/users/").hasRole("ADMIN")
.antMatchers(HttpMethod.GET, "/api/notes/").hasRole("USER")
.antMatchers(HttpMethod.POST, "/api/notes/").hasRole("USER")
.antMatchers(HttpMethod.PUT, "/api/notes/").hasRole("USER")
.antMatchers(HttpMethod.POST, "/login").permitAll()
.antMatchers(HttpMethod.GET, "/h2-console/**").permitAll()
.anyRequest().authenticated()
.and()
.addFilter(new JWTAuthenticationFilter(authenticationManager(), jwtFactory, customAuthenticationFailureHandler()))
.addFilter(new JWTAuthorizationFilter(authenticationManager(), domainUserRepository))
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
;
}
Dodatkowo taką klasę konfigurującą Corsy
@Configuration
@EnableWebMvc
public class CorsConfiguration implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedMethods("GET", "POST", "DELETE", "OPTIONS", "PUT");
}
}
Problem polega na tym, że jeśli wywołuje żądanie metodą GET lub POST wszystko jest okey, ale gdy chce wysłać żądanie DELETE lub PUT odbijam się od już wspomnianych 'corsów':
OPTIONS http://localhost:8080/api/notes 403
Failed to load http://localhost:8080/api/notes: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.
Siedzę nad tym już kilka godzin więc proszę o pomoc.