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>
相关推荐
java小罗_江西南昌6 天前
1.初识SpringSecurity
java·springsecurity
虎鲸不是鱼7 天前
Win10环境借助DockerDesktop部署大数据时序数据库Apache Druid
大数据·docker·apache·时序数据库·druid
java小罗_江西南昌9 天前
4.SpringSecurity在分布式环境下的使用
java·springsecurity
生产队队长14 天前
SpringSecurity:授权服务器与客户端应用(入门案例)
oauth2·springsecurity
一二小选手1 个月前
【SpringSecurity】SpringSecurity安全框架授权
java·springsecurity·安全框架
书晨0071 个月前
【BUG排查记】HttpUtil和SpringSecurity结合的坑
bug·springsecurity·hutool·httprequest
跳跳的向阳花2 个月前
07、SpringSecurity系列,第五章:原理初探
原理·springsecurity
java_学习爱好者2 个月前
SpringSecurity使用教程
spring boot·springsecurity
机跃2 个月前
springSecurity权限控制
springsecurity
长臂人猿3 个月前
SpringSecurity构建登录模块
登录·token·springsecurity