日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

SpringBoot + Sharding Sphere:輕松搞定數據加解密,支持字段級!

來源: 責編: 時間:2024-07-17 16:55:26 599觀看
導讀01、故事背景在實際的軟件系統開發過程中,由于業務的需求,在代碼層面實現數據的脫敏還是遠遠不夠的,往往還需要在數據庫層面針對某些關鍵性的敏感信息,例如:身份證號、銀行卡號、手機號、工資等信息進行加密存儲,實現真正意

01、故事背景

在實際的軟件系統開發過程中,由于業務的需求,在代碼層面實現數據的脫敏還是遠遠不夠的,往往還需要在數據庫層面針對某些關鍵性的敏感信息,例如:身份證號、銀行卡號、手機號、工資等信息進行加密存儲,實現真正意義的數據混淆脫敏,以滿足信息安全的需要。zVh28資訊網——每日最新資訊28at.com

那在實際的業務開發過程中,我們如何快速實現呢?zVh28資訊網——每日最新資訊28at.com

今天通過這篇文章,我們一起來了解一下如何在 Spring Boot 中快速實現數據的加解密功能。廢話不多說了,直接擼代碼!zVh28資訊網——每日最新資訊28at.com

02、方案實踐

在 Spring Boot 生態中,有一個非常厲害的開源框架:Apache ShardingSphere。zVh28資訊網——每日最新資訊28at.com

它是一款分布式 SQL 事務和查詢引擎,可通過數據分片、彈性伸縮、加密等能力對任意數據庫進行增強。我們可以利用它的數據脫敏模塊,快速實現 SQL 字段的加解密操作。zVh28資訊網——每日最新資訊28at.com

如果當前項目是采用 Spring Boot 開發的,可以實現無縫集成,對原系統的改造會非常少。zVh28資訊網——每日最新資訊28at.com

下面以用戶表為例,一起了解一下ShardingSphere的數據加解密的實現過程!zVh28資訊網——每日最新資訊28at.com

2.1、創建用戶表

首先,在數據庫中創建一張用戶表,示例腳本如下!zVh28資訊網——每日最新資訊28at.com

CREATE TABLE user (  id bigint(20) NOT NULL COMMENT '用戶ID',  email varchar(255)  NOT NULL DEFAULT '' COMMENT '郵件',  nick_name varchar(255)  DEFAULT NULL COMMENT '昵稱',  pass_word varchar(255)  NOT NULL DEFAULT '' COMMENT '二次密碼',  reg_time varchar(255)  NOT NULL DEFAULT '' COMMENT '注冊時間',  user_name varchar(255)  NOT NULL DEFAULT '' COMMENT '用戶名',  salary varchar(255) DEFAULT NULL COMMENT '基本工資',  PRIMARY KEY (id) USING BTREE) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

2.2、創建 springboot 項目并添加依賴包

接著,創建一個 Spring Boot 項目,并添加相關的依賴包,示例如下:zVh28資訊網——每日最新資訊28at.com

<dependencies>    <!--spring boot核心-->    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter</artifactId>    </dependency>    <!--spring boot 測試-->    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-test</artifactId>        <scope>test</scope>    </dependency>    <!--springmvc web-->    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <!--mysql 數據源-->    <dependency>        <groupId>mysql</groupId>        <artifactId>mysql-connector-java</artifactId>    </dependency>    <!--mybatis 支持-->    <dependency>        <groupId>org.mybatis.spring.boot</groupId>        <artifactId>mybatis-spring-boot-starter</artifactId>        <version>2.0.0</version>    </dependency>     <!--shardingsphere數據分片、脫敏工具-->    <dependency>        <groupId>org.apache.shardingsphere</groupId>        <artifactId>sharding-jdbc-spring-boot-starter</artifactId>        <version>4.1.0</version>    </dependency>    <dependency>        <groupId>org.apache.shardingsphere</groupId>        <artifactId>sharding-jdbc-spring-namespace</artifactId>        <version>4.1.0</version>    </dependency></dependencies>

2.3、添加相關配置

在application.properties文件中,添加shardingsphere相關配置,即可實現針對某個表進行脫敏zVh28資訊網——每日最新資訊28at.com

