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>
相关推荐
长路 ㅤ   13 天前
Druid手写核心实现案例 实现一个简单Select 解析,包含Lexer、Parser、AstNode
druid·java编程·语法分析·sql解析·ast构建
长路 ㅤ   13 天前
Druid学习笔记 01、快速了解Druid中SqlParser实现
druid·抽象语法树·druid sql解析
世纪摆渡人1 个月前
SpringSecurity-SpringSecurity入门介绍
springsecurity
1.01^10001 个月前
[7-01-01].第01章:安全与权限 - 核心概念
springsecurity
喜欢踢足球的老罗2 个月前
在Spring Boot 3.3中使用Druid数据源及其监控功能
java·spring boot·后端·druid
梦想画家2 个月前
Apache Druid 架构深度解析:构建高性能分布式数据存储系统
架构·druid·数据工程
xiezhr3 个月前
SpringBoot3整合SpringSecurity6(五)自定义登陆页面
java·spring·springboot3·springsecurity
去哪里吃鱼3 个月前
自定义Spring Authorization Server登录页
springsecurity·spring authorization server·springauthorizationserver
xiezhr4 个月前
SpringBoot3整合SpringSecurity6(二)SpringSecurity默默的干了些啥
java·spring boot·springboot3·springsecurity