为保障安全,在我们生产环境中,可能会要求我们对密码进行加密操作,一起看看我们该如果操作。
1、pom.xml引入依赖
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.4</version> </dependency>
2、写一个main函数将我们的密码进行加密
public class JasyptTest { private static final Logger logger = LoggerFactory.getLogger(JasyptTest.class); public static void main(String[] args) { StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); encryptor.setAlgorithm("PBEWITHHMACSHA512ANDAES_256"); encryptor.setPassword("zhi"); encryptor.setIvGenerator(new RandomIvGenerator()); // 加密 String encryptText = encryptor.encrypt("abc123"); logger.info("加密后的信息:{}", encryptText); // 解密 String decryptText = encryptor.decrypt(encryptText); logger.info("解密后的信息:{}", decryptText); } }
abc123被加密成+yFLnV9/k2Km3vpGHMDhYLVId+2tfWnpJT/r1H0vmYPPtjiInBnfmamEjUQkFD0V
3、修改数据库配置参数
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/zhi_test?useSSL=false spring.datasource.username=root spring.datasource.password=ENC(I4gM5/FpHfeA7fW1fioYgBRjZRG5nHl1WEJhDbgm28YFgpndkb1HQR+846TrAFJt) #ENC()内的信息就上面加密信息
标签:加密,springboot,spring,datasource,jasypt,encryptor,logger,String From: https://www.cnblogs.com/zhi-leaf/p/16628354.html