Springboot: jasypt
<!-- https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter --> <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.4</version> </dependency>
jasypt: encryptor: algorithm: PBEWITHHMACSHA512ANDAES_256 # password: jasypt 不在配置文件中配置密钥 property: database: host: ENC(密文) spring: datasource: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://${property.database.host}:3306/thymeleaf?characterEncoding=UTF-8 driver-class-name: com.mysql.cj.jdbc.Driver username: ENC(87wMRLKpito1Cyi4/xqTE1xZ2g1REtU8dyULdXz8d6Mm0AA8TmSQvQ7PToqnk+Xv) password: ENC(87wMRLKpito1Cyi4/xqTE1xZ2g1REtU8dyULdXz8d6Mm0AA8TmSQvQ7PToqnk+Xv)
运行时指定参数:
--jasypt.encryptor.password=jasypt # program arguments
-Djasypt.encryptor.password=jasypt # JVM arguments
import org.jasypt.encryption.StringEncryptor; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest public class TestJasypt{ private final StringEncryptor stringEncryptor; @Autowired public TestJasypt(StringEncryptor stringEncryptor){ this.stringEncryptor = stringEncryptor; } @Test public void test(){ for(int i = 0; i < 10; i++){ String encrypt = stringEncryptor.encrypt("root"); System.out.println("encrypt = " + encrypt); // 每次生成的密文不相同 String decrypt = stringEncryptor.decrypt(encrypt); System.out.println("decrypt = " + decrypt); } } }
import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import us.transcode.thymeleaf.dao.EmployeeDao; import us.transcode.thymeleaf.entity.Employee; import java.util.List; @SpringBootTest public class TestDao{ private final EmployeeDao employeeDao; @Autowired public TestDao(EmployeeDao employeeDao){ this.employeeDao = employeeDao; } @Test public void b(){ List<Employee> employees = employeeDao.selectAll(); System.out.println("employees = " + employees); } }