Spring Boot 配置 - 配置信息加密

img

▶ Spring Boot 依賴與配置

Maven 依賴git

<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 算法加密算法進行以下配置:github

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 還支持非對稱加密、自定義加密器等等功能,更多信息:spring

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

▶ 配置參數加解密

添加依賴併發

<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

© 著做權歸做者全部,轉載或內容合做請聯繫做者dom

img

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

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

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

Spring Cloud Gateway - 快速開始

APM工具尋找了一圈,發現SkyWalking纔是個人真愛

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

將 HTML 轉化爲 PDF新姿式

Java 使用 UnixSocket 調用 Docker API

Fastjson致命缺陷

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

使用 Thymeleaf 動態渲染 HTML

原文連接:

本文由博客一文多發平臺 OpenWrite 發佈!

相關文章
相關標籤/搜索