ShardingJDBC 13_对用户密码加解密

156 阅读2分钟

1 创建数据表

在数据库中创建一个用于存储用户姓名、明文密码、加密密码的数据表 t_user。

CREATE TABLE t_user (

id BIGINT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,

name VARCHAR(50) DEFAULT NULL,

pwd_plain varchar(255) DEFAULT NULL,

pwd_cipher varchar(255) DEFAULT NULL

) ENGINE=INNODB DEFAULT CHARSET=utf8mb4;

2 创建实体类

创建的实体类的成员变量,与数据表字段并不完全一致。

因为是进行加解密,数据库中保存的是明文列密文列,但是在实体类中仅需要定义逻辑列即可。

package com.myjdbc.entity;

import lombok.Data;

import javax.persistence.*;
import java.io.Serializable;

@Data
@Entity
@Table(name = "t_user")
public class TUser implements Serializable {
    @Id
    @Column(name = "id", nullable = false)
    //设置自增策略
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "name")
    private String name;

    //设置逻辑列名
    @Column(name = "pwd")
    private String pwd;
}

3 创建接口类

创建资源类接口,需要继承自 JpaRepository,需要用到 TUser 实体类及主键类型 Long。

package com.myjdbc.repository;

import com.myjdbc.entity.TUser;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface TUserRepository extends JpaRepository<TUser, Long> {
    List<TUser> findByPwd(String pwd);
}

4 创建配置文件

添加配置文件 application-encypter.properties

  • 配置明文列

spring.shardingsphere.encrypt.tables.t_user.columns.pwd.plain-column=pwd_plain

  • 配置密文列

spring.shardingsphere.encrypt.tables.t_user.columns.pwd.cipher-column=pwd_cipher

  • 配置加密类型

spring.shardingsphere.encrypt.encryptors.my_pwd.type=aes

  • 配置用于加密的密钥

spring.shardingsphere.encrypt.encryptors.my_pwd.props.aes.key.value=123456

  • 将要加密的字段与加密类型进行关联

spring.shardingsphere.encrypt.tables.t_user.columns.pwd.encryptor=my_pwd

  • 配置主键及主键生成策略

spring.shardingsphere.sharding.tables.t_user.key-generator.column=id

spring.shardingsphere.sharding.tables.t_user.key-generator.type=SNOWFLAKE

  • 是否启用加密数据进行查询,默认为false,即使用明文进行查询

spring.shardingsphere.props.query.with.cipher.column=false

#datasource
spring.shardingsphere.datasource.names=ds0

spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds0.jdbc-url=jdbc:mysql://192.168.137.3:3366/orders?characterEncoding=UTF-8&useUnicode=true&serverTimezone=Asia/Shanghai
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=123456

spring.shardingsphere.encrypt.tables.t_user.columns.pwd.plain-column=pwd_plain
spring.shardingsphere.encrypt.tables.t_user.columns.pwd.cipher-column=pwd_cipher

spring.shardingsphere.encrypt.encryptors.my_pwd.type=aes
spring.shardingsphere.encrypt.encryptors.my_pwd.props.aes.key.value=123456

spring.shardingsphere.encrypt.tables.t_user.columns.pwd.encryptor=my_pwd

spring.shardingsphere.sharding.tables.t_user.key-generator.column=id
spring.shardingsphere.sharding.tables.t_user.key-generator.type=SNOWFLAKE

spring.shardingsphere.props.query.with.cipher.column=false

修改配置文件 application.properties 将其切换到对新配置文件的引用。

spring.profiles.active=encypter
spring.shardingsphere.props.sql.show=true

5 创建测试类

5.1 插入数据

创建对象添加的方法并执行

package com.myjdbc;

import com.myjdbc.entity.TUser;
import com.myjdbc.repository.TUserRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ShardingJdbc.class)
public class TestTuser {
    @Resource
    private TUserRepository repository;

    @Test
    public void addUser(){
        TUser user = new TUser();
        user.setName("张三");
        user.setPwd("123456");
        TUser save = repository.save(user);
        System.out.println(save);
    }
}

数据插入成功,并将明文数据转换为密文数据,进入了插入。

image.png

5.2 查询数据

5.2.1 使用明文查询

这也是默认的查询方式。

添加查询方法,方法中输入的密码为明文。

运行测试并观察控制台输出

@Test
public void queryUser(){
    List<TUser> all = repository.findByPwd("123456");
    all.forEach(System.out::println);
}

可以看到查询语句中,where 子句中使用的是明文进行的查询。

image.png

5.2.2 使用加密数据查询

修改配置文件中的配置项,将其改为 true,即使用加密数据进行查询

spring.shardingsphere.props.query.with.cipher.column=true

再次运行查询程序,并观察控制台输出

image.png

可以看到 where 子句后使用的是加密列进行的查询。