From 4feaef5e35f0dad7cc3f05e32fc7e3e52fa9396d Mon Sep 17 00:00:00 2001
From: puhanshu <a9236326>
Date: 星期四, 10 三月 2022 17:17:14 +0800
Subject: [PATCH] 随手拍/微心愿/志愿者修改
---
springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/model/helper/sensitive/SensitiveInterceptor.java | 48 +++++++++++++++++++++++++-----------------------
1 files changed, 25 insertions(+), 23 deletions(-)
diff --git a/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/model/helper/sensitive/SensitiveInterceptor.java b/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/model/helper/sensitive/SensitiveInterceptor.java
index 40901c1..80f7b0f 100644
--- a/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/model/helper/sensitive/SensitiveInterceptor.java
+++ b/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/model/helper/sensitive/SensitiveInterceptor.java
@@ -1,8 +1,10 @@
package com.panzhihua.service_community.model.helper.sensitive;
-import com.panzhihua.common.model.helper.sensitive.Sensitive;
-import com.panzhihua.common.model.helper.sensitive.SensitiveStrategy;
-import lombok.extern.slf4j.Slf4j;
+import java.lang.reflect.Field;
+import java.sql.Statement;
+import java.util.List;
+import java.util.stream.Stream;
+
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
@@ -11,59 +13,59 @@
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
-import java.lang.reflect.Field;
-import java.sql.Statement;
-import java.util.List;
-import java.util.stream.Stream;
+import com.panzhihua.common.model.helper.sensitive.Sensitive;
+import com.panzhihua.common.model.helper.sensitive.SensitiveStrategy;
+
+import lombok.extern.slf4j.Slf4j;
/**
* 字段脱敏拦截器
+ *
* @author cedoo
* @since 2021-4-1 10:40:52
*/
-@Intercepts(@Signature(type = ResultSetHandler.class,
- method = "handleResultSets",
- args = {Statement.class}))
+@Intercepts(@Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class}))
@Slf4j
@ConditionalOnProperty(value = "domain.sensitive", havingValue = "true")
@Component
+@Order(120)
public class SensitiveInterceptor implements Interceptor {
@SuppressWarnings("unchecked")
@Override
public Object intercept(Invocation invocation) throws Throwable {
- //log.debug("脱敏拦截器");
- List<Object> records = (List<Object>) invocation.proceed();
+ // log.debug("脱敏拦截器");
+ List<Object> records = (List<Object>)invocation.proceed();
// 对结果集脱敏
records.forEach(this::sensitive);
return records;
}
-
private void sensitive(Object source) {
- // 拿到返回值类型
- Class<?> sourceClass = source.getClass();
- // 初始化返回值类型的 MetaObject
- MetaObject metaObject = SystemMetaObject.forObject(source);
- // 捕捉到属性上的标记注解 @Sensitive 并进行对应的脱敏处理
- Stream.of(sourceClass.getDeclaredFields())
- .filter(field -> field.isAnnotationPresent(Sensitive.class))
+ if (source != null) {
+ // 拿到返回值类型
+ Class<?> sourceClass = source.getClass();
+ // 初始化返回值类型的 MetaObject
+ MetaObject metaObject = SystemMetaObject.forObject(source);
+ // 捕捉到属性上的标记注解 @Sensitive 并进行对应的脱敏处理
+ Stream.of(sourceClass.getDeclaredFields()).filter(field -> field.isAnnotationPresent(Sensitive.class))
.forEach(field -> doSensitive(metaObject, field));
+ }
}
-
private void doSensitive(MetaObject metaObject, Field field) {
// 拿到属性名
String name = field.getName();
// 获取属性值
Object value = metaObject.getValue(name);
- // 只有字符串类型才能脱敏 而且不能为null
+ // 只有字符串类型才能脱敏 而且不能为null
if (String.class == metaObject.getGetterType(name) && value != null) {
Sensitive annotation = field.getAnnotation(Sensitive.class);
// 获取对应的脱敏策略 并进行脱敏
SensitiveStrategy type = annotation.strategy();
- Object o = type.getDesensitizer().apply((String) value);
+ Object o = type.getDesensitizer().apply((String)value);
// 把脱敏后的值塞回去
metaObject.setValue(name, o);
}
--
Gitblit v1.7.1