我们在SpringBoot项目中的yml或者properties配置文件中都是明文的,所以使用Jasypt加密。
一、使用Jasypt进行加密
1.添加依赖
<!-- Jasypt加密 -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
之前使用1.8的版本的时候,发现项目启动会报错,但改用2.1.1的时候就正常启动了
2、将数据库的用户名和密码进行加密
@Test
void contextLoads() throws SQLException {
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
//加密所需的salt(盐),自定义
textEncryptor.setPassword("test_salt");
//要加密的数据(数据库的用户名或密码)
String password1 = textEncryptor.encrypt("user1234");
String password2 = textEncryptor.encrypt("usert_test");
System.out.println("password1:"+password1);
System.out.println("password2:"+password2);
}
运行后会得到对应的字符串,注:每次运行得到的加密字符串是不一样的
3、配置application-test.yml文件
spring:
datasource:
orcl:
username: root
password: ENC(v2hSXXiXcKNbzMgRWZ43YH31C0qBOdJt)
jdbc-url: jdbc:oracle:thin:@//localhost:1521/root
driver-class-name: oracle.jdbc.driver.OracleDriver
hisdb:
username: root_test
password: ENC(db7+wPneEQwoIDyKFi/ZxuYpaIv/ajCM)
jdbc-url: jdbc:oracle:thin:@//localhost:1521/root_test
driver-class-name: oracle.jdbc.driver.OracleDriver
########-jasypt数据库用户名、密码加密salt-########
jasypt:
encryptor:
password: test_salt #加密时的salt值
4、启动项目,即可生效
二、解密
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
//加密所需的salt(盐),自定义
textEncryptor.setPassword("test_salt");
//解密
String decrypt = textEncryptor.decrypt("v2hSXXiXcKNbzMgRWZ43YH31C0qBOdJt");
System.out.println("password:"+decrypt);
// 可解密得到 user1234
三、外置jasypt.properties配置文件
因为配置文件yml中明文填写了密钥,并且服务器上的war包中yml是可以直接查看到的,如果别人知道密钥的话,就可以通过密钥来解密获得密码,不够安全,所以,外面将密钥单独配置在项目包外,当war包启动的时候去读取就相对的更安全一点。
1、新建一个名为jasypt.properties的文件
jasypt.encryptor.password=test_salt
自己在服务器中找个位置例如:/home/test/jasypt.properties,本地位置例如:C:/leven/jasypt.properties
2、新建一个Java类LocalSettingsEnvironmentPostProcessor
package com.xxx.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
/**
* @author xxxx
* @description 读取本地配置文件jasypt加密salt值
* @date 2020/06/19
*/
public class LocalSettingsEnvironmentPostProcessor implements EnvironmentPostProcessor{
/**
* 第一个配置文件路径为部署环境路径,用于部署环境加载配置文件里的值(Linux服务器)
* 第二个配置文件路径为本地打包环境路径,解决打包时报错问题(本地路径)
* 这样配置的目的就是可以在不更改的情况下直接在本地和服务器上去运行
*/
private static final String LOCATIONS [] = {"/home/test/jasypt.properties","C:\\leven\\jasypt.properties"};
@Override
public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication springApplication) {
for(String fileLocation : LOCATIONS){
File file = new File(fileLocation);
if (file.exists()) {
MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
Properties properties = loadProperties(file);
propertySources.addFirst(new PropertiesPropertySource("Config", properties));
return ;
}
}
}
private Properties loadProperties(File f) {
FileSystemResource resource = new FileSystemResource(f);
try {
return PropertiesLoaderUtils.loadProperties(resource);
}
catch (IOException ex) {
throw new IllegalStateException("Failed to load local settings from " + f.getAbsolutePath(), ex);
}
}
}
这个java类的作用就是根据指定的配置文件路径,读取文件,添加到程序运行的环境中。
3、创建一个spring.factories的文件
在resources文件夹下创建一个文件夹名为META-INF,在里面创建一个spring.factories的文件,文件内容如下:
org.springframework.boot.env.EnvironmentPostProcessor=com.xxx.config.LocalSettingsEnvironmentPostProcessor
注意后面的类的位置不要写错了
这个文件的作用就是设置SpringBoot服务启动的时候调用我们刚才写的那个Java类。
至此,你的war包在使用tomcat启动的时候就应该可以读取制定位置的外部文件了。
本地启动和放到服务器上启动均正常