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>
相关推荐
阳爱铭7 天前
深度分析Apache Druid:定义、对比、使用场景、选型指南及注意事项
java·大数据·数据库·后端·架构·apache·druid
奋斗的袍子00713 天前
SpringBoot:SpringBoot集成Druid监控慢SQL
spring boot·后端·sql·spring·数据库连接池·慢sql·druid
慕木兮人可1 个月前
SpringBoot2.0.x旧版集成Swagger UI报错Unable to infer base url...解决办法
java·spring boot·cas·springsecurity·swagger-ui
徐子元竟然被占了!!3 个月前
SpringSecurity登录时在哪里调用我们自定义的UserDetailsServiceImpl
java·spring·springsecurity
谢小涛3 个月前
mybatis实体中时间类型LocalDateTime,查询的时候报错
spring boot·mybatis·druid·localdatetime
-代号95273 个月前
【SpringSecurity】十七、OAuth2授权服务器 + 资源服务器Demo
java·oauth2·springsecurity
小钱c74 个月前
关于SpringSecurity出现Failed to evaluate expression ‘ps.hasAuthority(‘role‘)‘
java·springsecurity
请回答10244 个月前
SpringSecurity 快速入门
spring·springsecurity
请回答10244 个月前
SpringSecurity原理简述
springsecurity
青石路4 个月前
都说了能不动就别动,非要去调整,出生产事故了吧 → 补充
mybatis·mybatis plus·druid·hikari