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

如何給yml配置文件的密碼加密(SpringBoot)

 更新時間:2022年10月27日 16:05:55   作者:_幻化成風_  
這篇文章主要介紹了如何給yml配置文件的密碼加密(SpringBoot),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

最近在忙著解決規(guī)約掃描的問題,其一就是這個明文密碼必須加密的問題,一般是數(shù)據(jù)庫的配置。首先我用的是默認的PBEWithMD5AndDES默認的MD5加密方式,

弄好之后有要求使用AES_256/SM2/SM4等高級的算法加密,于是后來又升級了jar包使用默認的PBEWITHHMACSHA512ANDAES_256.

JDK版本-1.8

1.低版本2.x

先記錄低版本的加密方式-----PBEWithMD5AndDES

1)引入jar包

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

如上一個啟動類,在聯(lián)網(wǎng)的情況下就能自動下載其余加密需要的jar包。

如果是在內(nèi)網(wǎng)的情況下,可以手動在項目的lib目錄引入所需jar包,如下:

<dependency>
    <groupId>org.jasypt</groupId>
    <artifactId>jasypt</artifactId>
    <version>1.9.2</version>
</dependency>
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot</artifactId>
    <version>2.1.0</version>
</dependency>

2)生成密碼

@Test
    public void testEncrypt() {
        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
 
        config.setAlgorithm("PBEWithMD5AndDES");          // 加密的算法,這個算法是默認的
        config.setPassword("Angel");                        // 加密的密鑰,隨便自己填寫,很重要千萬不要告訴別人
        standardPBEStringEncryptor.setConfig(config);
        String plainText = "123456";         //自己的密碼
        String encryptedText = standardPBEStringEncryptor.encrypt(plainText);
        System.out.println(encryptedText);
    }

如圖,數(shù)據(jù)庫密碼為123456,密鑰為Angel,加密算法也如上;

運行如圖:4o+eS8OaWQ7HcjVgrkoX0A==

3)測下解密

@Test
    public void testDe() {
        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
 
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setPassword("Angel");
        standardPBEStringEncryptor.setConfig(config);
        String encryptedText = "4o+eS8OaWQ7HcjVgrkoX0A==";   //加密后的密碼
        String plainText = standardPBEStringEncryptor.decrypt(encryptedText);
        System.out.println(plainText);
    }

運行如圖:

4)yml配置

記好加密好的密碼,在yml文件里將數(shù)據(jù)源那里用ENC套上,如下:

spring:
 datasource:
  username: root
  password: ENC(4o+eS8OaWQ7HcjVgrkoX0A==)
  url: jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
  driver-class-name: com.mysql.cj.jdbc.Driver
 
jasypt:
 encryptor:
  password: Angel
  algorithm: PBEWithMD5AndDES

5)測測登錄

題外話,啟動類上無需開啟加密注解(@EnableEncryptableProperties)

2.高版本 3.x

1)引入jar包

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

與前面 一致,沒有外網(wǎng)的條件,引入如下jar包

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

2)生成密碼

這里用的是一個工具類,CV大法。

package com.example.demo.utils;
 
/**
 * @author 楊帥帥
 * @time 2021/12/5 - 1:00
 */
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
 
public class JasypUtil {
 
    private static final String PBEWITHHMACSHA512ANDAES_256 = "PBEWITHHMACSHA512ANDAES_256";
 
    /**
     * @Description: Jasyp 加密(PBEWITHHMACSHA512ANDAES_256)
     * @Author:      Rambo
     * @CreateDate:  2020/7/25 14:34
     * @UpdateUser:  Rambo
     * @UpdateDate:  2020/7/25 14:34
     * @param		 plainText  待加密的原文
     * @param		 factor     加密秘鑰
     * @return       java.lang.String
     * @Version:     1.0.0
     */
    public static String encryptWithSHA512(String plainText, String factor) {
        // 1. 創(chuàng)建加解密工具實例
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        // 2. 加解密配置
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(factor);
        config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);
        // 為減少配置文件的書寫,以下都是 Jasyp 3.x 版本,配置文件默認配置
        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);
        // 3. 加密
        return encryptor.encrypt(plainText);
    }
 
    /**
     * @Description: Jaspy解密(PBEWITHHMACSHA512ANDAES_256)
     * @Author:      Rambo
     * @CreateDate:  2020/7/25 14:40
     * @UpdateUser:  Rambo
     * @UpdateDate:  2020/7/25 14:40
     * @param		 encryptedText  待解密密文
     * @param		 factor         解密秘鑰
     * @return       java.lang.String
     * @Version:     1.0.0
     */
    public static String decryptWithSHA512(String encryptedText, String factor) {
        // 1. 創(chuàng)建加解密工具實例
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        // 2. 加解密配置
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(factor);
        config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);
        // 為減少配置文件的書寫,以下都是 Jasyp 3.x 版本,配置文件默認配置
        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);
        // 3. 解密
        return encryptor.decrypt(encryptedText);
    }
    
    public static void main(String[] args) {
        String factor = "Angel";
        String plainText = "123456";
 
        String encryptWithSHA512Str = encryptWithSHA512(plainText, factor);
        String decryptWithSHA512Str = decryptWithSHA512(encryptWithSHA512Str, factor);
        System.out.println("采用AES256加密前原文密文:" + encryptWithSHA512Str);
        System.out.println("采用AES256解密后密文原文:" + decryptWithSHA512Str);
    }
}

執(zhí)行這個main方法,來,見證奇跡的時刻

