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

SpringBoot整合jasypt實現(xiàn)敏感信息的加密詳解

 更新時間:2022年09月29日 15:13:55   作者:不懂一休  
一般公司的核心業(yè)務(wù)代碼中,都會存在與數(shù)據(jù)庫、第三方通信的secret key等敏感信息,如果以明文的方式存儲,一旦泄露,那將會給公司帶來巨大的損失。本篇文章通過講解:Springboot集成Jasypt對項目敏感信息進行加密,提高系統(tǒng)的安全性

一、簡介

在后端開發(fā)中有很多敏感信息,比如數(shù)據(jù)庫用戶名密碼,第三方 Apikey,云服務(wù)商的 secretKey 等、如果不希望用明文在 application.yml 配置的,可以使用 jasypt 加密這些字段。

還有很重要的一點,如果你自己開源一些東西,將代碼上傳一些代碼托管平臺,肯定需要隱藏敏感信息,用 jasypt 加密可以簡化每次上傳下拉代碼修改敏感信息。

官方文檔, 官方文檔使用方法描述得很清楚適用于各種情況,下面我簡單記錄一下加密 MySQL 用戶名密碼方法

二、導(dǎo)入依賴

    <dependencies>
        <!-- jasypt 敏感數(shù)據(jù)加密,如:數(shù)據(jù)庫密碼,阿里云短信服務(wù)等-->
        <dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>3.0.4</version>
        </dependency>
        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.30</version>
            <scope>runtime</scope>
        </dependency>
        <!-- mybatis-plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.2</version>
        </dependency>
        <!-- springboot 啟動包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

三、加密字段工具類

加密 mysql 用戶名密碼,將用戶名密碼傳入 fields 數(shù)組,保存打印結(jié)果下面配置在 application.yaml 文件

public class JasyptUtil {
    private static PooledPBEStringEncryptor encryptor;
    static{
        encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword("This is a secret key"); // 秘鑰
        config.setAlgorithm("PBEWithMD5AndDES");
        //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);
    }
    public static void main(String[] args) {
    	// 需要加密的字段
        String[] fields = {"root","123456"};
        for (String field : fields) {
            System.out.println(field+"---->"+encryptorField(field));
        }
    }
    public static String encryptorField(String field){
       return  encryptor.encrypt(field);
    }
    public static String decryptField(String field){
        return encryptor.decrypt(field);
    }
}

可以看到加密過后的字符串如下

四、application.yaml 配置

數(shù)據(jù)源用戶名密碼使用上面生成加密字段

spring:
  datasource:
    username: ENC(J5GOvO1FBgtiwEytIjU/4WdzHUgbJq/W)
    password: ENC(SqCHgntWcYnthvtWGA3+GAycDle/qCBx)
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/oauth?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
# jasypt 敏感數(shù)據(jù)加密配置
# 詳細用法可參考 https://github.com/ulisesbocchio/jasypt-spring-boot
jasypt:
  encryptor:
    password: 123456 # 秘鑰,除了該項,下面都是默認值,該項建議設(shè)置 JVM 啟動參數(shù),如:-Djasypt.encryptor.password=123456
    algorithm: PBEWithMD5AndDES # 加密算法
    key-obtention-iterations: 1000 # 迭代次數(shù),值越大越復(fù)雜,相對越安全
    pool-size: 1
    provider-name: SunJCE
    salt-generator-classname: org.jasypt.salt.RandomSaltGenerator
    iv-generator-classname: org.jasypt.iv.RandomIvGenerator
    string-output-type: base64
    proxy-property-sources: false
    property:
      prefix: ENC( # 默認前綴
      suffix: ) # 默認后綴

五、啟動類測試

查詢 MySQL 的 user 表打印用戶名和密碼

