欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解如何利用jasypt實(shí)現(xiàn)配置文件加密

 更新時(shí)間:2022年07月05日 14:00:02   作者:步爾斯特  
Jasypt?(Java?Simplified?Encryption)?是一個(gè)?java?庫,它允許開發(fā)人員以最小的成本將基本的加密功能添加到項(xiàng)目中,而無需深入了解密碼學(xué)的工作原理。本文將利用jasypt實(shí)現(xiàn)配置文件加密,感興趣的可以學(xué)習(xí)一下

Jasypt (Java Simplified Encryption) 是一個(gè) java 庫,它允許開發(fā)人員以最小的成本將基本的加密功能添加到項(xiàng)目中,而無需深入了解密碼學(xué)的工作原理。

引入依賴

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

生效

如果是為spring boot項(xiàng)目的配置文件加密,則無需額外配置,啟動(dòng)類上的@SpringBootApplication自動(dòng)會(huì)將其注入程序,使其生效。

作用域

指定配置文件的作用域,其他配置文件不受jasypt影響

@Configuration
@EncryptablePropertySources({
    @EncryptablePropertySource("classpath:sentinel-1.properties"),                          
    @EncryptablePropertySource("classpath:sentinel-2.properties")
    })

應(yīng)用

工具類

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

/**
 * @author issavior
 */
public class JasyptUtil {
    /**
     * org.jasypt.encryption.StringEncryptor對(duì)象
     */
    private static StringEncryptor stringEncryptor = null;

    public static StringEncryptor getInstance(String secretKey) throws Exception {
        if (secretKey == null || secretKey.trim().equals("")) {
            System.out.println("秘鑰不能為空!");
            throw new Exception("org.jasypt.encryption.StringEncryptor秘鑰不能為空!");
        }
        if (stringEncryptor == null) {
            PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
            SimpleStringPBEConfig config = new SimpleStringPBEConfig();
            // 這個(gè)秘鑰必須是我們自己定義
            config.setPassword(secretKey);
            config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
            config.setKeyObtentionIterations("1000");
            config.setPoolSize("1");
            config.setProviderName("SunJCE");
            config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
            config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
            config.setStringOutputType("base64");
            encryptor.setConfig(config);

            stringEncryptor = encryptor;
        }
        return stringEncryptor;
    }

    public static void main(final String[] args) {

        // 秘鑰字符串
        String secretKey = "123!@#";

        // 待加密的明文密碼
        String pass = "123456";
        try {
            StringEncryptor stringEncryptor = JasyptUtil.getInstance(secretKey);
            String encryptPass = stringEncryptor.encrypt(pass);
            System.out.println("【" + pass + "】被加密成【" + encryptPass + "】");

            String clearPass = stringEncryptor.decrypt(encryptPass);
            System.out.println("【" + encryptPass + "】被解密成【" + clearPass + "】");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

配置

jasypt:
  encryptor:
    password: 123!@#
my:
  # 必須用ENC()包起來,這樣jasypt才能識(shí)別出來這個(gè)密碼需要解密再傳給應(yīng)用程序
  msg: ENC(PwyGulNOC9YERAC9A6zpH8Da1tn50dtgJ1XBqygkdNoBTkjmENd+F5yJJMp4sthf)

屬性一覽

進(jìn)階

其內(nèi)部屬性都是可以重寫的,包括ENC()等。

當(dāng)然了,你的鹽和密碼一定要分開存儲(chǔ),也就是說,加密后的密碼配置在配置文件中,加密的鹽可以放在啟動(dòng)參數(shù)上。

到此這篇關(guān)于詳解如何利用jasypt實(shí)現(xiàn)配置文件加密的文章就介紹到這了,更多相關(guān)jasypt配置文件加密內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳

相關(guān)文章

最新評(píng)論