server.port=8080logging.path=log#shardingsphere數據源集成spring.shardingsphere.datasource.name=dsspring.shardingsphere.datasource.ds.type=com.zaxxer.hikari.HikariDataSourcespring.shardingsphere.datasource.ds.driver-class-name=com.mysql.cj.jdbc.Driverspring.shardingsphere.datasource.ds.jdbc-url=jdbc:mysql://127.0.0.1:3306/testspring.shardingsphere.datasource.ds.username=xxxxspring.shardingsphere.datasource.ds.password=xxxx#加密方式、密鑰配置spring.shardingsphere.encrypt.encryptors.encryptor_aes.type=aesspring.shardingsphere.encrypt.encryptors.encryptor_aes.props.aes.key.value=hkiqAXU6Ur5fixGHaO4Lb2V2ggausYwW#plainColumn表示明文列,cipherColumn表示脫敏列spring.shardingsphere.encrypt.tables.user.columns.salary.plainColumn=spring.shardingsphere.encrypt.tables.user.columns.salary.cipherColumn=salary#spring.shardingsphere.encrypt.tables.user.columns.pass_word.assistedQueryColumn=spring.shardingsphere.encrypt.tables.user.columns.salary.encryptor=encryptor_aes#sql打印spring.shardingsphere.props.sql.show=truespring.shardingsphere.props.query.with.cipher.column=true#基于xml方法的配置mybatis.mapper-locations=classpath:mapper/*.xml

其中有幾個的配置信息比較重要,spring.shardingsphere.encrypt.tables是指要脫敏的表,user是表名,salary表示user表中的真實列,其中plainColumn指的是明文列,cipherColumn指的是脫敏列,如果是新工程,只需要配置脫敏列即可!zVh28資訊網——每日最新資訊28at.com

配置示例如下!zVh28資訊網——每日最新資訊28at.com

# 用于告訴 ShardingSphere 數據表里哪個列用于存儲明文數據spring.shardingsphere.encrypt.tables.user.columns.salary.plainColumn=# 用于告訴 ShardingSphere 數據表里哪個列用于存儲密文數據spring.shardingsphere.encrypt.tables.user.columns.salary.cipherColumn=salary# 用于告訴 ShardingSphere 數據表里哪個列用于存儲輔助查詢數據#spring.shardingsphere.encrypt.tables.user.columns.salary.assistedQueryColumn=# 用于告訴 ShardingSphere 數據表里哪個列使用什么算法加解密spring.shardingsphere.encrypt.tables.user.columns.salary.encryptor=encryptor_aes

2.4、編寫數據持久層

然后,編寫一個數據持久層,用于數據的存儲和查詢操作。zVh28資訊網——每日最新資訊28at.com