Exception in thread "main" org.jasypt.exceptions.EncryptionOperationNotPossibleException: Encryption raised an exception. A possible cause is you are using strong encryption algorithms and you have not installed the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files in this Java Virtual Machine
    at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.handleInvalidKeyException(StandardPBEByteEncryptor.java:1207)
    at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.encrypt(StandardPBEByteEncryptor.java:996)
    at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.encrypt(StandardPBEStringEncryptor.java:655)
    at org.jasypt.encryption.pbe.PooledPBEStringEncryptor.encrypt(PooledPBEStringEncryptor.java:465)
    at com.example.demo.utils.JasypUtil.encryptWithSHA512(JasypUtil.java:41)
    at com.example.demo.utils.JasypUtil.main(JasypUtil.java:78)

這個錯,很有意思,在加密的時候,手動配置了JCE的方法來支持加密的程序,然而jasypt3.x版本的對于jdk8自帶的jce的包不兼容,需要升級一下,所以到網(wǎng)上下載jdk8對應的JCE包jce_policy-8。

下載后解壓有兩個jar包,如圖:

到該目錄下替換即可:C:\Program Files\Java\jdk1.8.0_25\jre\lib\security

 然后運行main方法:8jLUdq0Fr7UhJGNwK/Nc6i6/WV4+UBpvtfBLDh4e3jZMJZAhPqfZdGlpFEUk24UZ

成功。

3)yml配置

spring:
 datasource:
  username: root
  password: ENC(8jLUdq0Fr7UhJGNwK/Nc6i6/WV4+UBpvtfBLDh4e3jZMJZAhPqfZdGlpFEUk24UZ)
  url: jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
  driver-class-name: com.mysql.cj.jdbc.Driver
 
jasypt:
 encryptor:
  password: Angel
  algorithm: PBEWITHHMACSHA512ANDAES_256

 改個登錄密碼:

很順利,不妨再來驗證加密算法,如圖我如果改為

jasypt:
 encryptor:
  password: Angel
  algorithm: PBEWithMD5AndDES

運行項目則報錯,圖略。

yml文件里面需要配置密鑰以及算法,才能正確解密訪問數(shù)據(jù)庫,那你看這個Angel密鑰是用password來修飾的,是否會被外行的人認作密碼呢?

答案是肯定的,所以呀需要使用寫小手段,把密鑰也寫成ENC()這種格式的就OK了。

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

相關文章

  • java 重載(overload)與重寫(override)詳解及實例

    java 重載(overload)與重寫(override)詳解及實例

    這篇文章主要介紹了java 重載(overload)與重寫(override)詳解及實例的相關資料,并附實例代碼,需要的朋友可以參考下
    2016-10-10
  • SpringBoot注冊Filter的兩種實現(xiàn)方式

    SpringBoot注冊Filter的兩種實現(xiàn)方式

    這篇文章主要介紹了SpringBoot注冊Filter的兩種實現(xiàn)方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • Java的JSON轉換庫GSON的基本使用方法示例

    Java的JSON轉換庫GSON的基本使用方法示例

    GSON是Google制作的一個可以讓Java對象與JSON互相轉換的類庫,下面我們就來看一下Java的JSON轉換庫GSON的基本使用方法示例:
    2016-06-06
  • springmvc請求轉發(fā)和重定向問題(攜帶參數(shù)和不攜帶參數(shù))

    springmvc請求轉發(fā)和重定向問題(攜帶參數(shù)和不攜帶參數(shù))

    這篇文章主要介紹了springmvc請求轉發(fā)和重定向問題(攜帶參數(shù)和不攜帶參數(shù)),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • 詳解Spring如何解決循環(huán)引用的問題

    詳解Spring如何解決循環(huán)引用的問題

    在Spring框架中,當兩個或多個Bean之間存在相互依賴關系時,可能會導致循環(huán)引用的問題,循環(huán)引用指的是兩個或多個Bean之間互相依賴,形成一個循環(huán)鏈,本文將和大家一起探討Spring如何解決循環(huán)引用的問題,感興趣的小伙伴跟著小編一起來看看吧
    2023-08-08
  • spring在service層的方法報錯事務不會回滾的解決

    spring在service層的方法報錯事務不會回滾的解決

    這篇文章主要介紹了spring在service層的方法報錯事務不會回滾的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 通過Spring Boot + Mybatis + Redis快速搭建現(xiàn)代化Web項目

    通過Spring Boot + Mybatis + Redis快速搭建現(xiàn)代化Web項目

    本篇文章介紹了如何通過Spring Boot、Mybatis以及Redis快速搭建一個現(xiàn)代化的Web項目,并且同時介紹了如何在Spring Boot下優(yōu)雅地書寫單元測試來保證我們的代碼質量。具體內(nèi)容詳情大家通過本文學習下吧
    2017-12-12
  • 基于jenkins實現(xiàn)發(fā)布node.js項目

    基于jenkins實現(xiàn)發(fā)布node.js項目

    這篇文章主要介紹了基于jenkins實現(xiàn)發(fā)布node.js項目,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • SpringBoot利用MDC機制過濾單次請求的所有日志

    SpringBoot利用MDC機制過濾單次請求的所有日志

    在服務出現(xiàn)故障時,我們經(jīng)常需要獲取一次請求流程里的所有日志進行定位 ,如何將一次數(shù)據(jù)上報請求中包含的所有業(yè)務日志快速過濾出來,就是本文要介紹的,需要的朋友可以參考下
    2024-04-04
  • SpringBoot使用Spring Test進行集成測試的流程步驟

    SpringBoot使用Spring Test進行集成測試的流程步驟

    Spring Test 是 Spring Framework 提供的一個測試框架,它可以幫助我們進行集成測試,在本文中,我們將介紹如何使用 Spring Test 進行集成測試,需要的朋友可以參考下
    2023-06-06

最新評論