文章目录
插件开发流程
项目搭建:
- 新建Maven工程,并引入相关依赖
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hx</groupId>
<artifactId>hx-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hx-base</name>
<description>hx-base</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
- yaml配置 application.yml
spring:
datasource:
username: root
password: a
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
# Hikari连接池配置
hikari:
#最小连接池数
minimum-idle: 5
# 最大连接池数量
maximum-pool-size: 150
auto-commit: true
pool-name: MyHicaryCP
#最大超时时间 10分钟
idle-timeout: 180000
# 最长生命周期,0无限期,默认1800000,即30分钟
max-lifetime: 1800000
# 连接超时时间,默认30秒
connection-timeout: 30000
connection-test-query: SELECT 1
mybatis:
mapper-locations: classpath*:/mapper/*.xml
type-aliases-package: com.hx.base.*
Mybatis四大对象
mybatis插件:拦截器
一个接口:Interceptor
源码解读:
package org.apache.ibatis.plugin;
import java.util.Properties;
//mybatis中的拦截器使用的是代理实现的
public interface Interceptor {
//拦截逻辑,前后拦截都写在此方法
Object intercept(Invocation var1) throws Throwable;
default Object plugin(Object target) {
//包装成代理对象,并加入拦截器链
return Plugin.wrap(target, this);
}
//设置属性。
default void setProperties(Properties properties) {
// NOP
}
}
四大对象
1. Executor method 执行器
2. ParameterHandler 参数处理器
3. ResultHandler 结果处理器; ResultSetHandler 结果集处理器
4. StatementHandler sql语法构建,构建SQL语句
数据脱敏插件
实现:
- 脱敏方法接口 DesensitizeFunction.java
package com.hx.hxbase.desensitizePlugin;
import java.util.function.Function;
/**
* @Author: Huathy
* @ClassPath: com.hx.hxbase.desensitizePlugin.DesensitizeFunction
* @Date: 2021-04-16 16:38
* @Description: 脱敏方法接口
*/
public interface DesensitizeFunction extends Function<String, String> {
}
- 脱敏策略枚举类 DesensitizeStrategy.java
package com.hx.hxbase.desensitizePlugin;
/**
* 脱敏策略
*
* @author Huathy
*/
public enum DesensitizeStrategy {
USERNAME(s -> s.replaceAll("(\\S)\\S(\\S*)", "$1*$2")),
ID_CARD(s -> s.replaceAll("(\\d{4})\\d{10}(\\w{4})", "$1****$2")),
PHONE(s -> s.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2")),
ADDRESS(s -> s.replaceAll("(\\S{8})\\S{4}(\\S*)\\S{4}", "$1****$2****")),
PASSWORD(s -> s.replaceAll("([\\s\\S]*)([\\d\\D])([\\w\\W]*)", "******")),
;
private final DesensitizeFunction desensitizeFunction;
DesensitizeStrategy(DesensitizeFunction fun) {
this.desensitizeFunction = fun;
}
public DesensitizeFunction getDesensitizeFunction() {
return desensitizeFunction;
}
}
- 定义脱敏注解 Desensitize.java
package com.hx.hxbase.desensitizePlugin;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 脱敏注解
* @author Huathy
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Desensitize {
DesensitizeStrategy strategy();
}
- 脱敏插件类
package com.hx.hxbase.desensitizePlugin;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.sql.Statement;
import java.util.List;
import java.util.stream.Stream;
/**
* @Author: Huathy
* @ClassPath: com.hx.hxbase.plugin.DataDesensitizePlugin
* @Date: 2021-04-16 16:23
* @Description: 数据脱敏插件
*/
//拦截器注解 签名注解 拦截类 拦截方法 参数
@Intercepts(@Signature(type = ResultSetHandler.class, method = "handleResultSets", args = Statement.class))
@Slf4j
@Component
public class DataDesensitizePlugin implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
//调用方法查询记录
List<Object> records = (List<Object>) invocation.proceed();
records.forEach(this::desensitize);
return records;
}
/**
* 脱敏方法
*
* @param source
*/
private void desensitize(Object source) {
//只是class对象
Class<?> sourceClass = source.getClass();
//返回MetaObject,真正的包含值的记录
MetaObject metaObject = SystemMetaObject.forObject(source);
Stream.of(sourceClass.getDeclaredFields())
.filter(field -> field.isAnnotationPresent(Desensitize.class))
.forEach(field -> doDesensitize(metaObject, field));
}
private void doDesensitize(MetaObject metaObject, Field field) {
String name = field.getName();
Object value = metaObject.getValue(name);
if (value != null && (String.class == metaObject.getGetterType(name) ||
Integer.class == metaObject.getGetterType(name) || Long.class == metaObject.getGetterType(name))) {
Desensitize desensitize = field.getAnnotation(Desensitize.class);
DesensitizeStrategy strategy = desensitize.strategy();
//获取一下脱敏的方式
String result = strategy.getDesensitizeFunction().apply(String.valueOf(value));
//按照数据类型,来将脱敏的结果输入结果对象
if (String.class == metaObject.getGetterType(name)) {
metaObject.setValue(name, result);
} else if (Integer.class == metaObject.getGetterType(name)) {
metaObject.setValue(name, Integer.valueOf(result));
} else if (Long.class == metaObject.getGetterType(name)) {
metaObject.setValue(name, Long.valueOf(result));
}
log.info("[hx-desensitizePlugin] ===>" + name + "字段脱敏" + result);
} else {
log.info("[hx-desensitizePlugin] ===>" + name + "字段为Null无需脱敏");
}
}
}
测试方法
- 测试实体类
package com.hx.hxbase.entity;
import com.hx.hxbase.desensitizePlugin.Desensitize;
import com.hx.hxbase.desensitizePlugin.DesensitizeStrategy;
import lombok.Data;
/**
* @Author: Huathy
* @ClassPath: com.hx.hxbase.entity.User
* @Date: 2021-04-15 23:35
* @Description:
*/
@Data
public class User {
@Desensitize(strategy = DesensitizeStrategy.ID_CARD)
Integer id;
@Desensitize(strategy = DesensitizeStrategy.USERNAME)
String name;
@Desensitize(strategy = DesensitizeStrategy.PASSWORD)
String pwd;
Integer ref;
}
- 测试Mapper
package com.hx.hxbase.mapper;
import com.hx.hxbase.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @Author: Huathy
* @ClassPath: com.hx.hxbase.mapper.HelloDao
* @Date: 2021-04-15 23:34
* @Description:
*/
@Mapper
@Repository
public interface UserMapper {
@Select("SELECT * FROM `USER`")
List<User> selectAll();
}
- 测试结果: