SpringBoot + Sharding Sphere:輕松搞定數(shù)據(jù)加解密,支持字段級(jí)!
01、故事背景
在實(shí)際的軟件系統(tǒng)開(kāi)發(fā)過(guò)程中,由于業(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ù)開(kāi)發(fā)過(guò)程中,我們?nèi)绾慰焖賹?shí)現(xiàn)呢?
今天通過(guò)這篇文章,我們一起來(lái)了解一下如何在 Spring Boot 中快速實(shí)現(xiàn)數(shù)據(jù)的加解密功能。廢話不多說(shuō)了,直接擼代碼!
02、方案實(shí)踐
在 Spring Boot 生態(tài)中,有一個(gè)非常厲害的開(kāi)源框架:Apache ShardingSphere。
它是一款分布式 SQL 事務(wù)和查詢引擎,可通過(guò)數(shù)據(jù)分片、彈性伸縮、加密等能力對(duì)任意數(shù)據(jù)庫(kù)進(jìn)行增強(qiáng)。我們可以利用它的數(shù)據(jù)脫敏模塊,快速實(shí)現(xiàn) SQL 字段的加解密操作。
如果當(dāng)前項(xiàng)目是采用 Spring Boot 開(kāi)發(fā)的,可以實(shí)現(xiàn)無(wú)縫集成,對(duì)原系統(tǒng)的改造會(huì)非常少。
下面以用戶表為例,一起了解一下ShardingSphere的數(shù)據(jù)加解密的實(shí)現(xiàn)過(guò)程!
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、編寫(xiě)數(shù)據(jù)持久層
然后,編寫(xiě)一個(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è)試
最后,我們編寫(xiě)一個(gè)單元測(cè)試,驗(yàn)證一下代碼的正確性。
編寫(xiě)啟用服務(wù)程序
@SpringBootApplication
@MapperScan("com.example.shardingsphere.mapper")
public class ShardingSphereApplication {
public static void main(String[] args) {
SpringApplication.run(ShardingSphereApplication.class, args);
}
}
編寫(xiě)單元測(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ù)被成功解密!
采用配置方式,最大的好處就是直接通過(guò)配置脫敏列就可以完成對(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è)要留心一下。
希望以上的案例,能幫助到大家!
想要獲取項(xiàng)目源代碼的小伙伴,可以通過(guò)如下地址獲?。?/p>
https://gitee.com/pzblogs/spring-boot-example-demo