最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot配置文件中敏感數(shù)據(jù)(賬號密碼)加密方式

 更新時間:2024年04月01日 15:34:00   作者:快樂敲代碼  
這篇文章主要介紹了springboot配置文件中敏感數(shù)據(jù)(賬號密碼)加密方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

背景

原本項(xiàng)目中數(shù)據(jù)庫和中間件賬號密碼都是直接用明文配置的,畢竟這樣最方便開發(fā),平時也沒人在乎這個。

但是部署到實(shí)際項(xiàng)目中,甲方進(jìn)行安全檢查第一個就查到這個,要求我們整改,那只能整了。

網(wǎng)上找了一些資料,好像大都用Jasypt加密工具操作,Jasypt官網(wǎng)看其他博客看了下,發(fā)現(xiàn)使用起來比較方便,直接簡單記錄下,

擴(kuò)展:

直接預(yù)研敏感數(shù)據(jù)加密技術(shù),不只是對配置文件加密,對其他接口經(jīng)過后臺的敏感數(shù)據(jù)也進(jìn)行了解,包括數(shù)據(jù)加密、加密后的技術(shù)怎么進(jìn)行模糊查詢等等,會在后續(xù)博客中進(jìn)行編寫。

當(dāng)然這都是后話,下面直接操作Jasypt進(jìn)行配置文件賬號密碼加密。

實(shí)踐Jasypt

1、引入依賴

    <!-- https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter -->
    <dependency>
        <groupId>com.github.ulisesbocchio</groupId>
        <artifactId>jasypt-spring-boot-starter</artifactId>
        <version>3.0.5</version>
    </dependency>

如果項(xiàng)目中啟動類沒有 @SpringBootApplication或@EnableAutoConfiguration注解,

那么需要引入jasypt-spring-boot依賴且需要創(chuàng)建配置類

并啟用 @Configuration和@EnableEncryptableProperties,進(jìn)行聲明。

2、賬號密碼轉(zhuǎn)成加密值

創(chuàng)建一個工具類把我們的賬號密碼進(jìn)行加密

import org.jasypt.properties.PropertyValueEncryptionUtils;
import org.jasypt.util.text.BasicTextEncryptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Jasypt加密工具類
* @author ppp
* @date 2023/1/5
*/
public class JasyptUtil {
    private static final Logger logger = LoggerFactory.getLogger(JasyptUtil.class);
    /**
     * 加密使用密鑰,需要在和配置文件中的jasypt.encryptor.password保持一致	
     */
    private static final String PRIVATE_KEY = "demo";
    /**
     * BasicTextEncryptor對象初始化使用的算法就是"PBEWithMD5AndDES"
     * 點(diǎn)擊進(jìn)源碼構(gòu)造方法中就可以看到下面的設(shè)置
     * this.encryptor.setAlgorithm("PBEWithMD5AndDES");
     */
    private static BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor();

    static {
        basicTextEncryptor.setPassword(PRIVATE_KEY);
    }
    /**
     * 明文加密
     *
     * @param plaintext 明文
     * @return String
     */
    public static String encrypt(String plaintext) {
        logger.info("明文字符串為:{}", plaintext);
        String ciphertext = basicTextEncryptor.encrypt(plaintext);
        logger.info("密文字符串為:{}", ciphertext);
        return ciphertext;
    }

    /**
     * 解密
     *
     * @param ciphertext 密文
     * @return String
     */
    public static String decrypt(String ciphertext) {
        logger.info("密文字符串為:{}", ciphertext);
        ciphertext = "ENC(" + ciphertext + ")";
        if (PropertyValueEncryptionUtils.isEncryptedValue(ciphertext)) {
            String plaintext = PropertyValueEncryptionUtils.decrypt(ciphertext, basicTextEncryptor);
            logger.info("明文字符串為:{}", plaintext);
            return plaintext;
        }
        logger.error("解密失敗!");
        return "";
    }

    public static void main(String[] args) {
        String encrypt = encrypt("123456");
        String test = decrypt(encrypt);
    }
}

3、配置文件添加配置

application.yml中添加配置

jasypt:
  encryptor:
    # 指定加密密鑰,生產(chǎn)環(huán)境請放到啟動參數(shù)里面 -Djasypt.encryptor.password=加密密鑰
    password: demo
    # 指定解密算法,需要和加密時使用的算法一致
    algorithm: PBEWithMD5AndDES
    # 指定initialization vector類型
    iv-generator-classname: org.jasypt.iv.NoIvGenerator

4、替換賬號密碼

把a(bǔ)pplication.yml中需要加密的賬號密碼都換成 ENC(密文) 格式即可

啟用項(xiàng)目后他會檢測配置文件中ENC樣式的數(shù)據(jù)把里面的密文解密出來給框架使用。

總結(jié)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

太谷县| 嵊泗县| 荆州市| 崇文区| 上犹县| 微山县| 定远县| 海宁市| 宝应县| 通渭县| 潜江市| 闻喜县| 宝丰县| 介休市| 罗平县| 翁牛特旗| 浦北县| 无棣县| 望城县| 高邮市| 晋州市| 江孜县| 禄丰县| 驻马店市| 元朗区| 安乡县| 兴义市| 大宁县| 昌都县| 扬中市| 西乌珠穆沁旗| 谢通门县| 郧西县| 光山县| 台湾省| 新营市| 双辽市| 乳山市| 新密市| 分宜县| 瑞昌市|