springboot3 SecurityConfig SecurityFilterChain 需要使用CorsFilter,实际是CorsWebFilter

news2024/9/23 16:12:40

使用springboot3做微服务开发,由于网关gateway使用webFlux,因此导致实际类型是CorsWebFilter,但是在public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception方法中,项目启动报错异常信息:

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-08-25 00:19:48.514 [main] ERROR org.springframework.boot.SpringApplication:859 - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration': Unsatisfied dependency expressed through method 'setFilterChains' parameter 0: Error creating bean with name 'authorizationServerSecurityFilterChain' defined in class path resource [org/jeecg/config/security/SecurityConfig.class]: Failed to instantiate [org.springframework.security.web.SecurityFilterChain]: Factory method 'authorizationServerSecurityFilterChain' threw exception with message: Bean named 'corsFilter' is expected to be of type 'org.springframework.web.filter.CorsFilter' but was actually of type 'org.springframework.web.cors.reactive.CorsWebFilter'
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.resolveMethodArguments(AutowiredAnnotationBeanPostProcessor.java:895)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:848)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:145)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:508)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1421)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:599)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:337)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:335)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:975)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:962)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:624)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:335)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1363)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1352)
	at org.jeecg.JeecgSystemCloudApplication.main(JeecgSystemCloudApplication.java:45)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authorizationServerSecurityFilterChain' defined in class path resource [org/jeecg/config/security/SecurityConfig.class]: Failed to instantiate [org.springframework.security.web.SecurityFilterChain]: Factory method 'authorizationServerSecurityFilterChain' threw exception with message: Bean named 'corsFilter' is expected to be of type 'org.springframework.web.filter.CorsFilter' but was actually of type 'org.springframework.web.cors.reactive.CorsWebFilter'
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:648)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:636)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1337)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1167)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:337)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:335)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
	at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.addCandidateEntry(DefaultListableBeanFactory.java:1689)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1653)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveMultipleBeanCollection(DefaultListableBeanFactory.java:1543)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveMultipleBeans(DefaultListableBeanFactory.java:1511)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1392)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1353)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.resolveMethodArguments(AutowiredAnnotationBeanPostProcessor.java:887)
	... 20 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.web.SecurityFilterChain]: Factory method 'authorizationServerSecurityFilterChain' threw exception with message: Bean named 'corsFilter' is expected to be of type 'org.springframework.web.filter.CorsFilter' but was actually of type 'org.springframework.web.cors.reactive.CorsWebFilter'
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:177)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:644)
	... 37 common frames omitted
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'corsFilter' is expected to be of type 'org.springframework.web.filter.CorsFilter' but was actually of type 'org.springframework.web.cors.reactive.CorsWebFilter'
	at org.springframework.beans.factory.support.AbstractBeanFactory.adaptBeanInstance(AbstractBeanFactory.java:422)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:403)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:205)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1240)
	at org.springframework.security.config.annotation.web.configurers.CorsConfigurer.getCorsFilter(CorsConfigurer.java:85)
	at org.springframework.security.config.annotation.web.configurers.CorsConfigurer.configure(CorsConfigurer.java:73)
	at org.springframework.security.config.annotation.web.configurers.CorsConfigurer.configure(CorsConfigurer.java:41)
	at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.configure(AbstractConfiguredSecurityBuilder.java:376)
	at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.doBuild(AbstractConfiguredSecurityBuilder.java:330)
	at org.springframework.security.config.annotation.AbstractSecurityBuilder.build(AbstractSecurityBuilder.java:38)
	at org.jeecg.config.security.SecurityConfig.authorizationServerSecurityFilterChain(SecurityConfig.java:123)
	at org.jeecg.config.security.SecurityConfig$$SpringCGLIB$$0.CGLIB$authorizationServerSecurityFilterChain$5(<generated>)
	at org.jeecg.config.security.SecurityConfig$$SpringCGLIB$$FastClass$$1.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:258)
	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:348)
	at org.jeecg.config.security.SecurityConfig$$SpringCGLIB$$0.authorizationServerSecurityFilterChain(<generated>)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:140)
	... 38 common frames omitted

异常情况如下图所示:
在这里插入图片描述
1、异常情况分析
gateway网关使用webFlux,使用是CorsWebFilter,因此导致项目启动异常。

