| | |
| | | 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; |
| | |
| | | @Slf4j |
| | | @ConditionalOnProperty(value = "domain.sensitive", havingValue = "true") |
| | | @Component |
| | | @Order(120) |
| | | public class SensitiveInterceptor implements Interceptor { |
| | | @SuppressWarnings("unchecked") |
| | | @Override |
| | |
| | | |
| | | |
| | | 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)) |
| | | .forEach(field -> doSensitive(metaObject, field)); |
| | | 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)); |
| | | } |
| | | } |
| | | |
| | | |
| | |
| | | // 获取对应的脱敏策略 并进行脱敏 |
| | | SensitiveStrategy type = annotation.strategy(); |
| | | Object o = type.getDesensitizer().apply((String) value); |
| | | log.debug("脱敏信息:{}->{}" , value, o.toString() ); |
| | | // 把脱敏后的值塞回去 |
| | | metaObject.setValue(name, o); |
| | | } |