Использование Spring boot 2.2.4.RELEASE, spring-security-oauth2-2.3.3
, spring-security-web-5.2.1
.
Я успешно настроил сервер oauth2 и защитил свои конечные точки с помощью WebSecurityConfigurerAdapter
и ResourceServerConfigurer
.
У меня проблема в том, что когда я использую addFilterBefore(customFilter(), AbstractPreAuthenticatedProcessingFilter.class)
в моем ResourceServerConfigurer
. Вызов незащищенных путей все еще пытается аутентифицироваться, вместо того, чтобы игнорироваться, запрос пытается пройти через мой customFilter()
.
Я настроил все свои пользовательские фильтры вручную, а не как бины, чтобы они не будет добавлен автоматически весной к цепочке фильтров, но я все еще получаю это поведение. Я также использовал ("/rest/**", "/api/**")
ant matchers, поэтому customFilter () применяется только при обнаружении этих путей, но я также все еще получаю это поведение.
При запуске сервера я вижу это, которое предназначено:
org.springframework.security.web.DefaultSecurityFilterChain - Creating filter chain: Ant [pattern='/usecured*'], []
org.springframework.security.web.DefaultSecurityFilterChain - Creating filter chain: Ant [pattern='/unsecured2*'], []
org.springframework.security.web.DefaultSecurityFilterChain - Creating filter chain: Ant [pattern='/usecured3*'], []
Мой WebSecurityConfigurerAdapter
@EnableWebSecurity
@Configuration
@Order(1) // order 1 so it applies before ResourceServerConfigurer paths
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
public class ApiSecurityRestLoginConfig extends WebSecurityConfigurerAdapter {
//...
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/usecured*","/unsecured2*","/usecured3*");
}
}
Мой ResourceServerConfigurer
@EnableResourceServer
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
public class ApiSecurityResourceServerConfig implements ResourceServerConfigurer {
//...
@Override
public void configure(HttpSecurity http) throws Exception {
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.authorizeRequests().antMatchers("/rest/**", "/api/**").authenticated()
.and()
//..
.addFilterBefore(customFilter(), AbstractPreAuthenticatedProcessingFilter.class) // <-- when I remove this line, web.ignoring() works, otherwise it doesn't.
//..
}
}
Это ошибка или я подхожу к ней неправильно?