防止 Spring Boot 注册 Spring Security 过滤器之一

Posted

技术标签:

【中文标题】防止 Spring Boot 注册 Spring Security 过滤器之一【英文标题】:Prevent Spring Boot from registering one of Spring Security filter 【发布时间】:2015-10-19 06:36:09 【问题描述】:

我想禁用安全链中的 Spring Security 过滤器之一。

我已经看到Prevent Spring Boot from registering a servlet filter 的问题 - 并且接受应该可以工作,但不幸的是不是。

带代码:

    @Bean
    public FilterRegistrationBean registration(AnonymousAuthenticationFilter filter) 
        FilterRegistrationBean registration = new FilterRegistrationBean(filter);
        registration.setEnabled(false);
        return registration;
    

Spring Boot 会及时宣布没有符合条件的 bean,这很可悲:

原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有找到类型为 [org.springframework.security.web.authentication.AnonymousAuthenticationFilter] 的合格 bean 依赖项:预计至少有 1 个 bean 有资格作为自动装配候选者依赖。依赖注释:

创建另一个 bean 后:

    @SuppressWarnings("deprecation") // Oh, there be dragons
    @Bean
    public AnonymousAuthenticationFilter anonymousAuthenticationFilter() 
        return new AnonymousAuthenticationFilter();
    

我被攻击了

原因:java.lang.IllegalArgumentException: [Assertion failed] - 此 String 参数必须有长度;它不能为 null 或为空

这是完全不稳定的; Asserts 中的 afterPropertiesSet() 方法 https://github.com/spring-projects/spring-security/blob/master/web/src/main/java/org/springframework/security/web/authentication/AnonymousAuthenticationFilter.java 阻止我使用默认构造函数。使用另一种方法:

    @Bean
    public AnonymousAuthenticationFilter anonymousAuthenticationFilter() 
        // it will be disabled anyway so...
        return new AnonymousAuthenticationFilter("_", new Object(), new ArrayList<GrantedAuthority>());
    

一切都变得更好了:

INFO 4916 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : 过滤器 anonymousAuthenticationFilter 未注册(禁用)

调试 4916 --- [ost-startStop-1] o.s.security.web.FilterChainProxy:初始化过滤器“springSecurityFilterChain”

DEBUG 4916 --- [ost-startStop-1] o.s.security.web.FilterChainProxy : Filter 'springSecurityFilterChain' 配置成功

但是在访问了一些资源后,我得到了:

DEBUG 4916 --- [nio-8080-exec-3] o.s.security.web.FilterChainProxy : /user 位于附加过滤器链中 13 的第 10 位;触发过滤器:'AnonymousAuthenticationFilter'

DEBUG 4916 --- [nio-8080-exec-3] o.s.s.w.a.AnonymousAuthenticationFilter:使用匿名令牌填充 SecurityContextHolder:'org.springframework.security.authentication.AnonymousAuthenticationToken@90572420:主体:anonymousUser;凭证:[受保护];已认证:真实;详细信息:org.springframework.security.web.authentication.WebAuthenticationDetails@255f8:RemoteIpAddress:127.0.0.1;会话ID:6B9D974A4634548750FE78C18F62A6B0;授予权限:ROLE_ANONYMOUS'

由于某种原因,AnonymousAuthenticationFilter 仍在工作。 问题:有没有办法在 Spring Boot 应用程序中禁用此类过滤器?

【问题讨论】:

【参考方案1】:

Spring Security 将所有过滤器捆绑在 HttpSecurity 配置中。要禁用匿名身份验证,请使用以下命令:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter 


    @Override
    protected void configure(HttpSecurity http) throws Exception 
        http
            .anonymous().disable()
            ...
    
    ...

如果您想禁用 Spring Security 中的所有默认值,您可以将 true 传递给父类构造函数以禁用默认值。例如:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter 

    public SecurityConfig() 
        super(true);
    
    ...

【讨论】:

谢谢。当解决方案总是如此接近时,我不敢相信我花了这么多时间搜索互联网、文档和源代码。你是我的英雄。 不得不说,说到春天,总是这样。做同一件事的方法太多了,你会感到困惑。 完全同意上述评论,我也有 keycloak 并四处寻找这个“anonymousUser”的来源......

以上是关于防止 Spring Boot 注册 Spring Security 过滤器之一的主要内容,如果未能解决你的问题,请参考以下文章

如何防止spring-web的spring-boot自动配置?

一个依赖解决 Spring Boot 反爬虫,防止接口盗刷

一个依赖搞定 Spring Boot 反爬虫,防止接口盗刷!

Spring Cloud 任务的 SimpleTaskConfiguration 和 Spring Batch 的 SimpleBatchConfiguration 防止 Spring Boot 自动

Spring Boot、Spring Security - 防止 MongoDB 的直接 URL 查询

spring boot 防止重复提交