- VisualStudio2022插件的安装及使用-编程手把手系列文章
- pprof-在现网场景怎么用
- C#实现的下拉多选框,下拉多选树,多级节点
- 【学习笔记】基础数据结构:猫树
在实际的软件系统开发过程中,由于业务的需求,在代码层面实现数据的脱敏还是远远不够的,往往还需要在数据库层面针对某些关键性的敏感信息,例如:身份证号、银行卡号、手机号、工资等信息进行加密存储,实现真正意义的数据混淆脱敏,以满足信息安全的需要.
那在实际的业务开发过程中,我们如何快速实现呢?
今天通过这篇文章,我们一起来了解一下如何在 Spring Boot 中快速实现数据的加解密功能。废话不多说了,直接撸代码! 。
在 Spring Boot 生态中,有一个非常厉害的开源框架:Apache ShardingSphere.
它是一款分布式 SQL 事务和查询引擎,可通过数据分片、弹性伸缩、加密等能力对任意数据库进行增强。我们可以利用它的数据脱敏模块,快速实现 SQL 字段的加解密操作.
如果当前项目是采用 Spring Boot 开发的,可以实现无缝集成,对原系统的改造会非常少.
下面以用户表为例,一起了解一下ShardingSphere的数据加解密的实现过程! 。
首先,在数据库中创建一张用户表,示例脚本如下! 。
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;
接着,创建一个 Spring Boot 项目,并添加相关的依赖包,示例如下:
<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>
在application.properties文件中,添加shardingsphere相关配置,即可实现针对某个表进行脱敏 。
server.port=8080
logging.path=log
#shardingsphere数据源集成
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
其中有几个的配置信息比较重要,spring.shardingsphere.encrypt.tables是指要脱敏的表,user是表名,salary表示user表中的真实列,其中plainColumn指的是明文列,cipherColumn指的是脱敏列,如果是新工程,只需要配置脱敏列即可! 。
配置示例如下! 。
# 用于告诉 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
然后,编写一个数据持久层,用于数据的存储和查询操作.
<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...
}
最后,我们编写一个单元测试,验证一下代码的正确性.
编写启用服务程序 。
@SpringBootApplication
@MapperScan("com.example.shardingsphere.mapper")
public class ShardingSphereApplication {
public static void main(String[] args) {
SpringApplication.run(ShardingSphereApplication.class, args);
}
}
编写单元测试 。
@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));
}
}
插入数据后,如下图,数据库存储的数据已被加密! 。
我们继续来看看,运行查询服务,结果如下图,数据被成功解密! 。
采用配置方式,最大的好处就是直接通过配置脱敏列就可以完成对某些数据表字段的脱敏,非常方便.
当需要对某些数据表字段进行脱敏处理的时候,可以采用 Apache ShardingSphere 框架快速实现.
但是有个细节很容易遗漏,那就是字段类型,例如salary字段,根据常规,很容易想到使用数字类型,但是却不是,要知道加密之后的数据都是一串乱码,数字类型肯定是无法存储字符串的,因此在定义的时候,这个要留心一下.
希望以上的案例,能帮助到大家! 。
示例代码:spring-boot-example-shardingsphere 。
最后此篇关于SpringBoot+ShardingSphere轻松实现数据库字段加解密的文章就讲到这里了,如果你想了解更多关于SpringBoot+ShardingSphere轻松实现数据库字段加解密的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
目录 集成sharding jdbc 1. 引入依赖 2. 配置分表规则 问题 集成多数据源
Apache ShardingSphere 是一个开源的分布式数据库,它还有一个用户和开发人员需要的生态系统,为之提供了定制和云原生的体验。 Apache ShardingSphere
谈到分库分表中间件时,我们自然而然的会想到 ShardingSphere-JDBC 。 这篇文章,我们聊聊 ShardingSphere-JDBC 相关知识点,并实战演示一番。
本文整理了Java中io.shardingsphere.core.yaml.sharding.YamlShardingRuleConfiguration类的一些代码示例,展示了YamlSharding
本文整理了Java中org.apache.shardingsphere.core.yaml.YamlAuthentication类的一些代码示例,展示了YamlAuthentication类的具体用法
本文整理了Java中org.apache.shardingsphere.core.yaml.sharding.YamlShardingRuleConfiguration类的一些代码示例,展示了Yaml
本文整理了Java中org.apache.shardingsphere.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration类的一些代码示例,展
本文整理了Java中org.apache.shardingsphere.orchestration.yaml.config.YamlOrchestrationConfiguration类的一些代码示例
本文整理了Java中org.apache.shardingsphere.core.yaml.sharding.YamlKeyGeneratorConfiguration类的一些代码示例,展示了Yaml
本文整理了Java中io.shardingsphere.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration.getMasterSlaveRul
本文整理了Java中io.shardingsphere.core.yaml.sharding.YamlShardingRuleConfiguration.getShardingRuleConfigur
本文整理了Java中org.apache.shardingsphere.core.yaml.YamlAuthentication.getUsername()方法的一些代码示例,展示了YamlAuthe
本文整理了Java中org.apache.shardingsphere.core.yaml.YamlAuthentication.getPassword()方法的一些代码示例,展示了YamlAuthe
本文整理了Java中org.apache.shardingsphere.core.yaml.sharding.YamlShardingRuleConfiguration.getShardingRule
本文整理了Java中org.apache.shardingsphere.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration.getMaster
本文整理了Java中org.apache.shardingsphere.orchestration.yaml.config.YamlOrchestrationConfiguration.getOrch
我是一名优秀的程序员,十分优秀!