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

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

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

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

01、故事背景

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

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

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

02、方案實踐

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

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

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

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

2.1、創建用戶表

首先,在數據庫中創建一張用戶表,示例腳本如下!tSK28資訊網——每日最新資訊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 項目,并添加相關的依賴包,示例如下:tSK28資訊網——每日最新資訊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相關配置,即可實現針對某個表進行脫敏tSK28資訊網——每日最新資訊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指的是脫敏列,如果是新工程,只需要配置脫敏列即可!tSK28資訊網——每日最新資訊28at.com

配置示例如下!tSK28資訊網——每日最新資訊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、編寫數據持久層

然后,編寫一個數據持久層,用于數據的存儲和查詢操作。tSK28資訊網——每日最新資訊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、單元測試

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

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

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

編寫單元測試tSK28資訊網——每日最新資訊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));    }}

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

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

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

三、小結

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

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

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

想要獲取項目源代碼的小伙伴,可以通過如下地址獲取!tSK28資訊網——每日最新資訊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; 標簽:絕對改變游戲規則

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 弋阳县| 开原市| 察哈| 张北县| 洛南县| 石渠县| 龙胜| 永定县| 正定县| 顺义区| 新兴县| 土默特左旗| 九江市| 山阴县| 隆安县| 北碚区| 康乐县| 尚义县| 清水县| 馆陶县| 阿拉善盟| 民乐县| 邓州市| 德保县| 桓台县| 肇庆市| 枞阳县| 泗阳县| 平陆县| 晋江市| 松潘县| 原阳县| 南开区| 庆云县| 康保县| 星子县| 安龙县| 太保市| 博兴县| 抚州市| 太白县|