配置文件脱敏
配置文件脫敏
使用原因:在項目中,經常需要在配置文件里配置一些敏感信息,比如數據庫用戶名和密碼,redis、mq的連接信息等。如果直接寫明文,很容易造成密碼泄露等安全問題。
jasypt簡介
Jasypt是一個Java庫,它允許開發者以最小的改動為項目添加基本的加密功能,而且不需要對密碼學的工作原理有深刻的了解。
Jasypt特點
可以加密所有的Spring環境配置信息,比如系統變量、環境變量、命令行變量、applicationproperties,yaml配置等
jar包
<!--配置文件加密--><!--方式一: 直接引入jasypt-spring-boot-starter--><dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>2.0.0</version></dependency><!-- 方式二: 需在啟動類添加@EnableEncryptableProperties --><dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot</artifactId><version>3.0.3</version></dependency>需要注意版本對應
| 2.1.0 | 2.0.3.RELEASE 2.2.6.RELEASE |
| 2.0.0 | 2.0.0.RELEASE 2.2.6.RELEASE |
| 1.18 | 1.5.10.RELEASE 2.2.6.RELEASE |
| 1.12 | 1.5.1.RELEASE 2.2.6.RELEASE |
需要注加解密的類型一致,如:
2.0.0;2.1.0;2.1.1;2.1.2版本默認加密方式為:PBEWithMD5AndDES 3.0.3版本默認加密方式為:PBEWITHHMACSHA512ANDAES_256 當引入3.0.3依賴,卻沒有添加相關jasypt加解密配置,而密文通過【PBEWithMD5AndDES】來加密,啟動會報錯。 需要切換為【PBEWITHHMACSHA512ANDAES_256】方式進行。配置文件中密碼加解密工具類
import org.jasypt.encryption.StringEncryptor; import org.jasypt.encryption.pbe.PooledPBEStringEncryptor; import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig; import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig; import org.jasypt.util.text.BasicTextEncryptor; import org.springframework.beans.factory.annotation.Autowired;/*** 配置文件中密碼加解密工具類*/ public class JasypUtil {public static void main(String[] args) { // /*加密方式為: PBEWithMD5AndDES*/ // BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); // //加密所需的salt(鹽) // textEncryptor.setPassword("XVo2eH1oYD+Mc5hwZUdp6w=="); // //要加密的數據(數據庫的用戶名或密碼) // String passwordEncrypt = textEncryptor.encrypt("admin_123"); // //解密 // String passwordDencrypt = textEncryptor.decrypt(passwordEncrypt); // System.out.println("解密后的password明文" + passwordDencrypt); // System.out.println("password密文:" + passwordEncrypt);// 加密方式:PBEWithMD5AndDESString factor = encryptWithMD5("123456", "123456");System.out.println("加密后的密鑰: "+factor);String password = encryptWithMD5("admin_123","1x6rd4Yu7IVQ1/O64gggqw==");System.out.println("加密后的密碼: "+password);//加密方式:PBEWITHHMACSHA512ANDAES_256 // String factor = encryptWithSHA512("123456", "123456"); // System.out.println("加密后的密鑰: "+factor); // String password = encryptWithSHA512("admin_123","cSkz30kwdEEKkThXtpxGhGse9HeNtofAzvzAFgVK58cXPOVeOX7Dm2tZ9IqRTyFL"); // System.out.println("加密后的密碼: "+password);}private static final String PBEWITHMD5ANDDES = "PBEWithMD5AndDES";private static final String PBEWITHHMACSHA512ANDAES_256 = "PBEWITHHMACSHA512ANDAES_256";/*** @param plainText 待加密的原文* @param factor 加密秘鑰* @return java.lang.String* @Description: Jasyp加密(PBEWithMD5AndDES)* @Version: 1.0.0*/ public static String encryptWithMD5(String plainText, String factor) {// 1. 創建加解密工具實例StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();// 2. 加解密配置EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();config.setAlgorithm(PBEWITHMD5ANDDES);config.setPassword(factor);encryptor.setConfig(config);// 3. 加密return encryptor.encrypt(plainText);}/*** @param encryptedText 待解密密文* @param factor 解密秘鑰* @return java.lang.String* @Description: Jaspy解密(PBEWithMD5AndDES)* @Version: 1.0.0*/public static String decryptWithMD5(String encryptedText, String factor) {// 1. 創建加解密工具實例StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();// 2. 加解密配置EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();config.setAlgorithm(PBEWITHMD5ANDDES);config.setPassword(factor);encryptor.setConfig(config);// 3. 解密return encryptor.decrypt(encryptedText);}/*** @param plainText 待加密的原文* @param factor 加密秘鑰* @return java.lang.String* @Description: Jasyp 加密(PBEWITHHMACSHA512ANDAES_256)* @Version: 2.1.1;2.1.1;3.0.3*/public static String encryptWithSHA512(String plainText, String factor) {// 1. 創建加解密工具實例PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();// 2. 加解密配置SimpleStringPBEConfig config = new SimpleStringPBEConfig();config.setPassword(factor);config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);// 為減少配置文件的書寫,以下都是 Jasyp 3.x 版本,配置文件默認配置config.setKeyObtentionIterations("1000");config.setPoolSize("1");config.setProviderName("SunJCE");config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");config.setStringOutputType("base64");encryptor.setConfig(config);// 3. 加密return encryptor.encrypt(plainText);}/*** @param encryptedText 待解密密文* @param factor 解密秘鑰* @return java.lang.String* @Description: Jaspy解密(PBEWITHHMACSHA512ANDAES_256)* @Version: 2.1.1;2.1.1;3.0.3*/public static String decryptWithSHA512(String encryptedText, String factor) {// 1. 創建加解密工具實例PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();// 2. 加解密配置SimpleStringPBEConfig config = new SimpleStringPBEConfig();config.setPassword(factor);config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);// 為減少配置文件的書寫,以下都是 Jasyp 3.x 版本,配置文件默認配置config.setKeyObtentionIterations("1000");config.setPoolSize("1");config.setProviderName("SunJCE");config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");config.setStringOutputType("base64");encryptor.setConfig(config);// 3. 解密return encryptor.decrypt(encryptedText);}}配置文件
datasource:url: jdbc:postgresql://xxip:host/publicdb?allowMultiQueries=trueusername: postgrespassword: PASSWORD(jzABo0VIZC0/vnIHIO4yuzeqiiNSlOMm) driver-class-name: org.postgresql.Driver jasypt:encryptor:#密鑰password: 1x6rd4Yu7IVQ1/O64gggqw== algorithm: PBEWITHHMACSHA512ANDAES_256#指定前綴、后綴property:prefix: 'PASSWORD('suffix: ')'關于密鑰放置位置:
1.配置文件中(若使用明文配置,仍有安全問題,可加密后放置于配置文件中)
jasypt:encryptor:password: 1x6rd4Yu7IVQ1/O64gggqw==2.啟動類里:不易更改密鑰
/*配置加解密密鑰,與配置文件的密文分開方*/System.setProperty("jasypt.encryptor.password","XVo2eH1oYD+Mc5hwZUdp6w==");3.放置于啟動配置中
-Djasypt.encryptor.password=123456Edit Configurations->VM options
總結
- 上一篇: ios开发教程入门到精通
- 下一篇: android清理缓存实现,androi