Я строю платформу с использованием Spring с авторизацией через JWT.
В службе шлюза у меня есть пользовательский фильтр (основанный на BasicAuthenticationFilter) для разбора токена, но его следует вызывать только для маршрутов, помеченных в конфигурации безопасности как .authenticated()
, поэтому маршруты, к которым может обращаться любой, добавляются в WebSecurity.ignoring()
.
Проблема в том, что добавление пути к WebSecurity.ignoring()
также отключает для него конфигурацию CORS, и запрос OPTIONS
не возвращает Access-Control-*
заголовки.
@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;
}
Есть ли способ обойти только определенный фильтр для некоторых путей?
-SOLVED-
Я решил свою проблему, включив WebMvc
и добавив конфигурацию cors (предыдущая может быть удалена).
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:3000")
.allowedMethods("*")
.allowedHeaders("*")
.allowCredentials(true)
.maxAge(3600);
}