-
首先我们使用Druid包的自带密码加密工具对我们的密码进行加密
如下所示,我们可以得到私钥、公钥和密码密文:
C:UsersAdministrator>cd /d C:UsersAdministrator.m2 epositorycomalibabadruid1.1.6
C:UsersAdministrator.m2 epositorycomalibabadruid1.1.6>java -cp druid-1.1.6.jar com.alibaba.druid.filter.config.ConfigTools password
privateKey:MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAlwfk3zil6HQGe8QFz9UbttPds8k/JgEm6/feCl4cCmFW1DP7PKD2/TLFZCoeFFRhC1hqOEBhpadrR013huuFTQIDAQABAkAr4Cg2TP+twx8rD2xcz11IxM3wb/N/d/A1twFzY5mjzeXihuoZrefje1WN2q36YhAiEA5GgcdSV0qwYxAkUS24tgFp1EPlTmNKAa2YRWYUNUJkCIQCpRsuO9PrEVf8morsx5VousmyapnoOtW5Y7663zp7m1QIgb8sZaqkbkn3kTPUcBsMQE+GDvnJ1fnqCyrneQg+idbkCIGmzXkqPKMNOm7upY9C9UXnVSkGrcI4ctqHsUKJP8CNdAiEAnr2gYIT1EO3dPVpTyguKCxpawMnhQ/biwDUyzYb5mAw=
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJcH5N84peh0BnvEBc/VG7bT3bPJPyYBJuv33gpeHAphVtQz+zyg9v0yxWQqHhRUYQtYajhAYaWna0dNd4brhU0CAwEAAQ==
password:gXhFklHpK8L6Z39gjA0lFccC7zHREii6FLXtW43h8yqH+47Hbat6URMhX6R3X4qMunhpxfWlisoZXqnFLlbEcw==
-
在Spring boot的配置文件中配置密码和config filter.
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/videoDB
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.filters=stat,log4j,wall,config
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.maxActive=20
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
-
配置数据源的用户密码以及解密的配置选项,password和connectionProperties属性:
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/videoDB
spring.datasource.username=root
spring.datasource.password=gXhFklHpK8L6Z39gjA0lFccC7zHREii6FLXtW43h8yqH+47Hbat6URMhX6R3X4qMunhpxfWlisoZXqnFLlbEcw==
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.filters=stat,log4j,wall,config
spring.datasource.connectionProperties=config.decrypt=true;config.decrypt.key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJcH5N84peh0BnvEBc/VG7bT3bPJPyYBJuv33gpeHAphVtQz+zyg9v0yxWQqHhRUYQtYajhAYaWna0dNd4brhU0CAwEAAQ==
-
启动Spring boot应用进行测试
1)启动Application
2)测试有数据库连接服务的操作,如视频文件的上传
这时候可以测试可以知道数据库信息入库失败,日志提示数据库连接失败:
2018-01-17 15:10:20.481 ERROR 1144 --- [Create-15674126] com.alibaba.druid.pool.DruidDataSource : create connection SQLException, url: jdbc:mysql://127.0.0.1:3306/videoDB, errorCode 1045, state 28000
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
3)说明我们配置的解密过滤器没有生效,这也说明了根据Spring Boot+Druid是存在兼容性问题的,因为官方的样例是使用Spring+Druid的模式
-
最后得到的结论是:Druid与Spring Boot的兼容性问题还是比较多的,不建议使用该组合。
从项目提交的问题来看,也体现出与Spring Boot的兼容性问题,具体问题如下图所示。
-
最后这里提供过一种方法解决该问题,就是自定义DataSource配置文件解析类,初始化Druid的数据源,让其支持加密后的密文:
1)创建一个配置文件读初始化类 DataSourceConfig
该类主要完成DataSource个参数值得获取和解析,采用注解Configuration方式,主要的代码如下:
package image.dao;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
@ConfigurationProperties(prefix="spring.datasource")
public class DataSourceConfig {
private Logger logger=LoggerFactory.getLogger(DataSourceConfig.class);
private String type;
private String url;
private String driverClassName;
private String username;
private String password;
private String filters;
private String connectionProperties;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFilters() {
return filters;
}
public void setFilters(String filters) {
this.filters = filters;
}
public String getConnectionProperties() {
return connectionProperties;
}
public void setConnectionProperties(String connectionProperties) {
this.connectionProperties = connectionProperties;
}
@Bean
@Primary
public DataSource druidDataSource()
{
DruidDataSource dataSource=new DruidDataSource();
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClassName);
dataSource.setConnectionProperties(connectionProperties);
try {
dataSource.setFilters(filters);
} catch (SQLException e) {
e.printStackTrace();
}
logger.info(">>>>>>>>>>>>>>>>>>>>>>Druid DataSource init>>>>>>>>>>>");
return dataSource;
}
}
-
运行Spring boot的程序,进行视频文件数据的上传,检查数据库连接是否正常
1)启动Spring boot主类Application,可以看到日志输出了自定义的DruidDataSource初始化成功
[ restartedMain] image.dao.DataSourceConfig : >>>>>>>>>>>>>>>>>>>>>>Druid DataSource init>>>>>>>>>>>
2)在浏览器中访问视频上传的功能页面,进行视频文件上传,成功后可以看到数据库插入了一条心的记录,没有错误出现,说明我们已经解决了该问题