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

2024-05-18

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

我已经看到了防止 Spring Boot 注册 servlet 过滤器 https://stackoverflow.com/questions/28421966/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:[断言失败] - 此字符串参数必须有长度;它不能为 null 或为空

这是完全不稳定的;Asserts in afterPropertiesSet() method https://github.com/spring-projects/spring-security/blob/master/web/src/main/java/org/springframework/security/web/authentication/AnonymousAuthenticationFilter.java 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未注册(已禁用)

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

DEBUG 4916 --- [ost-startStop-1] o.s.security.web.FilterChainProxy:过滤器“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 应用程序中禁用此类过滤器?


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);
    }
    ...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

防止 Spring Boot 注册 Spring Security 过滤器之一 的相关文章

随机推荐

  • 在 jQuery .live() 方法中模拟“焦点”和“模糊”

    Update 从 jQuery 1 4 开始 live 现在支持focusin and focusout events jQuery http www jquery com currently1 doesn t support blur o
  • Matplotlib - 可以用寄生轴制作子图吗?

    我正在尝试制作一个包含两个子图的图表 每个子图都有一个寄生轴 如文档中所示here https matplotlib org examples axes grid demo parasite axes2 html 然而 虽然我可以用单个图复
  • 使用记录而不是类有什么缺点?

    C 9 介绍record https devblogs microsoft com dotnet c 9 0 on the record 参考类型 记录提供了一些综合方法 例如复制构造函数 克隆操作 哈希码计算和比较 相等操作 在我看来 使
  • 如何将 tera::Error 转换为 actix_web::Error?

    我正在学习rust actix tera并且不知道如何实施ResponseError上的特质tera Error 或者如何转换tera Error to actix web Error 使用以下代码片段 match TEMPLATES re
  • GRPC/C++ - 如何检测异步服务器中客户端断开连接

    我正在使用这个代码example https github com grpc grpc blob v1 32 0 examples cpp helloworld greeter async server cc创建我的 GRPC 异步服务器
  • 如何在 Caret 中绘制随机森林(护林员)树

    我生成了如下所示的随机森林树 并尝试绘制它 但出现错误 我在哪里犯了错误 我怎样才能以正确的方式绘制它 Actmodel lt train Activity Section Author data CB1 method ranger trC
  • 使用 scalapb 在 Spark Streaming 中解码 Proto Buf 消息时出错

    这是一个 Spark Streaming 应用程序 它使用编码的 Kafka 消息Proto Buf Using scalapb图书馆 我收到以下错误 请帮忙 gt com google protobuf InvalidProtocolBu
  • 当不支持 Google Play 应用内结算 V.3 时

    在 Google Play 的应用内结算 V 3 中 有一个选项可以检查用户设备是否支持它 使用是否支持计费 http developer android com google play billing versions html meth
  • 如何在 DOM 中的每个元素中调用函数,即使它们是动态创建的

    我想对 DOM 上的特定元素调用函数 例如 red css backgroundColor pink 它适用于 DOM 中已经存在的任何元素 但我也希望在动态添加到 DOM 的元素中调用此方法 我尝试过类似的事情 red on functi
  • 实体框架代码首先保存后不延迟加载

    我的数据库中有一个查找表和一个数据表 我将使用性别和人物作为例子 假设性别表如下所示 Id Code 1 Male 2 Female 人员表如下所示 Id Name GenderId 1 Bob 1 2 Jane 2 我首先在 EF 代码中
  • 带表格格式的 Matplotlib 条形图

    我在图的底部添加了一个表格 但它存在许多问题 右边的内边距太多了 左边的填充太少 底部没有填充物 单元格对于其中的文本来说太小 该表距离图的底部太近 属于行名称的单元格的颜色未与条形图的颜色相匹配 我要发疯了 去摆弄这个 有人可以帮我解决这
  • 使用 Firefox 插件 sdk 的 nsISocketTransportService

    我正在尝试使用 Firefox 来读取 SSH 横幅 IE 当您最初连接到 SSH 服务器时 服务器会向您发送其横幅 标识服务器软件 并且您向 SSH 服务器发送您的横幅 标识您的客户端软件 为此 我使用以下 URL 中的示例 firefo
  • 使用哈希参数进行 DRY Ruby 初始化

    我发现自己经常在构造函数中使用哈希参数 尤其是在为配置或最终用户将接触到的其他 API 编写 DSL 时 我最终做的是类似以下的事情 class Example PROPERTIES name age PROPERTIES each p a
  • Angular 模态对话框最佳实践

    与不具有动态内容的对话框相比 创建具有动态内容的模式对话框的最佳实践是什么 例如 我们有一些模式表单接受表单元素列表 并具有提交 取消功能 此外 还有一些模式对话框仅显示确认 确定类型的操作 我见过很多人说对话框应该是传递到控制器的服务 但
  • 在线运行 Facebook FQL

    我可以在网络浏览器中的哪里在线运行 FQL 查询 我可以在网络浏览器中的哪里在线运行 FQL 查询 在 Facebook 开发者中图形浏览器 https developers facebook com tools explorer工具 采用
  • 如何从 C# 中的两个列表中获取唯一对象?

    我有两个列表对象 例如 list1 id 101 name one id 102 name two id 103 name three list2 id 111 name one id 112 name two id 114 name fo
  • R、Rcpp 与 Armadillo 中矩阵 rowSums() 与 colSums() 的效率

    背景 来自 R 编程 我正在扩展到 C C 形式的编译代码Rcpp 作为循环交换 以及一般的 C C 效果的实践练习 我实现了 R 的等效项rowSums and colSums 矩阵的函数Rcpp 我知道它们以 Rcpp 糖的形式存在 并
  • 检查一个数字是 int 还是 float

    在perl中 我想检查给定变量是否包含浮点数 为了检查我正在使用的 my Var 0 02 Floating point number if int Var Var floating point number 但上面的代码对于 0 0 不起
  • 我点击的号码没有出现在反应项目的屏幕上

    当我尝试在屏幕中呈现点击的数字时 该数字没有出现 但它确实可以在控制台中显示 我不知道如何修复它应该在屏幕上 号码 旁边应用的号码 import render from testing library react import React
  • 防止 Spring Boot 注册 Spring Security 过滤器之一

    我想禁用安全链中的 Spring Security 过滤器之一 我已经看到了防止 Spring Boot 注册 servlet 过滤器 https stackoverflow com questions 28421966 prevent s