Я исследовал это и нашел этот Ответ на SO.
Однако у меня есть дополнительный вопрос к этому:
У меня есть набор фильтров, которые я хочу применить ко ВСЕМ запросам, за исключением особых случаев (например: все пути, кроме / mgmt / ** и /error/**).
Этого нельзя сделать с помощью того же метода, который представлен в связанном ответе, так как я бы добавил фильтры в объект http-security по умолчанию, который затем будет применяться и для особых случаев.
есть что-то вроде "отрицательных совпадений", позволяющее мне сделать что-то вроде:
http.negativeAntMatchers("/mgmt/**).addFilter(...)
добавить фильтр для всего, кроме / mgmt / **?
мой код:
Это конфиг для "/ mgmt", помещающий ManagementBasicAuthFilter в цепочку - это работает, поскольку никакие конечные точки, кроме "/ mgmt / **", не запрашивают базовую аутентификацию.
@Order(1)
@Configuration
@RequiredArgsConstructor
public static class ManagementSecurityConfig extends WebSecurityConfigurerAdapter {
private final AuthenticationManager authenticationManager;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("mgmt/**")
.csrf().disable()
.headers().frameOptions().sameOrigin()
.cacheControl().disable()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.addFilterBefore(new ManagementBasicAuthenticationFilter(authenticationManager,
getAuthenticationEntryPoint(), "/mgmt"), BasicAuthenticationFilter.class)
.authorizeRequests()
.anyRequest()
.permitAll();
}
private BasicAuthenticationEntryPoint getAuthenticationEntryPoint() {
BasicAuthenticationEntryPoint entryPoint = new BasicAuthenticationEntryPoint();
entryPoint.setRealmName("myApp");
return entryPoint;
}
}
Это конфигурация для всех точек входа, КРОМЕ мгмт - все фильтры в этом файле НЕ должны применяться к / мгмт / **
@Order(2)
@Configuration
@RequiredArgsConstructor
@Import({ ResourceServerTokenServicesConfiguration.class })
@EnableOAuth2Client
public static class OAuthSecurityConfig extends WebSecurityConfigurerAdapter {
private final OAuth2ClientContextFilter clientContextFilter;
private final OAuth2ClientAuthenticationProcessingFilter ssoFilter;
private final StatePropagatingLoginRedirectFilter statePropagatingLoginRedirectFilter;
@Override
public void configure(WebSecurity web) {
web.ignoring().antMatchers("/mgmt/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.headers().frameOptions().sameOrigin()
.cacheControl().disable()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.exceptionHandling()
.defaultAuthenticationEntryPointFor(
new LoginUrlAuthenticationEntryPoint("/login"),
request -> true)
.and()
.addFilterAfter(statePropagatingLoginRedirectFilter, AbstractPreAuthenticatedProcessingFilter.class)
.addFilterAfter(ssoFilter, statePropagatingLoginRedirectFilter.getClass())
.addFilterAfter(clientContextFilter, ssoFilter.getClass())
.authorizeRequests()
.anyRequest()
.authenticated();
}
}
Когда я запрашиваю, например: «/ mgmt / health», мне предлагают базовую аутентификацию, но после входа в систему фильтры в (statePropagating, sso, clientContext) все еще применяются - почему это так?