springboot對數據庫密碼加密


第一步:maven引jar包

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

第二步:配置文件

#jasypt加密的鹽值
jasypt.encryptor.password=erp

第三步:調用加密方法

public class JasyptTest {

    @Autowired
    static StringEncryptor encryptor;
    //加密
    public static void getPass(){
        encryptor = SpringUtil.getBean(StringEncryptor.class);
        String name = encryptor.encrypt("root");
        String password = encryptor.encrypt("firewall_new");
        System.out.println(name); //hifVDwDPW5MNYa0tAabvJQ==
        System.out.println(password);  //5mTXmB5IYPxqiSuD/045cjHEkJz2NhNi
    }
    //解密
    public static void passDecrypt(){
        encryptor = SpringUtil.getBean(StringEncryptor.class);
        String username = encryptor.decrypt("hifVDwDPW5MNYa0tAabvJQ==");
        String password = encryptor.decrypt("5mTXmB5IYPxqiSuD/045cjHEkJz2NhNi");
        System.out.println(username+"--"+password);
    }

}

控制台就會輸出加密后的秘鑰。

切記:將上面的生成的密匙如下替換,此處主要是數據庫密碼密文使用ENC進行標識

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM