ulisesbocchio對配置文件加密


GitHub地址: https://github.com/ulisesbocchio/jasypt-spring-boot

 

maven依賴

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


以下依賴需要加@EnableEncryptableProperties注解
<dependency>
     <groupId>com.github.ulisesbocchio</groupId>
     <artifactId>jasypt-spring-boot</artifactId>
     <version>2.1.0</version>
</dependency>

 

Jasypt Spring Boot 為 Spring Boot 項目中的屬性源(property sources)提供加密支持。

有三種方法可以在項目中集成 jasypt-spring-boot:

  • 如果 Spring Boot 項目中使用了 @SpringBootApplication 或者 @EnableAutoConfiguration ,在項目里添加jasypt-spring-boot-starter 依賴會自動對項目中整個屬性(包括系統屬性、環境屬性、命令行參數、application.properties, yaml)啟動加密。

  • 如果項目里沒有使用 @SpringBootApplication 或者 @EnableAutoConfiguration ,可以手動在 Configuration 類上添加注解 @EnableEncryptableProperties ,來在整個環境的屬性啟用屬性加密。

  • 如果想指定加密文件,可以使用 @EncryptablePropertySource 指定單個屬性源。

簡單點說就是 如果springboot項目mven依賴引入starter帶則不用加@EnableEncryptableProperties ,  引入<artifactId>jasypt-spring-boot-starter</artifactId>則需要加@EnableEncryptableProperties注解

 

yml配置文件

jasypt:
  encryptor:
    password: e!Jd&ljyJ^e4I5oU    #隨便定義
datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: ENC(EFwaxIyXCXYwJo0tP0jY9jD1MfXLFJNIYG8kRpTLZGskQYrnfMrazE7APFfYWUSzySGQFqk66tP9/sJMuJJrZCpm36HRiSU53vT42/K/Z8KH2rCfSFw8zhPIVltH5FPfCAP9fJbJhwwScwCwbGXegthu5RpSgaE8cRIXt8E=)
    username: ENC(kEbiaqGlnNvRy7RKHhg==)
    password: ENC(4AG83a7fTXnmN5CCXVltrN0bWHv)

 

加解密工具,單元測試

    @Resource
    StringEncryptor encryptor;

    @Test
    public void jacketEncrypt() {

        //加密
        String mongodb = encryptor.encrypt("mongodb://用戶名:密碼@IP地址:端口/庫名");
        String url = encryptor.encrypt("jdbc:mysql://IP地址:端口/庫名?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=true");
        String name = encryptor.encrypt("用戶名");
        String password = encryptor.encrypt("密碼");
        System.out.println("url 密文: " + url);
        System.out.println("mongodb 密文: " + mongodb);
        System.out.println("name 密文: " + name);
        System.out.println("password 密文: " + password);

        //解密
        String decrypt1 = encryptor.decrypt(name);
        String mongodb1 = encryptor.decrypt(mongodb);
        String decrypt2 = encryptor.decrypt(password);
        System.out.println(mongodb1 + "------------" + decrypt2);

        /*// 加密
        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
        textEncryptor.setPassword("cShoiltAiro");
        String newPassword = textEncryptor.encrypt("mongodb://lywater");
        System.out.println(newPassword);
        // 解密
        BasicTextEncryptor textEncryptor2 = new BasicTextEncryptor();
        textEncryptor2.setPassword("cShoiltAiro");
        String oldPassword = textEncryptor2.decrypt(newPassword);
        System.out.println(oldPassword);
        System.out.println("--------------------------");*/
    }

 


免責聲明!

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



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