package com.ye;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@SpringBootApplication
public class Test2Application {
    public static void main(String[] args) throws SQLException {
        ConfigurableApplicationContext context = SpringApplication.run(Test2Application.class, args);
        DataSource dataSource = (DataSource) context.getBean("dataSource");
        Connection connection = dataSource.getConnection();
        try {
            PreparedStatement ps = connection.prepareStatement("select * from user;");
            ResultSet rs = ps.executeQuery();
            System.out.println("<----------  user 表數(shù)據(jù)  ----------->");
            while (rs.next()) {
                String userName = rs.getString("user_name");
                String password = rs.getString("password");
                System.out.printf("userName: %s, password: %s%n", userName, password);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            connection.close();
        }
    }
}

點擊 Edit Configurations ,添加VM 啟動參數(shù) -Djasypt.encryptor.password="This is a secret key",這個密碼和加密工具類 JasyptUtil 密碼保持一致

運行成功打印結(jié)果如下

經(jīng)過上面的操作,我們已經(jīng)將敏感信息加密

到此這篇關(guān)于SpringBoot整合jasypt實現(xiàn)敏感信息的加密詳解的文章就介紹到這了,更多相關(guān)SpringBoot敏感信息加密內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java 中Collection存儲器詳解及簡單實例

    java 中Collection存儲器詳解及簡單實例

    這篇文章主要介紹了java 中Collection存儲器詳解及簡單實例的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Java實現(xiàn)一個簡單的長輪詢的示例代碼

    Java實現(xiàn)一個簡單的長輪詢的示例代碼

    長輪詢是與服務(wù)器保持即時通信的最簡單的方式,它不使用任何特定的協(xié)議,例如 WebSocket ,所以也不依賴于瀏覽器版本等外部條件的兼容性。本文將用Java實現(xiàn)一個簡單的長輪詢,需要的可以參考一下
    2022-08-08
  • SpringBoot兩種方式刷新配置信息

    SpringBoot兩種方式刷新配置信息

    這篇文章主要介紹了SpringBoot兩種方式刷新配置信息,一種是@?ConfigurationProperties?不能自動刷新,需要手動調(diào)用contextRefresher.refresh()方法來刷新配置,第二種方法可以嘗試下,需要的朋友可以參考下
    2023-08-08
  • Mybatis-Plus?新增獲取自增列id方式

    Mybatis-Plus?新增獲取自增列id方式

    這篇文章主要介紹了Mybatis-Plus?新增獲取自增列id方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Spring的BeanFactoryPostProcessor接口示例代碼詳解

    Spring的BeanFactoryPostProcessor接口示例代碼詳解

    這篇文章主要介紹了Spring的BeanFactoryPostProcessor接口,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • IDEA 2020.1.1好用的plugins插件推薦

    IDEA 2020.1.1好用的plugins插件推薦

    這篇文章主要介紹了IDEA 2020.1.1好用的plugins插件推薦,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • SpringBoot中@Conditional注解的使用

    SpringBoot中@Conditional注解的使用

    這篇文章主要介紹了SpringBoot中@Conditional注解的使用,@Conditional注解是一個條件裝配注解,主要用于限制@Bean注解在什么時候才生效,以指定的條件形式控制bean的創(chuàng)建,需要的朋友可以參考下
    2024-01-01
  • 這一次搞懂Spring的XML解析原理說明

    這一次搞懂Spring的XML解析原理說明

    這篇文章主要介紹了這一次搞懂Spring的XML解析原理說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 解決Spring導(dǎo)出可以運行的jar包問題

    解決Spring導(dǎo)出可以運行的jar包問題

    最近需要解決Maven項目導(dǎo)入可執(zhí)行的jar包的問題,如果項目不包含Spring,那么使用mvn assembly:assembly即可,這篇文章主要介紹了Spring導(dǎo)出可以運行的jar包,需要的朋友可以參考下
    2023-03-03
  • java生成驗證碼步驟歸納總結(jié)

    java生成驗證碼步驟歸納總結(jié)

    這篇文章主要為大家詳細介紹了java生成驗證碼的步驟總結(jié),需要的朋友可以參考下
    2017-04-04

最新評論