Commit 3d9b0293ddf752218e2a12eccd928d9c68fbe323
Merge branch 'desensitization' into develop
Showing
4 changed files
with
95 additions
and
76 deletions
huaheng-wms-core/src/main/java/org/jeecg/modules/desensitization/plugin/DesensitizationInterceptor.java renamed to huaheng-wms-core/src/main/java/org/jeecg/modules/desensitization/plugin/DesensitizationResultSetInterceptor.java
1 | 1 | package org.jeecg.modules.desensitization.plugin; |
2 | 2 | |
3 | - | |
4 | -import cn.hutool.core.util.ObjectUtil; | |
5 | 3 | import org.apache.ibatis.executor.resultset.ResultSetHandler; |
6 | 4 | import org.apache.ibatis.plugin.*; |
7 | 5 | import org.apache.shiro.SecurityUtils; |
8 | -import org.jeecg.common.exception.JeecgBootException; | |
9 | 6 | import org.jeecg.modules.desensitization.annotation.SensitiveField; |
10 | 7 | import org.jeecg.modules.desensitization.enums.SensitiveTypeEnums; |
11 | 8 | import org.jeecg.modules.desensitization.strategy.SensitiveContext; |
12 | 9 | import org.jeecg.modules.desensitization.strategy.SensitiveStrategy; |
13 | -import org.jeecg.modules.system.entity.SysUserRole; | |
14 | -import org.jeecg.modules.system.service.ISysRoleService; | |
15 | -import org.jeecg.utils.HuahengJwtUtil; | |
16 | 10 | import org.reflections.ReflectionUtils; |
17 | 11 | import org.slf4j.Logger; |
18 | 12 | import org.slf4j.LoggerFactory; |
19 | -import org.springframework.core.annotation.Order; | |
20 | -import org.springframework.stereotype.Service; | |
21 | -import org.springframework.util.CollectionUtils; | |
22 | 13 | |
23 | -import javax.annotation.Resource; | |
24 | -import javax.servlet.http.HttpServletRequest; | |
14 | +import java.sql.Statement; | |
25 | 15 | import java.lang.reflect.Field; |
26 | 16 | import java.util.*; |
27 | 17 | import java.util.concurrent.ConcurrentHashMap; |
28 | 18 | |
29 | -import static org.jeecg.utils.HuahengJwtUtil.getRolesByToken; | |
30 | - | |
31 | - | |
32 | -/** | |
33 | - * 基于拦截器对数据脱敏 | |
34 | - * | |
35 | - * @author xub | |
36 | - * @date 2022/6/2 下午2:23 | |
37 | - */ | |
38 | 19 | @Intercepts({ |
39 | - @Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {java.sql.Statement.class}) | |
20 | + @Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class}) | |
40 | 21 | }) |
41 | -@Service | |
42 | -@Order(-1) | |
43 | -public class DesensitizationInterceptor implements Interceptor { | |
44 | - | |
22 | +public class DesensitizationResultSetInterceptor implements Interceptor { | |
45 | 23 | |
46 | - private static final Logger log = LoggerFactory.getLogger(DesensitizationInterceptor.class); | |
24 | + private static final Logger log = LoggerFactory.getLogger(DesensitizationResultSetInterceptor.class); | |
47 | 25 | |
48 | - /** | |
49 | - * key值为class对象 value可以理解成是该类带有SensitiveField注解的属性,只不过对属性封装了一层。 | |
50 | - * 它是非常能够提高性能的处理器 它的作用就是不用每一次一个对象经来都要看下它的哪些属性带有SensitiveField注解 | |
51 | - * 毕竟类的反射在性能上并不友好。只要key包含该对象那就不需要检查它哪些属性带SensitiveField注解。 | |
52 | - */ | |
53 | - private Map<Class, List<Handler>> handlerMap = new ConcurrentHashMap<>(); | |
26 | + private final Map<Class<?>, List<Handler>> handlerMap = new ConcurrentHashMap<>(); | |
54 | 27 | |
55 | 28 | @Override |
56 | 29 | public Object intercept(Invocation invocation) throws Throwable { |
57 | - // 获取结果 | |
58 | - List<Object> results = (List<Object>) invocation.proceed(); | |
59 | - if (CollectionUtils.isEmpty(results)) { | |
60 | - return results; | |
61 | - } | |
62 | - // 批量设置加密 | |
63 | - for (Object object : results) { | |
64 | - process(object); | |
30 | + Object result = invocation.proceed(); | |
31 | + | |
32 | + // 仅对查询结果集进行脱敏处理 | |
33 | + if (result instanceof List) { | |
34 | + List<?> results = (List<?>) result; | |
35 | + for (Object object : results) { | |
36 | + process(object); | |
37 | + } | |
65 | 38 | } |
66 | - return results; | |
39 | + return result; | |
67 | 40 | } |
68 | 41 | |
69 | - | |
70 | 42 | private void process(Object object) throws Throwable { |
71 | - if(ObjectUtil.isNotEmpty(object)){ | |
72 | - Class handlerKey = object.getClass(); | |
43 | + if (object != null) { | |
44 | + Class<?> handlerKey = object.getClass(); | |
73 | 45 | List<Handler> handlerList = handlerMap.get(handlerKey); |
74 | - //性能优化点,如果有两个都是user对象同时,那么只需有个进行反射处理属性就好了,另一个只需执行下面的for循环 | |
75 | - SYNC: | |
76 | 46 | if (handlerList == null) { |
77 | 47 | synchronized (this) { |
78 | 48 | handlerList = handlerMap.get(handlerKey); |
79 | - //如果到这里map集合已经存在,则跳出到指定SYNC标签 | |
80 | - if (handlerList != null) { | |
81 | - break SYNC; | |
82 | - } | |
83 | - handlerMap.put(handlerKey, handlerList = new ArrayList<>()); | |
84 | - // 反射工具类 获取带有SensitiveField注解的所有属性字段 | |
85 | - Set<Field> allFields = ReflectionUtils.getAllFields( | |
86 | - object.getClass(), | |
87 | - input -> input != null && input.getAnnotation(SensitiveField.class) != null | |
88 | - ); | |
89 | - | |
90 | - for (Field field : allFields) { | |
91 | - handlerList.add(new Handler(field)); | |
49 | + if (handlerList == null) { | |
50 | + handlerList = new ArrayList<>(); | |
51 | + Set<Field> allFields = ReflectionUtils.getAllFields( | |
52 | + object.getClass(), | |
53 | + input -> input != null && input.getAnnotation(SensitiveField.class) != null | |
54 | + ); | |
55 | + for (Field field : allFields) { | |
56 | + handlerList.add(new Handler(field)); | |
57 | + } | |
58 | + handlerMap.put(handlerKey, handlerList); | |
92 | 59 | } |
93 | 60 | } |
94 | 61 | } |
... | ... | @@ -96,10 +63,8 @@ public class DesensitizationInterceptor implements Interceptor { |
96 | 63 | handler.accept(object); |
97 | 64 | } |
98 | 65 | } |
99 | - | |
100 | 66 | } |
101 | 67 | |
102 | - | |
103 | 68 | @Override |
104 | 69 | public Object plugin(Object target) { |
105 | 70 | return Plugin.wrap(target, this); |
... | ... | @@ -109,35 +74,35 @@ public class DesensitizationInterceptor implements Interceptor { |
109 | 74 | public void setProperties(Properties properties) { |
110 | 75 | } |
111 | 76 | |
112 | - | |
113 | 77 | private static class Handler { |
114 | - Field field; | |
78 | + private final Field field; | |
79 | + private final SensitiveTypeEnums typeEnums; | |
80 | + private final String fillValue; | |
81 | + private final SensitiveStrategy sensitiveStrategy; | |
115 | 82 | |
116 | 83 | Handler(Field field) { |
117 | 84 | this.field = field; |
85 | + SensitiveField annotation = field.getAnnotation(SensitiveField.class); | |
86 | + this.typeEnums = annotation.value(); | |
87 | + this.fillValue = annotation.fillValue(); | |
88 | + this.sensitiveStrategy = SensitiveContext.get(typeEnums); | |
118 | 89 | } |
119 | 90 | |
120 | - private boolean checkField(Object object, Field field) throws IllegalAccessException { | |
91 | + private boolean checkField(Object object) throws IllegalAccessException { | |
121 | 92 | if (!field.isAccessible()) { |
122 | 93 | field.setAccessible(true); |
123 | 94 | } |
124 | - //如果为空 那么就不用进行脱敏操作了 | |
125 | 95 | return field.get(object) != null; |
126 | 96 | } |
127 | 97 | |
128 | 98 | public void accept(Object o) throws Throwable { |
129 | - if (checkField(o, field)) { | |
130 | - SensitiveField annotation = field.getAnnotation(SensitiveField.class); | |
131 | - SensitiveTypeEnums typeEnums = annotation.value(); | |
132 | - String fillValue = annotation.fillValue(); | |
133 | - Object o1 = field.get(o); | |
134 | - if(!SecurityUtils.getSubject().hasRole("desensitization")){ | |
135 | - SensitiveStrategy sensitiveStrategy = SensitiveContext.get(typeEnums); | |
136 | - String s = sensitiveStrategy.handle(o1, fillValue); | |
137 | - field.set(o, s); | |
99 | + if (checkField(o)) { | |
100 | + if (!SecurityUtils.getSubject().hasRole("desensitization")) { | |
101 | + Object fieldValue = field.get(o); | |
102 | + String maskedValue = sensitiveStrategy.handle(fieldValue, fillValue); | |
103 | + field.set(o, maskedValue); | |
138 | 104 | } |
139 | 105 | } |
140 | 106 | } |
141 | 107 | } |
142 | - | |
143 | 108 | } |
... | ... |
huaheng-wms-core/src/main/java/org/jeecg/modules/desensitization/plugin/MyBatisUpdateInterceptor.java
0 → 100644
1 | +package org.jeecg.modules.desensitization.plugin; | |
2 | + | |
3 | +import org.apache.ibatis.executor.Executor; | |
4 | +import org.apache.ibatis.mapping.MappedStatement; | |
5 | +import org.apache.ibatis.plugin.*; | |
6 | +import org.slf4j.Logger; | |
7 | +import org.slf4j.LoggerFactory; | |
8 | +import org.springframework.core.annotation.Order; | |
9 | +import org.springframework.stereotype.Service; | |
10 | + | |
11 | +import java.util.Properties; | |
12 | + | |
13 | +@Intercepts({ | |
14 | + @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}) | |
15 | +}) | |
16 | +@Service | |
17 | +@Order(-1) | |
18 | +public class MyBatisUpdateInterceptor implements Interceptor { | |
19 | + | |
20 | + | |
21 | + private static final Logger log = LoggerFactory.getLogger(MyBatisUpdateInterceptor.class); | |
22 | + | |
23 | + @Override | |
24 | + public Object intercept(Invocation invocation) throws Throwable { | |
25 | + // 在这里处理插入/更新/删除前后的操作 | |
26 | + return invocation.proceed(); | |
27 | + } | |
28 | + | |
29 | + @Override | |
30 | + public Object plugin(Object target) { | |
31 | + return Plugin.wrap(target, this); | |
32 | + } | |
33 | + | |
34 | + @Override | |
35 | + public void setProperties(Properties properties) { | |
36 | + } | |
37 | +} | |
... | ... |
huaheng-wms-core/src/main/java/org/jeecg/modules/system/entity/SysUser.java
huaheng-wms-core/src/main/resources/mybatis-config.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8" ?> | |
2 | +<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" | |
3 | + "http://mybatis.org/dtd/mybatis-3-config.dtd"> | |
4 | +<configuration> | |
5 | + | |
6 | + <!-- 其他 MyBatis 配置 --> | |
7 | + | |
8 | + <plugins> | |
9 | + <!-- 配置查询操作的脱敏拦截器 --> | |
10 | + <plugin interceptor="org.jeecg.modules.desensitization.plugin.DesensitizationResultSetInterceptor"/> | |
11 | + <!-- 配置插入、更新、删除操作的拦截器 --> | |
12 | + <plugin interceptor="org.jeecg.modules.desensitization.plugin.MyBatisUpdateInterceptor"/> | |
13 | + </plugins> | |
14 | + | |
15 | + <!-- 其他 MyBatis 配置 --> | |
16 | + | |
17 | +</configuration> | |
... | ... |