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); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律