使用jasypt加密Spring Boot應用中的敏感配置


jasypt 簡介

詳細信息直接看github文檔即可,這里僅簡單羅列一丟丟信息。

Jasypt是java中流行的開源加解密工具包。Jasypt為Spring Boot應用提供property sources的加密支持,可以加密的數據有:

  • system property

  • environment property

  • command line argument

  • application.properties

  • yaml properties

  • other custom property sources

哪些是敏感信息

由於很多應用使用 配置文件 (eg:properties、yml) 來存儲配置信息,配置中經常會涉及到許多敏感信息。

舉幾個小例子:

普通應用密碼信息,如:DB、Rabbit、Redis等
特殊密碼信息,如:Spring Cloud Config需要配置Git等VCS密碼信息
第三方通訊憑證信息,如:調用第三方接口發送短信的通訊憑證信息
由於各業務場景不同,因此敏感信息的定義也不同。

如何使用

1、springboot項目中,引入依賴

<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.2</version>
</dependency>

2、在application.yml中配置jasypt.encryptor.password=yoursalt ,例如

jasypt:
  encryptor:
    #加解密的根密碼
    password: turing 

默認使用 PBEWithMD5AndDES 加密算法,只有 jasypt.encryptor.password 是必要配置。

如果不想將jasypt的加密鹽值直接配置在配置文件中,則也可改為在啟動jar命令行中指定程序參數或jvm參數
#啟動springboot的jar包,指定程序參數
java -jar xxx.jar --jasypt.encryptor.password=turing

#啟動springboot的jar包,指定JVM參數
java  -Djasypt.encryptor.password=turing -jar xxx.jar 

3、命令行加解密

#直接使用命令對明文進行加密
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123456" password=turing algorithm=PBEWithMD5AndDES
#直接使用命令對密文進行解密
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="KCQCruXa2BN+StcKVPlkAg==" password=turing algorithm=PBEWithMD5AndDES

4、 在項目業務代碼中使用jasypt

@autowired 注入StringEncryptor  bean

package com.tingcream.springmybatis2;
 
import org.jasypt.encryption.StringEncryptor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
 
 
@RunWith(SpringRunner.class)
@SpringBootTest
public class JasyptTest {
 
@Autowired
private StringEncryptor stringEncryptor;
 
 
@Test
public void encrypt() {
String text ="123456";
String s1= stringEncryptor.encrypt(text);
System.out.println("加密后得到密文為:"+s1);
String s2= stringEncryptor.decrypt(s1);
System.out.println("解密后得到原文為:"+s2);
 
/*
 * 注意: 每次使用stringEncryptor加密同樣的明文,所得到的密碼都可能不一樣
 * 加密后得到密文為:LTsP+Ixe26vAZYnVd28Lag==
解密后得到原文為:123456
加密后得到密文為:EeTv7ggGS3SVEICVd1TVdA==
解密后得到原文為:123456
加密后得到密文為:L0gcXucNVewXZSsFNrmVhw==
解密后得到原文為:123456
 * 
 * */
 
}  
}

這個是依賴spring容器來進行加密。
不依賴spring容器直接使用JAVA方法:

@Test
public void testEnvironmentProperties() {
  //對應配置文件中對應的根密碼
  System.setProperty("jasypt.encryptor.password", "jiaxing");
  StringEncryptor stringEncryptor = new DefaultLazyEncryptor(new StandardEnvironment();
  //加密方法
  System.out.println(stringEncryptor.encrypt("jiaxing"));
  //解密方法    
  System.out.println(stringEncryptor.decryptstringEncryptor.encrypt("jiaxing"))
}

5、application.yml中配置加密后的屬性

將加密后的屬性值配置在配置文件中即可,ENC 是約定的關鍵字,在啟動時會解析所有 PropertySource 中的加密屬性。

spring: 
  datasource: 
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      username: root
      password: ENC(KCQCruXa2BN+StcKVPlkAg==)  #注意jasypt的密文需要使用ENC括起來

附上工具類

package com.example.code.bot_monomer.utils;

import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEByteEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

public class JasyptUtils {


    public static void main(String[] args) {
        String pw = JasyptUtils.encryptPwd("123abc","aaaaafddddddddddfdfdfdfdfdfddfdfd");
        System.out.println(pw);
        String mw = JasyptUtils.decyptPwd("123abc","XtCQ+/Totbxh66iktPkWg/O1Orj7fYKZBq1yx4mh0C4rDESUcwaC7moofKX9cify");
        System.out.println(mw);
    }

    /**
     * Jasypt生成加密結果
     *
     * @param password 配置文件中設定的加密密碼 jasypt.encryptor.password
     * @param value    待加密值
     * @return
     */
    public static String encryptPwd(String password, String value) {
        PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor();
        encryptOr.setConfig(cryptOr(password));
        String result = encryptOr.encrypt(value);
        return result;
    }

    /**
     * 解密
     *
     * @param password 配置文件中設定的加密密碼 jasypt.encryptor.password
     * @param value    待解密密文
     * @return
     */
    public static String decyptPwd(String password, String value) {
        PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor();
        encryptOr.setConfig(cryptOr(password));
        String result = encryptOr.decrypt(value);
        return result;
    }

    /**
     * @param password salt
     * @return
     */
    public static SimpleStringPBEConfig cryptOr(String password) {
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(password);
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setKeyObtentionIterations(1000);
        config.setPoolSize(1);
        // according Jasypt documentation, if no provider name is explicitly set, the default JVM provider will be used.
        config.setProviderName(null);
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        return config;
    }
}
View Code

 

 

 

參考文章:

https://blog.csdn.net/jasnet_u/article/details/101485385

https://www.jianshu.com/p/5a120bd360bc


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM