背景:测试软件对服务环境以及服务本身进行检测时,会检测配置文件的敏感信息是否明文方式存储,对此可以通过一下方式进行简单加密处理。
以下推荐一种相对没有难度的方式:jasypt
maven坐标如下:
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot</artifactId>
<version>2.0.0</version>
</dependency>
-
Application.java上增加注解@EnableEncryptableProperties,特别:如果maven 引入坐标是jasypt-spring-boot-starter,则这一步可以省略;
-
配置文件中增加:jasypt.encryptor.password = missyou,这是加密的秘钥;
-
需要明文处理的配置项,使用ENC(密文)的方式,例如ENC(9vvbPPEwXGU);
密文生成方式1:
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123456" password="missyou" algorithm=PBEWithMD5AndDES
input:明文内容
password:配置项中添加的秘钥
algorithm:加密方式
密文生成方式2:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class JasyptTest {
@Autowired
StringEncryptor encryptor;
//加密
@Test
public void getPass() {
String name = encryptor.encrypt("hello");
System.out.println("加密结果:" + name);//解密
}
@Test
public void passDecrypt() {
String username = encryptor.decrypt("ve0JfMYQr11A4qpF4hJfYA==");
System.out.println("解密结果:" + username);
}
}