Spring Security将筛选器添加到除一个端点之外的所有端点
我研究了一下,发现这是真的 不过,我有一个补充问题: 我有一组过滤器,我希望应用于所有请求,特殊情况除外(例如:除/mgmt/**和/error/**之外的所有路径) 这不能使用链接答案中提供的相同方法来完成,因为我会将过滤器添加到默认http安全对象中,然后该对象也会应用于特殊情况 是否有“消极匹配者”这样的东西,允许我做如下事情:Spring Security将筛选器添加到除一个端点之外的所有端点,spring,spring-boot,spring-security,Spring,Spring Boot,Spring Security,我研究了一下,发现这是真的 不过,我有一个补充问题: 我有一组过滤器,我希望应用于所有请求,特殊情况除外(例如:除/mgmt/**和/error/**之外的所有路径) 这不能使用链接答案中提供的相同方法来完成,因为我会将过滤器添加到默认http安全对象中,然后该对象也会应用于特殊情况 是否有“消极匹配者”这样的东西,允许我做如下事情: http.negativeAntMatchers("/mgmt/**).addFilter(...) 是否为除/mgmt/**之外的所有内容添加筛选器 我的代码
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;
}
}
这是除管理之外的所有入口点的配置-此文件中的所有筛选器不应应用于/mgmt/**
@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”时,系统会提示我输入基本身份验证,但在登录后,(StatePropaging、sso、clientContext)中的过滤器仍然会被应用-这是为什么?您需要允许“/mgmt/**”中的所有请求,并对其他请求进行身份验证,尝试类似的操作
http
.authorizeRequests()
.antMatchers("**").hasAnyAuthority("ANY_AUTHORITY")
.antMatchers("/mgmt/**").permitAll()
.anyRequest().authenticated()
如果我没有弄错您的问题,您不希望将筛选器应用于
/mgmt
,那么您可以使用
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) {
// Overridden to exclude some url's
web.ignoring().antMatchers("/mgmt");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// configure the other url's as required
}
}
您可以使用web.ignering将spring安全性配置为忽略某些url模式 就你而言
web.ignoring().antMatchers("/mgmt/**");
多亏了kimhom和他的团队,再加上Nicholas和e.g.78,我才明白了这一点-web。忽略
不起作用,原因我想稍后再研究-我忘记的是,spring会自动将所有作为bean存在的过滤器添加到所有过滤器链中。为了防止这种情况发生,我们可以
@Bean
public FilterRegistrationBean disableMyFilterBean(MyFilterBean filter) {
FilterRegistrationBean registration = new FilterRegistrationBean(filter);
registration.setEnabled(false);
return registration;
}
然后我的过滤器只在我想要的地方应用-我甚至不需要提供
网络安全性。忽略匹配器谢谢,我会试试这个-我只使用了HttpSecurity配置,直到现在我还没有意识到它什么时候起作用,所以我接受了答案,而事实上问题没有解决,我将为问题添加详细信息您现在无法更改问题,您需要问另一个问题。这是完全相同的问题,我只是添加了代码,因为您的回答没有解决问题,我想,最有可能需要更多信息。我弄明白了,通过搜索你的答案不起作用的情况——因此我想在应该得到的地方给予赞扬,而不是仅仅接受我自己的答案,就好像我在自己的网站上发现它是WebSecurity配置项一样。无论如何,尼古拉斯·K写了一个更好的答案。