springboot3+springsecurity6集成druid启动报错

环境:springboot3+security6+druid1.2.20

报错:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'filterChain' defined in class path resource [com/free/security/SecurityConfiguration.class]: Failed to instantiate [org.springframework.security.web.SecurityFilterChain]: Factory method 'filterChain' threw exception with message: This method cannot decide whether these patterns are Spring MVC patterns or not. If this endpoint is a Spring MVC endpoint, please use requestMatchers(MvcRequestMatcher); otherwise, please use requestMatchers(AntPathRequestMatcher).

This is because there is more than one mappable servlet in your servlet context: {org.springframework.web.servlet.DispatcherServlet=[/], com.alibaba.druid.support.jakarta.StatViewServlet=[/druid/*]}.

For each MvcRequestMatcher, call MvcRequestMatcher#setServletPath to indicate the servlet path.

原因:

看报错位置加粗内容是重点

意思是 对于每一个接口,security无法确定这个接口是不是mvc接口,请开发者明确的声明这个接口为MvcRequestMatcher或者AntPathRequestMatcher

看代码,在security配置放行接口的位置,配置了放行druid接口

java 复制代码
httpSecurity.authorizeHttpRequests(
    auth -> auth
        .requestMatchers("/druid/**").permitAll());

这样的写法是不对的,应该明确的声明这个pattern是哪种pattern,改为

java 复制代码
.requestMatchers(AntPathRequestMatcher.antMatcher("/druid/**")).permitAll()

再次启动项目,不再报错

结论:问题并不是因为druid产生的,而是security配置类代码不够严谨

另外,值得一提的是

druid1.2.20这个版本开始支持springboot3自动配置,不再需要手动引入

springboot3的依赖包与springboot2不同

依赖为

复制代码
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-3-starter</artifactId>
    <version>1.2.20</version>
</dependency>
相关推荐
佛祖让我来巡山16 小时前
小明网站双登录系统实现——微信授权登录+用户名密码登录完整指南
oauth2·springsecurity·微信授权登录
佛祖让我来巡山1 天前
Spring Security 鉴权流程与过滤器链深度剖析
springsecurity·authenticationmanager
佛祖让我来巡山2 天前
大型项目基于Spring Security的登录鉴权与数据权限控制完整方案
springsecurity·保姆级鉴权·大型项目登录认证
佛祖让我来巡山2 天前
Spring Security前后端分离接入流程保姆级教程
权限校验·springsecurity·登录认证
佛祖让我来巡山2 天前
Spring Security 认证流程闭环与调用链路详解
springsecurity·authenticationmanager
佛祖让我来巡山3 天前
小明的Spring Security入门到深入实战
springsecurity
佛祖让我来巡山4 天前
⚠️登录认证功能的成长过程:整体概述
安全·登录·springsecurity·登录认证·认证授权
belldeep1 个月前
网络安全:Apache Druid 安全漏洞
网络安全·apache·druid
码熔burning3 个月前
Spring Security 深度学习(六): RESTful API 安全与 JWT
安全·spring·restful·springsecurity
A尘埃3 个月前
SpringSecurity版本的不同配置
认证·springsecurity·安全配置·不同版本