1.第一种方式:使用srpingboot druid
pom.xml引入
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
测试类对密码进行加密解密
/**
* 使用Druid数据库密码加密配置
* @param args
*/
public static void main(String[] args) {
System.out.println("1111");
//加密
try {
String miwenofter = ConfigTools.encrypt("aaa");
System.out.println("加密后"+miwenofter);
} catch (Exception e) {
e.printStackTrace();
}
//解密
try {
String mingwen = ConfigTools.decrypt("frL2qjh4LPrVaWZdzZkfVKDPgyrbfaEofNBMrk9fHoYE7KJ08RSQEjCS+9JMKe/w==");
System.out.println("解密后:"+mingwen);
} catch (Exception e) {
e.printStackTrace();
}
}
application.properties配置文件对数据源进行配置
server.port=8080
spring.datasource.druid.driverClassName=com.mysql.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://XXX:3306/vehicle?useSSL=false&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true
spring.datasource.druid.username=vehicle
#密码
spring.datasource.druid.password=通过测试类获取到加密后密码
spring.datasource.type: com.alibaba.druid.pool.DruidDataSource
# 初始化大小、最小、最大连接数
spring.datasource.druid.initial-size=3
spring.datasource.druid.min-idle=3
spring.datasource.druid.max-active=10
# 配置获取连接等待超时的时间
spring.datasource.druid.max-wait=60000
# 监控后台账号和密码
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=admin
# 配置 StatFilter
spring.datasource.druid.filter.stat.log-slow-sql=true
spring.datasource.druid.filter.stat.slow-sql-millis=2000
#数据库加密 这两步很重要
spring.datasource.druid.filters = config,wall,stat
spring.datasource.druid.connectionProperties=druid.stat.slowSqlMillis=200;druid.stat.logSlowSql=true;config.decrypt=true
启动
访问:http://localhost:8080/druid/login.html
===============================================
2.第二种方式 使用spring boot jasypt
pom.xml引入
<!--数据库加密使用-->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
测试类进行密码加密解密
import org.jasypt.encryption.StringEncryptor;
import org.junit.Assert;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class Test {
@Autowired
StringEncryptor encryptor;
@org.junit.Test
public void getPass() {
String url = encryptor.encrypt("jdbc:mysql://xxxx:3306/sell?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8");
String name = encryptor.encrypt("2018");
String password = encryptor.encrypt("aaa");
System.out.println(url+"----------------");
System.out.println(name+"----------------");
System.out.println(password+"----------------");
// Assert.assertTrue(name.length() > 0);
//Assert.assertTrue(password.length() > 0);
}
}
application.properties配置文件 对需要加密的地方进行加密(ENC(XXX... ....))
#jasypt加密的密匙
jasypt.encryptor.password=EbfYkitulv73I2p0mXI50JMXoaxZTKJ7
注意上面的 ENC()是固定写法,()里面是加密后的信息。
到此,我们就实现了springboot配置文件里的敏感信息加密。是不是很简单。
完整配置文件:
server.port=9095
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://XXX:3306/vehicle?useSSL=false&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true
spring.datasource.username=
#密码进行加密
spring.datasource.password=ENC(983JIZVS3qwDbvvi2z0cFM5t5eZLJsN9A==)
# Hikari will use the above plus the following to setup connection pooling
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=15
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.pool-name=DatebookHikariCP
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.connection-test-query=SELECT 1
spring.application.name=vehicle-services
#分布式uuid,部署多个实例的时候需要修改,不能重复
app.workerId=1
app.datacenterId=1
#推送到业务平台的线程配置
app.distribute.totalThread=30
app.distribute.maxPerRoute=15
app.distribute.httpReqTimeout=5000
# Redis服务器地址 #
spring.redis.host=
# Redis服务器连接端口
spring.redis.port=26379
# Redis服务器连接密码(默认为空)fxft2018
spring.redis.password=ENC(kCR3uPi/Bd9dZnoxyBTHjhND)
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.max-total=5000
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.max-idle=200
# 连接池中的最小空闲连接
spring.redis.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
# 连接的库序号
spring.redis.select=3
#mapper配置
mybatis.mapper-locations=classpath:mapper/**/*.xml
mybatis.configuration.map-underscore-to-camel-case=true
#jasypt加密的密匙
jasypt.encryptor.password=EbfYkitulv73I2p0mXI50JMXoaxZTKJ7