SpringBoot+?Sharding?Sphere?輕松實(shí)現(xiàn)數(shù)據(jù)庫(kù)字段加解密功能
一、介紹
在實(shí)際的軟件系統(tǒng)開發(fā)過程中,由于業(yè)務(wù)的需求,在代碼層面實(shí)現(xiàn)數(shù)據(jù)的脫敏還是遠(yuǎn)遠(yuǎn)不夠的,往往還需要在數(shù)據(jù)庫(kù)層面針對(duì)某些關(guān)鍵性的敏感信息,例如:身份證號(hào)、銀行卡號(hào)、手機(jī)號(hào)、工資等信息進(jìn)行加密存儲(chǔ),實(shí)現(xiàn)真正意義的數(shù)據(jù)混淆脫敏,以滿足信息安全的需要。
那在實(shí)際的業(yè)務(wù)開發(fā)過程中,我們?nèi)绾慰焖賹?shí)現(xiàn)呢?
今天通過這篇文章,我們一起來(lái)了解一下如何在 Spring Boot 中快速實(shí)現(xiàn)數(shù)據(jù)的加解密功能。廢話不多說(shuō)了,直接擼代碼!
二、方案實(shí)踐
在 Spring Boot 生態(tài)中,有一個(gè)非常厲害的開源框架:Apache ShardingSphere。
它是一款分布式 SQL 事務(wù)和查詢引擎,可通過數(shù)據(jù)分片、彈性伸縮、加密等能力對(duì)任意數(shù)據(jù)庫(kù)進(jìn)行增強(qiáng)。我們可以利用它的數(shù)據(jù)脫敏模塊,快速實(shí)現(xiàn) SQL 字段的加解密操作。
如果當(dāng)前項(xiàng)目是采用 Spring Boot 開發(fā)的,可以實(shí)現(xiàn)無(wú)縫集成,對(duì)原系統(tǒng)的改造會(huì)非常少。
下面以用戶表為例,一起了解一下ShardingSphere
的數(shù)據(jù)加解密的實(shí)現(xiàn)過程!
2.1、創(chuàng)建用戶表
首先,在數(shù)據(jù)庫(kù)中創(chuàng)建一張用戶表,示例腳本如下!
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 '注冊(cè)時(shí)間', 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、創(chuàng)建 springboot 項(xiàng)目并添加依賴包
接著,創(chuàng)建一個(gè) Spring Boot 項(xiàng)目,并添加相關(guān)的依賴包,示例如下:
<dependencies> <!--spring boot核心--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--spring boot 測(cè)試--> <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 數(shù)據(jù)源--> <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數(shù)據(jù)分片、脫敏工具--> <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、添加相關(guān)配置
在application.properties
文件中,添加shardingsphere
相關(guān)配置,即可實(shí)現(xiàn)針對(duì)某個(gè)表進(jìn)行脫敏
server.port=8080 logging.path=log #shardingsphere數(shù)據(jù)源集成 spring.shardingsphere.datasource.name=ds spring.shardingsphere.datasource.ds.type=com.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.ds.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.ds.jdbc-url=jdbc:mysql://127.0.0.1:3306/test spring.shardingsphere.datasource.ds.username=xxxx spring.shardingsphere.datasource.ds.password=xxxx #加密方式、密鑰配置 spring.shardingsphere.encrypt.encryptors.encryptor_aes.type=aes spring.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=true spring.shardingsphere.props.query.with.cipher.column=true #基于xml方法的配置 mybatis.mapper-locations=classpath:mapper/*.xml
其中有幾個(gè)的配置信息比較重要,spring.shardingsphere.encrypt.tables
是指要脫敏的表,user
是表名,salary
表示user
表中的真實(shí)列,其中plainColumn
指的是明文列,cipherColumn
指的是脫敏列,如果是新工程,只需要配置脫敏列即可!
配置示例如下!
# 用于告訴 ShardingSphere 數(shù)據(jù)表里哪個(gè)列用于存儲(chǔ)明文數(shù)據(jù) spring.shardingsphere.encrypt.tables.user.columns.salary.plainColumn= # 用于告訴 ShardingSphere 數(shù)據(jù)表里哪個(gè)列用于存儲(chǔ)密文數(shù)據(jù) spring.shardingsphere.encrypt.tables.user.columns.salary.cipherColumn=salary # 用于告訴 ShardingSphere 數(shù)據(jù)表里哪個(gè)列用于存儲(chǔ)輔助查詢數(shù)據(jù) #spring.shardingsphere.encrypt.tables.user.columns.salary.assistedQueryColumn= # 用于告訴 ShardingSphere 數(shù)據(jù)表里哪個(gè)列使用什么算法加解密 spring.shardingsphere.encrypt.tables.user.columns.salary.encryptor=encryptor_aes
2.4、編寫數(shù)據(jù)持久層
然后,編寫一個(gè)數(shù)據(jù)持久層,用于數(shù)據(jù)的存儲(chǔ)和查詢操作。
<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(); /** * 新增數(shù)據(jù) * @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、單元測(cè)試
最后,我們編寫一個(gè)單元測(cè)試,驗(yàn)證一下代碼的正確性。
編寫啟用服務(wù)程序
@SpringBootApplication @MapperScan("com.example.shardingsphere.mapper") public class ShardingSphereApplication { public static void main(String[] args) { SpringApplication.run(ShardingSphereApplication.class, args); } }
編寫單元測(cè)試
@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)); } }
插入數(shù)據(jù)后,如下圖,數(shù)據(jù)庫(kù)存儲(chǔ)的數(shù)據(jù)已被加密!
我們繼續(xù)來(lái)看看,運(yùn)行查詢服務(wù),結(jié)果如下圖,數(shù)據(jù)被成功解密!
采用配置方式,最大的好處就是直接通過配置脫敏列就可以完成對(duì)某些數(shù)據(jù)表字段的脫敏,非常方便。
三、小結(jié)
當(dāng)需要對(duì)某些數(shù)據(jù)表字段進(jìn)行脫敏處理的時(shí)候,可以采用 Apache ShardingSphere 框架快速實(shí)現(xiàn)。
但是有個(gè)細(xì)節(jié)很容易遺漏,那就是字段類型,例如salary
字段,根據(jù)常規(guī),很容易想到使用數(shù)字類型,但是卻不是,要知道加密之后的數(shù)據(jù)都是一串亂碼,數(shù)字類型肯定是無(wú)法存儲(chǔ)字符串的,因此在定義的時(shí)候,這個(gè)要留心一下。
希望以上的案例,能幫助到大家!
示例代碼:spring-boot-example-shardingsphere
到此這篇關(guān)于SpringBoot+ Sharding Sphere 輕松實(shí)現(xiàn)數(shù)據(jù)庫(kù)字段加解密的文章就介紹到這了,更多相關(guān)SpringBoot Sharding Sphere 數(shù)據(jù)庫(kù)字段加解密內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot整合ShardingSphere5.x實(shí)現(xiàn)數(shù)據(jù)加解密功能(最新推薦)
- springboot如何使用yml文件方式配置shardingsphere
- SpringBoot3和ShardingSphere5框架實(shí)現(xiàn)數(shù)據(jù)分庫(kù)分表
- springboot整合shardingsphere和seata實(shí)現(xiàn)分布式事務(wù)的實(shí)踐
- SpringBoot整合ShardingSphere的示例代碼
- Springboot2.x+ShardingSphere實(shí)現(xiàn)分庫(kù)分表的示例代碼
相關(guān)文章
Spring MVC攔截器(Interceptor)的定義和配置過程
這篇文章主要介紹了Spring MVC攔截器(Interceptor)的定義和配置過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03Java實(shí)現(xiàn)注冊(cè)郵箱激活賬戶實(shí)例代碼
本篇文章主要介紹了Java實(shí)現(xiàn)郵箱激活賬戶實(shí)例代碼,這里整理了詳細(xì)的代碼,具有一定的參考價(jià)值,有需要的小伙伴可以參考下。2017-07-07SpringBoot中的@PostConstruct注解詳細(xì)解析
這篇文章主要介紹了SpringBoot中的@PostConstruct注解詳細(xì)解析,@PostConstruct注解,主要用于在Spring容器啟動(dòng)時(shí)執(zhí)行某些操作或者任務(wù),@PostConstruct注解一般放在BEAN的方法上,一旦BEAN初始化完成之后,將會(huì)調(diào)用這個(gè)方法,需要的朋友可以參考下2024-01-01IDEA之IDEA連接gitlab協(xié)同開發(fā)方式
通過IDEA克隆GitLab項(xiàng)目實(shí)現(xiàn)代碼協(xié)同開發(fā)相較于使用SourceTree,?通過IDEA連接GitLab進(jìn)行代碼協(xié)同開發(fā)更顯便捷,方法包括通過VersionControl創(chuàng)建新項(xiàng)目,輸入項(xiàng)目的git?HTTP地址和本地路徑,測(cè)試連接成功后克隆項(xiàng)目,修改代碼后2024-11-11數(shù)組重排序(如何將所有奇數(shù)都放在所有偶數(shù)前面)的深入分析
本篇文章是對(duì)數(shù)組重排序(如何將所有奇數(shù)都放在所有偶數(shù)前面)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06詳解CopyOnWriteArrayList是如何保證線程安全
這篇文章主要為大家介紹了CopyOnWriteArrayList是如何保證線程安全講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09