1. 程式人生 > 程式設計 >Spring Boot 配置 - 配置資訊加密

Spring Boot 配置 - 配置資訊加密

img

▶ Spring Boot 依賴與配置

Maven 依賴

   <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>
複製程式碼

▶ 使用說明

假設有配置項 com.anoyi.custom.name=anoyi 不能明文顯示,則可以使用 jasyptPBEWithMD5AndDES 演演算法加密演演算法進行如下配置:

com.anoyi.custom.name=ENC(TqrnYZn55aFVwnSo2TrbFA==)
jasypt.encryptor.password=anoyi複製程式碼

  • jasypt.encryptor.password 為自定義值,用此密碼加密的明文,需要用此密碼解密密文
  • ENC(...)jasypt 提供的加密標識,Spring Boot 服務啟動時,載入各種 properties 時會依據此標識判斷是否解密賦值,可自定義
  • TqrnYZn55aFVwnSo2TrbFA== 為明文字串 anoyi 通過密碼 anoyi 加密後得到的值,此值不唯一,即同一明文通過同一密碼加密會得到不同的值

▶ 配置說明

基於 Password 的加密配置

引數
必填
預設值
jasypt.encryptor.password
True
-
jasypt.encryptor.algorithm
False PBEWithMD5AndDES
jasypt.encryptor.keyObtentionIterations False 1000
jasypt.encryptor.poolSize
False 1
jasypt.encryptor.providerName
False SunJCE
jasypt.encryptor.providerClassName
False null
jasypt.encryptor.saltGeneratorClassname False org.jasypt.salt.RandomSaltGenerator
jasypt.encryptor.ivGeneratorClassname
False org.jasypt.salt.NoOpIVGenerator
jasypt.encryptor.stringOutputType
False base64
jasypt.encryptor.proxyPropertySources
False false

最新版的 jasypt 還支援非對稱加密、自定義加密器等等功能,更多資訊:

MORE :https://github.com/ulisesbocchio/jasypt-spring-boot

▶ 配置引數加解密

新增依賴

    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>複製程式碼

示例加解密字串 anoyi

@RunWith(SpringRunner.class)
@SpringBootTest
public class EncryptTest {

    @Autowired
    private StringEncryptor jasyptStringEncryptor;

    @Test
    public void encrypt() {
        String encryptStr = jasyptStringEncryptor.encrypt("anoyi");
        System.out.println(encryptStr);
    }

    @Test
    public void decrypt() {
        String encryptStr = jasyptStringEncryptor.decrypt("TqrnYZn55aFVwnSo2TrbFA==");
        System.out.println(encryptStr);
    }

}複製程式碼

▶ Github Demo URL

  • https://github.com/ChinaSilence/spring-boot-demos/tree/master/05%20-%20config%20encrypt

© 著作權歸作者所有,轉載或內容合作請聯絡作者

img

拒絕黑盒應用-Spring Boot 應用視覺化監控

併發Bug之源有三,請睜大眼睛看清它們

史上最輕鬆入門之Spring Batch - 輕量級批處理框架實踐

Spring Cloud Gateway - 快速開始

APM工具尋找了一圈,發現SkyWalking才是我的真愛

Spring Boot 注入外部配置到應用內部的靜態變數

將 HTML 轉化為 PDF新姿勢

Java 使用 UnixSocket 呼叫 Docker API

Fastjson致命缺陷

Service Mesh - gRPC 本地聯調遠端服務

使用 Thymeleaf 動態渲染 HTML

原文連結:

本文由部落格一文多發平臺 OpenWrite 釋出!