2、解决方案
添加跨域和禁用csrf,关键代码如下

 @Bean
    @Order(1)
    public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http)
            throws Exception {
        OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http);
        // 注册自定义登录类型
        http.getConfigurer(OAuth2AuthorizationServerConfigurer.class)
                .tokenEndpoint(tokenEndpoint -> tokenEndpoint.accessTokenRequestConverter(new PasswordGrantAuthenticationConvert())
                        .authenticationProvider(new PasswordGrantAuthenticationProvider(authorizationService, tokenGenerator())))
                .tokenEndpoint(tokenEndpoint -> tokenEndpoint.accessTokenRequestConverter(new PhoneGrantAuthenticationConvert())
                        .authenticationProvider(new PhoneGrantAuthenticationProvider(authorizationService, tokenGenerator())))
                .tokenEndpoint(tokenEndpoint -> tokenEndpoint.accessTokenRequestConverter(new AppGrantAuthenticationConvert())
                        .authenticationProvider(new AppGrantAuthenticationProvider(authorizationService, tokenGenerator())))
                .tokenEndpoint(tokenEndpoint -> tokenEndpoint.accessTokenRequestConverter(new SocialGrantAuthenticationConvert())
                        .authenticationProvider(new SocialGrantAuthenticationProvider(authorizationService, tokenGenerator())))
                //开启OpenID Connect 1.0(其中oidc为OpenID Connect的缩写)。 访问 /.well-known/openid-configuration即可获取认证信息
                .oidc(Customizer.withDefaults());
        http
        		// 关键代码开始位置
                .cors(cors -> cors
                        .configurationSource(req -> {
                            CorsConfiguration config = new CorsConfiguration();
                            config.applyPermitDefaultValues();
                            config.setAllowedMethods(Arrays.asList("HEAD", "GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"));
                            return config;
                        }))
                .csrf(AbstractHttpConfigurer::disable)
                 // 关键代码结束位置
                //将需要认证的请求,重定向到login页面行登录认证。
                .exceptionHandling((exceptions) -> exceptions
                        .defaultAuthenticationEntryPointFor(
                                new LoginUrlAuthenticationEntryPoint("/sys/login"),
                                new MediaTypeRequestMatcher(MediaType.TEXT_HTML)
                        )
                )
                // 使用jwt处理接收到的access token
//                .oauth2ResourceServer(oauth2ResourceServer ->
//                        oauth2ResourceServer.jwt(Customizer.withDefaults()))
        ;

        return http.build();
    }

关键代码截图:
在这里插入图片描述

3.相关大数据学习demo地址:
https://github.com/carteryh/big-data

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2074252.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

rt-studio+clion+cubemx联合使用(使用scons进行整合)

前言 以前在clion中使用rt-thread的方式 1. 使用的cubemx生成的方式: 这种方式只能使用rt-thread的内核版本 2. 自己去把rt-thread的源码拷贝到对应的工程中&#xff0c;再编写对应的CMakelists文件进行管理思考 我的想法是通过rt-studio创建项目&#xff0c;然后通过工具转…

河南萌新2024第二场

H 狼狼的备忘录 题目大意&#xff1a; 给定n本备忘录&#xff0c;里面记录了一个人的m个星座信息&#xff0c;要求按一下要求整理备忘录 A&#xff1a;同一个成员的星座信息 x 是星座信息 y 的后缀&#xff0c;那么星座信息 x 会没有星座信息 y 完整&#xff0c;从而应该只保…

Nginx + Docker Compose前后端分离部署到服务器过程记录

一、采用Nginx部署前端VUE&#xff08;Vite&#xff09; 1、修改配置文件vite.config.ts&#xff0c;将本地环境改为开发环境 注意base处只能是‘/’ 不能是 ‘./!在这里插入图片描述 对项目进行打包 在当前目录的终端执行&#xff1a;npm run build 若报错如下&#xff1…

CORS错误

说明&#xff1a;记录一次CORS&#xff08;跨域&#xff09;错误&#xff0c;及解决方法。 场景 在vscode里面运行前端项目&#xff0c;idea中运行后端项目&#xff0c;登录时&#xff0c;访问接口&#xff0c;报CORS错误&#xff0c;如下&#xff1a; 解决 在后端项目的网关…

【PyQt6 应用程序】PyUIC使用加载可视化文件

使用uic模块可以方便地从Qt Designer设计的UI文件加载用户界面。这种方法使得设计和布局变得更加直观,并且可以将用户界面设计与程序逻辑分离。 本次展示如何使用PyQt6和uic模块来加载一个简单的UI文件。 文章目录 需要使用Qt Designer创建一个UI文件。Qt Designer是一个强大…

并行 parallel DOP 受 Resource Manager 限制

监控 Script select s.SID, s.SERIAL#, s.username ,rpd.plan, s.RESOURCE_CONSUMER_GROUP, rpd.PARALLEL_DEGREE_LIMIT_P1 from v$session s, DBA_RSRC_CONSUMER_GROUPS rcg, DBA_RSRC_PLAN_DIRECTIVES rpd , V$RSRC_CONSUMER_GROUP v…

甲方(北汽)渗透测试面试经验分享

《网安面试指南》http://mp.weixin.qq.com/s?__bizMzkwNjY1Mzc0Nw&mid2247484339&idx1&sn356300f169de74e7a778b04bfbbbd0ab&chksmc0e47aeff793f3f9a5f7abcfa57695e8944e52bca2de2c7a3eb1aecb3c1e6b9cb6abe509d51f&scene21#wechat_redirect 所在城市&…

