前言
闹腾了一天,什么访问Druid登录界面出现重定向过多,什么登录界面可以看到了就是登不进去的种种问题总算是解决了。
下面我们就来详细介绍如何在原生的JDBC上整合Druid。
一.新建一个整合了JDBC数据源的项目
[1] 引入starter
[2] 配置application.yml (关于数据源的相关配置可参考DataSourceProperties.class)
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://192.168.31.122:3306/jdbc
driver-class-name: com.mysql.jdbc.Driver
[3] 编写一个测试类(查看原生jdbc是否连通)
package com.gs.springboot;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@SpringBootTest
class Springboot06DataJabcApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException {
//com.zaxxer.hikari.HikariDataSource
System.out.println(dataSource.getClass());
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
}
解析:我们可知道系统默认使用的是com.zaxxer.hikari.HikariDataSource作为数据源
二. 在原有的jdbc上整合Druid
[1] 在pom.xml中引入druid数据源
<!--引入druid数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.8</version>
</dependency>
[2] 在application.yml中引入druid的相关配置
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://192.168.31.122:3306/jdbc
driver-class-name: com.mysql.jdbc.Driver
initialization-mode: always
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
# 数据源其他配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
# 进行数据库文件访问路径的修改
# schema:
# - classpath:department.sql
如果单纯在yml文件中编写如上的配置,SpringBoot肯定是读取不到druid的相关配置的。因为它并不像我们原生的jdbc,系统默认就使用DataSourceProperties与其属性进行了绑定。所以我们应该编写一个类与其属性进行绑定
[3] 编写整合druid的配置类DruidConfig
public class DruidConfig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druid(){
return new DruidDataSource();
}
}
当编写完上面的类兴高采烈地去测试的时候,突然发现控制台报错了。经过查找发现是yml文件里的
filters: stat,wall,log4j
因为我们springBoot2.0以后使用的日志框架已经不再使用log4j了。此时应该引入相应的适配器。我们可以在pom.xml文件上加入
<!--引入适配器-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
这时进行相关测试就可以了,不过我们只是把相关的数据源引入,还没有配置Druid的监控,这样我们是无法访问它相应的登录界面与监控管理内容的。接下来继续完善DruidConfig类
package com.gs.springboot.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druid(){
return new DruidDataSource();
}
//配置Druid的监控
//1.配置一个管理后台的Servlet
@Bean
public ServletRegistrationBean statViewServlet(){
// 记得加上"/druid/*",否则在进行登录页面的重定向过多而无法访问的问题(记得在Google浏览器才会报这个错)
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");
Map<String,String> initParams = new HashMap<>();
initParams.put("loginUsername","admin");
initParams.put("loginPassword","123456");
//默认是允许所有访问
//initParams.put("allow","");
// initParams.put("deny","192.168.31.30");
bean.setInitParameters(initParams);
return bean;
}
//2.配置一个web监控的filter
@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
Map<String,String> initParams = new HashMap<>();
//配置拦截时需要排除的请求
initParams.put("exclusions","*.js,*.css,/druid/*");
bean.setInitParameters(initParams);
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
}
错误解析:当我们在编写管理后台的Servlet这个方法时,一定要加上相应的地址,否则访问相应页面会报重定向次数过多
// 记得加上"/druid/*",否则在进行登录页面的重定向过多而无法访问的问题(记得在Google浏览器才会报这个错)
ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");
[4] 如果以上面的方式访问http://localhost:8080/druid ,你会发现你可以看到登录页面了,可别高兴的太早,你会发现无论怎么登录都进不去主页面。主要是因为被csrf拦截,我们得编写一个相关的安全配置类把我们被拦截的地址进行过滤
package com.gs.springboot.config;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//对我们访问的/druid/*进行过滤拦截
http.csrf().ignoringAntMatchers("/druid/*");
}
}
就这样总算雨过天晴了 当我们访问 http://localhost:8080/druid时
登录后
多么赏心悦目的两个页面。自此SpringBoot2.0整合Druid就算完成了。我还是对我踩过的坑做个小结:
[1] 能用google进行相关测试的一定要用Google,就像我上面那个重定向的问题排查了n久,别的浏览器就一直只会404,其余的啥也没有
[2] 整合日志框架,要看清楚我们现在所适配的版本,如果已过时,我们可以引入相应的适配器。
[3] 遇到某些页面一直无法登陆,可能是某些路径被拦截了,我们应该去追寻什么东西拦截了什么地址。
好了,今天的分享就这么多了。如果可以让各位大佬们少踩小弟今天的坑的话,我就十分荣幸了,如果对你有所帮助的话,可以点赞,关注。如果有任何疑问,我们可以在留言区进行探讨。