Cannot apply to AuthenticationConfiguration already built object

2023-11-06

前言

Spring Security:在Spring Boot 2.7.0中升级已弃用的WebSecurityConfigrerAdapter,并且根据@EnableWebSecurity推荐自定义配置类后,还是错误的问题。

失败的案例

首先看报错
在这里插入图片描述

Caused by: java.lang.IllegalStateException: Cannot apply org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration$EnableGlobalAuthenticationAutowiredConfigurer@74e40cc7 to already built object
	at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.add(AbstractConfiguredSecurityBuilder.java:182) ~[spring-security-config-5.7.3.jar:5.7.3]
	at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.apply(AbstractConfiguredSecurityBuilder.java:138) ~[spring-security-config-5.7.3.jar:5.7.3]
	at org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration.getAuthenticationManager(AuthenticationConfiguration.java:119) ~[spring-security-config-5.7.3.jar:5.7.3]
	at org.springframework.security.config.annotation.web.configuration.HttpSecurityConfiguration.authenticationManager(HttpSecurityConfiguration.java:109) ~[spring-security-config-5.7.3.jar:5.7.3]
	at org.springframework.security.config.annotation.web.configuration.HttpSecurityConfiguration.httpSecurity(HttpSecurityConfiguration.java:87) ~[spring-security-config-5.7.3.jar:5.7.3]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_291]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_291]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_291]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_291]
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.22.jar:5.3.22]
	... 51 common frames omitted

过时的配置类代码

@Configuration
@EnableWebSecurity
public class WebServerSecurityConfiguration {

    @Autowired
    private SecurityConfig securityConfig;

    @Autowired
    MyUserDetailService userDetailService;

    @Autowired
    JwtAuthenticationTokenFilter authenticationTokenFilter;

    @Bean
    public PasswordEncoder passwordEncoder() {
        DelegatingPasswordEncoder delegatingPasswordEncoder = (DelegatingPasswordEncoder) PasswordEncoderFactories.createDelegatingPasswordEncoder();
        delegatingPasswordEncoder.setDefaultPasswordEncoderForMatches(new BCryptPasswordEncoder());
        return delegatingPasswordEncoder;
    }

    @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        List<String> whiteList = securityConfig.getWhiteList();
        String[] whiteArrays = whiteList.toArray(new String[whiteList.size()]);
        http.authorizeRequests().antMatchers(whiteArrays).permitAll()
                .anyRequest().authenticated().and().csrf().disable();
        http.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        return http.build();
    }

    @Bean
    protected AuthenticationManager configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder
                .userDetailsService(userDetailService)
                .passwordEncoder(passwordEncoder())
        ;
        return authenticationManagerBuilder.build();
    }

}

可以看到在旧版本中AuthenticationManager ,您注入AuthenticationManagerBuilder,设置userDetailsService、passwordEncoder并构建它。但是身份验证管理器已经在这一步中创建。它是按照我们想要的方式创建的(使用userDetailsService和passwordEncoder)。

