- 导入依赖
<!-- 加密依赖 -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
- 加密
package cn.linjk.ehome;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig;
import org.junit.Test;
public class JasyptTest {
@Test
public void testEncrypt() throws Exception {
StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
EnvironmentPBEConfig config = new EnvironmentPBEConfig();
config.setAlgorithm("PBEWithMD5AndDES"); // 加密的算法,这个算法是默认的
config.setPassword("12345"); // 加密的密钥
standardPBEStringEncryptor.setConfig(config);
String plainText = "hello";
String encryptedText = standardPBEStringEncryptor.encrypt(plainText);
System.out.println(encryptedText);
}
}
- 在application.yml文件中配置
//之前的盐
jasypt:
encryptor:
password: 12345
algorithm: PBEWithMD5AndDES
ivGeneratorClassname: org.jasypt.salt.RandomIVGenerator
//ENC(加密的密文)
spring:
datasource:
username:ENC(sBdk7fXxdnCZMzPjGkZr0g==)
password:ENC(sBdk7fXxdnCZMzPjGkZr0g==)
- 启动类加入注解
/配置数据库加密注解
@EnableEncryptableProperties
网友评论