I'm building a platform using Spring with authorization via JWT.
In gateway service I have custom filter (based on BasicAuthenticationFilter) for parsing token but it should be invoked only for routes marked in security config as .authenticated()
so routes which can be access by anyone are added to WebSecurity.ignoring()
.
The problem is that adding path to WebSecurity.ignoring()
also disable CORS config for it and OPTIONS
request is not returning Access-Control-*
headers.
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
// Auth service
.antMatchers("/auth/login", "/auth/renew")
.permitAll()
.antMatchers("/auth/logout", "/auth/session/**")
.authenticated()
// User service
.antMatchers(HttpMethod.POST, "/user/")
.permitAll()
.antMatchers( "/user/confirm/**", "/user/recover", "/user/validate/**")
.permitAll()
.antMatchers("/user/", "/user/change/**")
.authenticated()
// further part of routes...
http
.formLogin()
.disable()
.logout()
.disable();
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.addFilter(new JwtAuthenticationFilter(authenticationManager(), jwtKey));
http.csrf().disable();
http.cors();
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring()
// Auth service
.antMatchers("/auth/login", "/auth/renew")
// User service
.antMatchers(HttpMethod.POST, "/user/")
.antMatchers( "/user/confirm/**", "/user/recover", "/user/validate/**")
// further part of routes...
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(corsOrigins);
configuration.setAllowedMethods(Collections.singletonList("*"));
configuration.setAllowedHeaders(Collections.singletonList("*"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
Is there any way to bypass only specific filter for some paths?
-SOLVED-
I've solved my problem by enabling WebMvc
and adding cors configuration (previous may be deleted).
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:3000")
.allowedMethods("*")
.allowedHeaders("*")
.allowCredentials(true)
.maxAge(3600);
}