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>
相关推荐
九皇叔叔11 小时前
003-SpringSecurity-Demo 统一响应类
java·javascript·spring·springsecurity
夫礼者19 小时前
【极简监控】选连接池送深度监控?用 Druid 补齐单体应用全局 SQL 统计的最后拼图
java·数据库·sql·druid
九皇叔叔2 天前
004-SpringSecurity-Demo 拆分环境
java·springboot3·springsecurity
九皇叔叔2 天前
006-SpringSecurity-Demo 跨域(CORS)配置
java·springboot3·springsecurity·跨域·cors
九皇叔叔2 天前
005-SpringSecurity-Demo 配置外部文件映射
java·springboot·文件·springsecurity
九皇叔叔3 天前
001-SpringSecurity-Demo 创建项目
java·springboot·springsecurity
成为大佬先秃头6 天前
数据库连接池:Druid
数据库·mysql·druid
创梦流浪人12 天前
soli-admin一款开箱即用的RBAC后台项目
java·spring boot·vue3·springsecurity
一叶飘零_sweeeet16 天前
数据库连接池天花板之争:HikariCP 与 Druid 底层原理 + 高并发调优全拆解
数据库·hikaricp·数据库连接池·druid
我是TT啊17 天前
GaussDB_DWS连接池问题排查记录
springboot·gaussdb·druid