1.依赖导入
<!-- 加密 --> <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot</artifactId> <version>2.0.0</version> </dependency>
2.启动类开启配置加密
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) @EnableEncryptableProperties # 开启配置文件加密 public class YangBuYiApplication { public static void main(String[] args) { SpringApplication.run(YangBuYiApplication.class, args); } }
3.创建测试类生成加密后的密钥
public static void main(String[] args) { BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); //加密所需的salt(盐) textEncryptor.setPassword("你的秘钥"); //要加密的数据(数据库的用户名或密码) String username = textEncryptor.encrypt("xxxxxx"); String password = textEncryptor.encrypt("xxxxxx"); System.out.println("username:"+username); System.out.println("password:"+password); }
生成完毕
username:yUPkUPMTb/UX/qKfP0xCQw== password:NwwqNtDKTBHOXDTg+5Y8Bg==
前往配置文件找到需要加密的位置使用ENC(生成的加密字符)
username: ENC(4ZE1F82knvMl3PD/BGr5JQ==) password: ENC(KV7zZatN5GLUY9sIobCAUQ==)
4.让解密系统知道你设置的秘钥是什么
方法一
# 加密密钥 jasypt: encryptor: password: 你的秘钥
方法二 -Djasypt.encryptor.password=加密密钥