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接口

httpSecurity.authorizeHttpRequests(
    auth -> auth
        .requestMatchers("/druid/**").permitAll());

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

.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>
  • 44
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot是一个用于快速开发基于Java的应用程序的开源框架。它提供了标准化的配置和约定,使开发人员能够轻松地构建和部署应用程序。Druid是一个开源的数据库连接池,它提供了高可用性、高性能和监控功能。TDengine是一个高性能、高可用性的开源时间序列数据库,可以用于海量数据的存储和查询。MySQL是一个广泛使用的关系型数据库管理系统。 在Spring Boot中,我们可以使用Druid来封装MySQL和TDengine的连接池。首先,我们需要在项目的pom.xml文件中引入相应的依赖。对于Druid和MySQL,我们可以使用以下依赖: ```xml <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> ``` 对于TDengine,我们可以使用以下依赖: ```xml <dependencies> <dependency> <groupId>com.taobao</groupId> <artifactId>tdengine-sdk</artifactId> <version>2.0.0</version> </dependency> </dependencies> ``` 接下来,我们需要配置Druid和MySQL的连接信息,我们可以在application.properties或application.yml文件中进行配置。例如,对于MySQL,我们可以使用以下配置: ```yaml spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 对于TDengine,我们可以使用以下配置: ```yaml spring.datasource.url=jdbc:tdengine://localhost:6020/mydatabase spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.alibaba.druid.TDEngineDriver ``` 完成这些配置后,我们可以在Spring Boot应用程序的代码中使用Druid来获取数据库连接。对于MySQL和TDengine,我们可以使用以下示例代码: ```java import com.alibaba.druid.pool.DruidDataSource; public class Example { private DruidDataSource dataSource; public Example() { dataSource = new DruidDataSource(); dataSource.setUrl("jdbc:mysql://localhost:3306/mydatabase"); dataSource.setUsername("root"); dataSource.setPassword("123456"); dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); // 其他配置... } // 使用dataSource获取连接进行操作 } ``` 以上是使用Spring Boot和Druid封装MySQL和TDengine的一种简单方法。通过使用这些封装,我们可以更加方便地在Spring Boot应用程序中使用MySQL和TDengine进行数据存储和查询操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值