1.pom.xml
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>1.14</version>
</dependency>
2.启动类加上
@EnableEncryptableProperties
3.application.properties
#配置文件加密
jasypt.encryptor.password=123456
123456是自己随意设置的加密密钥
4.如何生成加密过的字符?
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Test
public void encryptPwd() {
BasicTextEncryptor encryptor = new BasicTextEncryptor();
encryptor.setPassword("123456");
String encrypted = encryptor.encrypt("root");
System.out.println(encrypted);
}
}
123456是上面配置过的加密钥匙,root是你想加密的东西
5.对application.properties进行加密
ENC(密文)
spring.datasource.username=${db.username}
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
db.username=ENC(3IZcZWAdAEE0KUmmHWtYRA==)
6.验证
在controller中
@Value("${spring.datasource.username}")
private String name;
@RequestMapping(value = "jj")
@ResponseBody
public String jj(){
return name;
}
浏览器访问