我们将注入的AuthenticationManagerBuilder 更换为下面的方式进行注入

	@Bean
    protected AuthenticationManager configure(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Cannot apply to AuthenticationConfiguration already built object 的相关文章

随机推荐

  • 图像分割中的损失函数

    图像分割中的损失函数 文章目录 图像分割中的损失函数 前言 一 交叉熵损失 二 Dice loss 三 Focal loss 四 IOU损失函数 总结 前言 在深度学习中 所有算法都依赖于最小化或最大化一个函数 称之为损失函数 损失函数用于
  • 蓝桥杯基础练习(1)---数列排序

    一 数列排序问题的解决 问题描述 给定一个长度为n的数列 将这个数列按从小到大的顺序排列 1 lt n lt 200 输入格式 第一行为一个整数n 第二行包含n个整数 为待排序的数 每个整数的绝对值小于10000 输出格式 输出一行 按从小
  • 全球13台DNS根服务器分布:

    全球13台DNS根服务器分布 美国VeriSign公司 2台 网络管理组织IANA Internet Assigned Number Authority 1台 欧洲网络管理组织RIPE NCC Resource IP Europeens N
  • 解决curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused 问题

    自动化使用要去下载图像识别的工具 Tesseract mac 需要用到brew 但是我的电脑没有 就先需要下载homebrew 然而按照官网的命令 bin bash c curl fsSL https raw githubuserconte
  • 将一个链表分为奇偶两个链表

    1 问题描述 设计一个算法 将一个头结点为A的单链表 其数据域为整数 分解成两个单链表A和B 使得A链表只含有原来链表data域为奇数的节点 而B链表只含有原链表中data域为偶数的节点 而且保持原来的顺序 2 思路分析 这个问题不是在线网
  • python pandas.set_option()详解

    一 简介 set option是pandas里的一个函数 用法为pandas set option pat value 主要作用是设置一些指定参数的值 供设置的选项如下 后面会逐一进行介绍 compute use bottleneck us
  • python研究生导师_研究生导师布置的作业!利用Python和API收集与分析网络数据!...

    猜猜看 下面这一组调查对象是什么 为什么会这样呢 因为我在布置作业的时候 很贴心地给了一个样例 是我之前写的一篇教程 如何用R和API免费获取Web数据 于是 多组作业 都雷同 讲到这里 他们一副不好意思的表情 我却发觉 这里蕴藏着一个问题
  • 上传文件,提交数据---FormData对象格式

    上传文件 提交数据 FormData对象格式 在进行上传文件 例如Excel 时 处理的几步 否则无法上传 一 修改请求头 在修改请求头 是至关重要的 因为请求数据格式是不同的 header multipart form data 注 在写
  • 4- OpenCV+TensorFlow 入门人工智能图像处理-灰度化处理

    图片特效及线段文字的绘制 特效1 灰度处理 mark 完成彩色图片灰度化 彩色图片有三个颜色通道RGB 灰度图片也是三通道的话 RGB值相等 单通道的灰度图片的值 需要经过RGB值进行计算 图中两个公式 一个是取均值 一个是根据公式 特效2
  • OD-求字符串中所有整数的最小和(Python)

    题目描述 说明 字符串s 只包含 a z A Z 合法的整数包括 1 正整数 一个或者多个0 9组成 如 0 2 3 002 102 2 负整数 负号 开头 数字部分由一个或者多个0 9组成 如 0 012 23 00023 输入描述 包含
  • 华为云DevCloud平台部署bootdo博客论坛实战【开发者专属集市】

    华为云DevCloud平台部署bootdo博客论坛实战 开发者专属集市 一 bootdo blog开源博客介绍 二 本次实践所用工具及平台 三 购买华为RDS数据库 1 购买RDS数据库 2 查看RDS数据库状态 四 创建项目 1 登录华为
  • sqli-labs通关记录

    sqli lab通关记录 docker搭建 运行 docker info 查看docker信息 确认docker正常 搜索sqli labs docker search sqli labs 建立镜像 docker pull acgpiano
  • sharding-jdbc 分库分表配置方案

    Java配置 Yaml配置 Spring Boot配置 Spring命名空间配置 http shardingsphere io document current cn manual sharding jdbc configuration c
  • 【送书活动】AI时代,程序员需要焦虑吗?

    前言 作者主页 雪碧有白泡泡 个人网站 雪碧的个人网站 推荐专栏 java一站式服务 React从入门到精通 前端炫酷代码分享 从0到英雄 vue成神之路 uniapp 从构建到提升 从0到英雄 vue成神之路 解决算法 一个专栏就够了 架
  • C++的std::is_same与std::decay

    一 背景 有一个模板函数 函数在处理int型和double型时需要进行特殊的处理 那么怎么在编译期知道传入的参数的数据类型是int型还是double型呢 include
  • 第五课 for循环(1)--循环次数控制

    第五课 for循环 1 循环次数控制 循环引入 例题5 1 画下面形状的5级梯形 分析 研究问题的方法之一是 从简单到复杂 步骤 说明 图形 步骤1 先分析简单的1级梯形基本问题 步骤2 代码为 pen fd 30 pen rt 90 pe
  • 为什么手机短信长度限制70个中文、160个英文???

    手机短信的长度是由编码决定的 根据国际标准 每条短信最多发送1120位 合 1120 8 140 一个字节占8位 140字节的内容 如果发送纯英文字符 由于英文ASCII采用 7位编码 所以1120位的限额可以传送1120 7 160个字符
  • VUE调用高德地图之热力图

    上次用VUE实现了高德地图的轨迹回放 现在来实现热力图功能 照例 第一步 加载JS AP 在public index html中加入 将官方demo转换为vue代码 放置地图 初始化map对象 生成热力图map 完整代码如下
  • latex表格中的字上下垂直居中

    单元格内容纵向靠上对齐 而是表线距单元格内容太近 调整表线和单元格内容之间的距离 可以通过重定义 arraystretch 来解决 renewcommand arrarstretch
  • Cannot apply to AuthenticationConfiguration already built object

    前言 Spring Security 在Spring Boot 2 7 0中升级已弃用的WebSecurityConfigrerAdapter 并且根据 EnableWebSecurity推荐自定义配置类后 还是错误的问题 失败的案例 首先