1.启动模块中引入jasypt的依赖
<!--配置加解密-->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
2.启动类中添加注解@EnableEncryptableProperties
3.如果使用xml注入参数的方式,根据官方文档描述,除引入jar包外,还需要改造启动类main方法
public static void main(String[] args) {
try {
//SpringApplication.run(HsaHuaweiConsumerApplication.class, args);
new SpringApplicationBuilder()
.environment(new StandardEncryptableEnvironment())
.sources(HsaHuaweiConsumerApplication.class).run(args);
} catch (Exception e) {
e.printStackTrace();
}
}
4.使用jar包指令或者代码的方式进行加密
jar包指令:
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="明文" password=hxmec algorithm=PBEWithMD5AndDES
input:为需加密的明文参数。
password : 是自己自定义的密钥。
algorithm : 使用的加密算法
代码方式:
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
//加密所需的salt(盐)
textEncryptor.setPassword("hxmec");
//要加密的数据(数据库的用户名或密码)
String username = textEncryptor.encrypt("dp");
String password = textEncryptor.encrypt("dp123456!");
System.out.println("username:"+username);
System.out.println("password:"+password);
5.配置中的需要加密的信息加密后,将密文用ENC()包起来
password: ENC(D5sqCB7gj4OmVYos44zyj6NHSxeG7t3T)