SpringBoot使用jasypt加解密密碼

在咱們的服務中不可避免的須要使用到一些祕鑰(數據庫、redis等)git

開發和測試環境還好,但生產若是採用明文配置講會有安全問題,jasypt是一個通用的加解密庫,咱們可使用它。github

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

配置文件:redis

# 加密的密鑰
# 測試環境能夠採用在配置文件中配置
# 生產環境建議採用啓動參數的形式傳入
# 其餘配置參數參考:com.ulisesbocchio.jasyptspringboot.properties.JasyptEncryptorConfigurationProperties
jasypt.encryptor.password=you salt

# 解密獲得原始密碼
spring.datasource.password= ENC(密文)

那密文怎麼來的呢?spring

@Autowired
StringEncryptor stringEncryptor;

@Test
public void encrypt() {
    System.out.println("PWD: " + stringEncryptor.encrypt("yourpassword"));
}

jasypt默認使用StringEncryptor來進行加解密,咱們也能夠自定義本身的加解密類來替換它,具體用戶用時請百度/google。數據庫

相關文章
相關標籤/搜索