IDEA 2024.2.0.2 使用 Jrebel and XRebel 热部署

安装 激活 工具网站中url和邮箱复制进去 设置 允许项目自动构建 允许开发过程中自动部署

苍穹外卖项目DAY10

苍穹外卖项目DAY10 1、Spring Task 1.1、介绍 Spring Task是Spring框架提供的任务调度工具&#xff0c;可以按照约定的时间自动执行某个代码逻辑 定位&#xff1a;定时任务框架 作用&#xff1a;定时自动执行某段Java代码 只要是需要定时处理的场景都可以使用Spring Task…

大数据技术之Flume 拓扑结构(4)

目录 Flume 拓扑结构 简单串联 (Simple Serial) 复制和多路复用 (Replication and Multiplexing) 负载均衡和故障转移 (Load Balancing and Failover) 聚合 (Aggregation) 示例配置 Flume 拓扑结构 Flume 支持多种拓扑结构来满足不同的数据收集和传输需求。以下是 Flume 中常…

复现ssrf漏洞

目录 一、pikachu靶场 1、靶场环境&#xff1a; 使用docker拉取&#xff1a; docker run -d -p 8765:80 8023/pikachu-expect:latest 2、使用dict 3、使用file读取文件 二、redis未授权访问 1、源码 2、使用bp探测端口 3、继续使用bp探测172.18.0.2的端口 4、使用go…

鸿蒙(API 12 Beta3版)【使用AVPlayer开发音频播放功能(C/C++)】音视播放与录制

使用AVPlayer可以实现端到端播放原始媒体资源&#xff0c;本开发指导将以完整地播放一首音乐作为示例&#xff0c;向开发者讲解AVPlayer音频播放相关功能。 播放的全流程包含&#xff1a;创建AVPlayer&#xff0c;设置播放资源&#xff0c;设置播放参数&#xff08;音量/倍速/…

运用Archimate为 智慧文旅搭建 数字化架构体系【系统架构】

ArchiMate是一种用于企业架构建模的开放、独立且详细的语言&#xff0c;它提供了一套丰富的概念和关系来描述、分析和可视化企业架构的不同领域。以下是ArchiMate建模的一些关键功能&#xff1a; 多视图建模&#xff1a;ArchiMate定义了23个示例视图&#xff0c;分为四类&#…

python模块04-requests

1 requests模块发送请求 Requests是一个Python HTTP庫 相关参考资料&#xff1a; 文档&#xff1a;Requests: 让 HTTP 服务人类 — Requests 2.18.1 文档 requests PyPI&#xff1a;requests PyPI requests源码&#xff1a;GitH0ub - psf/requests: A simple, yet elegant, …

封装车牌号码输入组件

<!-- Title: 国内车辆号牌号码输入组件Description: 国内车辆号牌号码输入组件&#xff0c;具体使用方法如下&#xff1a;<its-hphmInput v-model"form.hphm" :glbm"京A" :parentmessage.sync"hphm" onChange"provinceAbbreviationC…

基于R语言进行AMMI分析3

参考资料&#xff1a;https://cran.r-project.org/web/packages/agricolae/agricolae.pdf 1、plot()函数 本次介绍的是Agricolae包中的plot.AMMI()函数。此函数可以绘制AMMI双标图&#xff0c;也可以绘制三标图&#xff08;三个坐标轴&#xff0c;IPCA1&#xff0c;IPCA2&…

「C++系列」类/对象

文章目录 一、类1. 基本类的定义2. 类的访问控制3. 类的实例化4. 构造函数和析构函数5. 继承6. 类的使用 二、对象1. 创建对象2. 对象的生命周期3. 对象的内存4. 对象的操作5. 对象的复制6. 总结 三、应用案例1. 定义BankAccount类2. 分析 四、相关链接 一、类 C 类&#xff0…

【初阶数据结构】顺序表和链表算法题(下)

链表 2.链表2.4合并两个有序链表2.5链表分割2.6链表的回⽂结构2.7相交链表2.8环形链表I2.9 环形链表II2.10随机链表的复制 2.链表 2.4合并两个有序链表 思路 /*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/ …

一键转换语言,探索四大在线翻译工具的魅力!

各种翻译工具不仅为个人用户提供了极大的便利&#xff0c;也为跨国企业、学术研究和文化交流提供了强有力的支持&#xff0c;接下来为大家推荐几款好用的翻译在线工具&#xff01; 福昕在线翻译 直达链接&#xff1a; https://fanyi.pdf365.cn/ 福昕在线翻译是一款功能强大…

分布式锁 redis与zookeeper

redis实现分布式锁 原理 基于redis命令setnx key value来实现分布式锁的功能&#xff0c;只有当key不存在时&#xff0c;setnx才可以设置成功并返回1&#xff0c;否则设置失败返回0。 方案1&#xff1a; 方案1存在的问题 假如在加锁成功&#xff0c;释放锁之前&#xff0c;…