springboot的版本是
Spring Boot :: (v2.1.5.RELEASE)
依赖
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency>
生成秘钥类
1 import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; 2 import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig; 3 4 /** 5 * 把密文放到配置文件中的时候要注意: 6 * ENC(密文) 7 */ 8 public class ConfigEncryptUtils { 9 10 /** 11 * 2.1.1 12 * Spring Boot :: (v2.1.5.RELEASE) 13 * @param args 14 */ 15 public static void main(String[] args) { 16 17 //加密工具 18 StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); 19 20 //加密配置 21 EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig(); 22 config.setAlgorithm("PBEWithMD5AndDES"); 23 24 //生成秘钥的公钥 25 config.setPassword("xiaostudy"); 26 27 //应用配置 28 encryptor.setConfig(config); 29 30 //明文密码 31 String plaintext = "123456"; 32 33 //加密 34 String ciphertext = encryptor.encrypt(plaintext); 35 36 System.out.println(plaintext + "加密后: " + ciphertext); 37 38 //解密 过程 39 String pText = encryptor.decrypt(ciphertext); 40 System.out.println(ciphertext + "解密后: " + pText); 41 } 42 }
配置文件application.yml添加
jasypt:
encryptor:
password: xiaostudy
密码:
ENC(Gs5skfuo8ovc/gSeQ45UlBF2fMWqipMksESuQPYIpao=)
注:如果发现启动报错,可能就是springboot与jasypt版本不对