<mapper namespace="com.example.shardingsphere.mapper.UserMapperXml" >    <resultMap id="BaseResultMap" type="com.example.shardingsphere.entity.UserEntity" >        <id column="id" property="id" jdbcType="BIGINT" />        <result column="email" property="email" jdbcType="VARCHAR" />        <result column="nick_name" property="nickName" jdbcType="VARCHAR" />        <result column="pass_word" property="passWord" jdbcType="VARCHAR" />        <result column="reg_time" property="regTime" jdbcType="VARCHAR" />        <result column="user_name" property="userName" jdbcType="VARCHAR" />        <result column="salary" property="salary" jdbcType="VARCHAR" />    </resultMap>    <select id="findAll" resultMap="BaseResultMap">        SELECT * FROM user    </select>        <insert id="insert" parameterType="com.example.shardingsphere.entity.UserEntity">        INSERT INTO user(id,email,nick_name,pass_word,reg_time,user_name, salary)        VALUES(#{id},#{email},#{nickName},#{passWord},#{regTime},#{userName}, #{salary})    </insert></mapper>
public interface UserMapperXml {    /**     * 查詢所有的信息     * @return     */    List<UserEntity> findAll();    /**     * 新增數據     * @param user     */    void insert(UserEntity user);}
public class UserEntity {    private Long id;    private String email;    private String nickName;    private String passWord;    private String regTime;    private String userName;    private String salary;    //省略set、get...}

2.5、單元測試

最后,我們編寫一個單元測試,驗證一下代碼的正確性。zVh28資訊網——每日最新資訊28at.com

編寫啟用服務程序zVh28資訊網——每日最新資訊28at.com

@SpringBootApplication@MapperScan("com.example.shardingsphere.mapper")public class ShardingSphereApplication {    public static void main(String[] args) {        SpringApplication.run(ShardingSphereApplication.class, args);    }}

編寫單元測試zVh28資訊網——每日最新資訊28at.com

@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = ShardingSphereApplication.class)public class UserTest {    @Autowired    private UserMapperXml userMapperXml;    @Test    public void insert() throws Exception {        UserEntity entity = new UserEntity();        entity.setId(3l);        entity.setEmail("123@123.com");        entity.setNickName("阿三");        entity.setPassWord("123");        entity.setRegTime("2021-10-10 00:00:00");        entity.setUserName("張三");        entity.setSalary("2500");        userMapperXml.insert(entity);    }    @Test    public void query() throws Exception {        List<UserEntity> dataList = userMapperXml.findAll();        System.out.println(JSON.toJSONString(dataList));    }}

插入數據后,如下圖,數據庫存儲的數據已被加密!zVh28資訊網——每日最新資訊28at.com

我們繼續來看看,運行查詢服務,結果如下圖,數據被成功解密!zVh28資訊網——每日最新資訊28at.com

采用配置方式,最大的好處就是直接通過配置脫敏列就可以完成對某些數據表字段的脫敏,非常方便。zVh28資訊網——每日最新資訊28at.com

三、小結

當需要對某些數據表字段進行脫敏處理的時候,可以采用 Apache ShardingSphere 框架快速實現。zVh28資訊網——每日最新資訊28at.com

但是有個細節很容易遺漏,那就是字段類型,例如salary字段,根據常規,很容易想到使用數字類型,但是卻不是,要知道加密之后的數據都是一串亂碼,數字類型肯定是無法存儲字符串的,因此在定義的時候,這個要留心一下。zVh28資訊網——每日最新資訊28at.com

希望以上的案例,能幫助到大家!zVh28資訊網——每日最新資訊28at.com

想要獲取項目源代碼的小伙伴,可以通過如下地址獲取!zVh28資訊網——每日最新資訊28at.com

https://gitee.com/pzblogs/spring-boot-example-demo

本文鏈接:http://www.www897cc.com/showinfo-26-101382-0.htmlSpringBoot + Sharding Sphere:輕松搞定數據加解密,支持字段級!

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 如何利用負載均衡器實現終極自由

下一篇: 新的 HTML &lt;dialog&gt; 標簽:絕對改變游戲規則

標簽:
  • 熱門焦點
  • 一加Ace2 Pro官宣:普及16G內存 引領24G

    一加官方今天繼續為本月發布的新機一加Ace2 Pro帶來預熱,公布了內存方面的信息。“淘汰 8GB ,12GB 起步,16GB 普及,24GB 引領,還有呢?#一加Ace2Pro#,2023 年 8 月,敬請期待。”同時
  • MIX Fold3包裝盒泄露 新機本月登場

    小米的全新折疊屏旗艦MIX Fold3將于本月發布,近日該機的真機包裝盒在網上泄露。從圖上來看,新的MIX Fold3包裝盒在外觀設計方面延續了之前的方案,變化不大,這也是目前小米旗艦
  • Rust中的高吞吐量流處理

    作者 | Noz編譯 | 王瑞平本篇文章主要介紹了Rust中流處理的概念、方法和優化。作者不僅介紹了流處理的基本概念以及Rust中常用的流處理庫,還使用這些庫實現了一個流處理程序
  • 谷歌KDD'23工作:如何提升推薦系統Ranking模型訓練穩定性

    谷歌在KDD 2023發表了一篇工作,探索了推薦系統ranking模型的訓練穩定性問題,分析了造成訓練穩定性存在問題的潛在原因,以及現有的一些提升模型穩定性方法的不足,并提出了一種新
  • 只需五步,使用start.spring.io快速入門Spring編程

    步驟1打開https://start.spring.io/,按照屏幕截圖中的內容創建項目,添加 Spring Web 依賴項,并單擊“生成”按鈕下載 .zip 文件,為下一步做準備。請在進入步驟2之前進行解壓。圖
  • 年輕人的“職場羞恥感”,無處不在

    作者:馮曉亭 陶 淘 李 欣 張 琳 馬舒葉來源:燃次元&ldquo;人在職場,應該選擇什么樣的著裝?&rdquo;近日,在網絡上,一個與著裝相關的帖子引發關注,在該帖子里,一位在高級寫字樓亞洲金
  • 三星推出Galaxy Tab S9系列平板電腦以及Galaxy Watch6系列智能手表

    2023年7月26日,三星電子正式發布了Galaxy Z Flip5與Galaxy Z Fold5。除此之外,Galaxy Tab S9系列平板電腦以及三星Galaxy Watch6系列智能手表也同期
  • 三星顯示已開始為AR設備研發硅基LED微顯示屏

    7月18日消息,據外媒報道,隨著蘋果首款頭顯產品Vision Pro在6月份正式推出,AR/VR/MR等頭顯產品也就將成為各大公司下一個重要的競爭領域,對顯示屏這一關
  • 親歷馬斯克血洗Twitter,硅谷的苦日子在后頭

    文/劉哲銘  編輯/李薇  馬斯克再次揮下裁員大刀。  美國時間11月14日,Twitter約4400名外包員工遭解雇,此次被解雇的員工的主要工作為內容審核等。此前,T
Top 主站蜘蛛池模板: 寻甸| 灯塔市| 临夏县| 乐都县| 南郑县| 建平县| 遵化市| 孟州市| 铜鼓县| 三门县| 文登市| 金山区| 司法| 江北区| 福清市| 县级市| 抚松县| 新宁县| 维西| 天津市| 永平县| 江源县| 滦平县| 开平市| 尼木县| 石河子市| 紫云| 铁力市| 汉川市| 隆安县| 梁河县| 临桂县| 沂源县| 洛隆县| 龙口市| 昌江| 南康市| 疏勒县| 密山市| 永福县| 聂拉木县|