SpringBoot整合jasypt實現(xiàn)配置文件加密

一. 概述

在企業(yè)項目,配置文件通常保存著各種明文密碼, 這種直接明文的做法可能就有點草率,不夠安全. 本DEMO整合jasypt簡單實現(xiàn)配置文件屬性加密, 具體詳細可以看jasypt官網(wǎng)

二. SpringBootDemo

2.1 依賴

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

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

2.2 獲取屬性加密值

HelloWorld加密

@RunWith(SpringRunner.class)
@SpringBootTest
public class PasswordTest {
    @Autowired
    private StringEncryptor encryptor;

    /**
     * 生成加密密碼
     */
    @Test
    public void testGeneratePassword() {
        // 你的郵箱密碼
        String password = "HelloWorld";
        // 加密后的密碼(注意:配置上去的時候需要加 ENC(加密密碼))
        String encryptPassword = encryptor.encrypt(password);
        String decryptPassword = encryptor.decrypt(encryptPassword);

        System.out.println("password = " + password);
        System.out.println("encryptPassword = " + encryptPassword);
        System.out.println("decryptPassword = " + decryptPassword);
    }
}

運行結(jié)果

password = HelloWorld
encryptPassword = nWwBNED1eKH2sZouKA0W71S6U5lggLI8
decryptPassword = HelloWorld

2.3 yml配置

demo:
  # 格式 ENC(加密密碼)
  value: ENC(nWwBNED1eKH2sZouKA0W71S6U5lggLI8)
# 為 jasypt 配置解密秘鑰
jasypt:
  encryptor:
    password: wpr

2.4 測試

    @Value("${demo.value}")
    public String password;

    @Test
    public void testValue(){
        System.out.println("密碼值為:"+password);
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容