配置加密
引入maven包(springboot)
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
配置文件添加配置项
指定密钥
- 添加配置
jasypt: encryptor: property: prefix: SOAR( password: xxx
- 对信息加密
public static void main(String[] args) { BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); //加密所需的salt(盐) textEncryptor.setPassword("G0CvDz7oJn6"); //要加密的数据(数据库的用户名或密码) String username = textEncryptor.encrypt("root"); String password = textEncryptor.encrypt("root123"); System.out.println("username:"+username); System.out.println("password:"+password); }
- 部署用密钥启动
该方式不需要在配置文件中指定密钥
java -jar -Djasypt.encryptor.password=abc xxx.jar
指定加密方式
- 添加配置
jasypt: encryptor: bean: customStringEncryptor property: prefix: SOAR(
-
加密bean
@Slf4j @Component("customStringEncryptor") public class CustomStringEncryptor implements StringEncryptor { @SneakyThrows @Override public String encrypt(final String message) { return XXX.encrypt(message); } @SneakyThrows @Override public String decrypt(final String encryptedMessage) { return XXX.desEncrypt(encryptedMessage).trim(); } }
加密工具可自行灵活实现。