feat: 添加 spring-boot-desensitize starter.
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.orion.ops</groupId>
|
||||
<artifactId>orion-ops-framework</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>orion-ops-spring-boot-starter-desensitize</artifactId>
|
||||
<name>${project.artifactId}</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<description>项目数据脱敏包</description>
|
||||
<url>https://github.com/lijiahangmax/orion-ops-pro</url>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.orion.ops</groupId>
|
||||
<artifactId>orion-ops-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.orion.ops.framework.desensitize.config;
|
||||
|
||||
import com.orion.ops.framework.desensitize.core.handler.DesensitizeResultHandler;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* 数据脱敏置类
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/6/29 16:55
|
||||
*/
|
||||
@AutoConfiguration
|
||||
public class OrionDesensitizeAutoConfiguration {
|
||||
|
||||
/**
|
||||
* @return 返回结果脱敏处理器
|
||||
*/
|
||||
@Bean
|
||||
public DesensitizeResultHandler desensitizeResultHandler() {
|
||||
return new DesensitizeResultHandler();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.orion.ops.framework.desensitize.core.handler;
|
||||
|
||||
import com.orion.lang.define.wrapper.Wrapper;
|
||||
import com.orion.ops.framework.common.constant.ResponseAdviceOrderConst;
|
||||
import com.orion.ops.framework.common.annotation.DoDesensitize;
|
||||
import com.orion.ops.framework.desensitize.core.processor.IDesensitizeProcessor;
|
||||
import com.orion.ops.framework.desensitize.core.processor.ObjectDesensitizeProcessor;
|
||||
import com.orion.ops.framework.desensitize.core.processor.WrapperDesensitizeProcessor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
import org.springframework.http.server.ServerHttpResponse;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
||||
|
||||
/**
|
||||
* 返回结果脱敏处理器
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/6/15 17:38
|
||||
*/
|
||||
@Order(ResponseAdviceOrderConst.DESENSITIZE)
|
||||
@ControllerAdvice
|
||||
public class DesensitizeResultHandler implements ResponseBodyAdvice<Object> {
|
||||
|
||||
@Override
|
||||
public boolean supports(MethodParameter methodParameter, @NotNull Class converterType) {
|
||||
return methodParameter.hasMethodAnnotation(DoDesensitize.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object beforeBodyWrite(Object body, @NotNull MethodParameter methodParameter, @NotNull MediaType selectedContentType, @NotNull Class selectedConverterType,
|
||||
@NotNull ServerHttpRequest request, @NotNull ServerHttpResponse response) {
|
||||
IDesensitizeProcessor processor;
|
||||
if (body instanceof Wrapper<?>) {
|
||||
// wrapper
|
||||
processor = new WrapperDesensitizeProcessor();
|
||||
} else {
|
||||
// 普通对象
|
||||
processor = new ObjectDesensitizeProcessor();
|
||||
}
|
||||
// 执行脱敏
|
||||
processor.execute(body);
|
||||
return body;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.orion.ops.framework.desensitize.core.processor;
|
||||
|
||||
/**
|
||||
* 脱敏处理器
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/6/29 17:50
|
||||
*/
|
||||
public interface IDesensitizeProcessor<T> {
|
||||
|
||||
/**
|
||||
* 脱敏操作
|
||||
*
|
||||
* @param data data
|
||||
*/
|
||||
void execute(T data);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.orion.ops.framework.desensitize.core.processor;
|
||||
|
||||
import com.orion.lang.utils.reflect.Annotations;
|
||||
import com.orion.lang.utils.reflect.Methods;
|
||||
import com.orion.ops.framework.common.utils.Desensitizes;
|
||||
import com.orion.ops.framework.common.annotation.Desensitize;
|
||||
import com.orion.ops.framework.common.annotation.DoDesensitize;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 对象脱敏处理器
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/6/29 17:21
|
||||
*/
|
||||
public class ObjectDesensitizeProcessor implements IDesensitizeProcessor<Object> {
|
||||
|
||||
@Override
|
||||
public void execute(Object data) {
|
||||
if (data == null) {
|
||||
return;
|
||||
}
|
||||
Class<?> dataClass = data.getClass();
|
||||
// 获取被标注脱敏的字段
|
||||
Map<Field, Desensitize> annotatedFields = Annotations.getAnnotatedFields(dataClass, Desensitize.class, true);
|
||||
annotatedFields.forEach((f, d) -> {
|
||||
// 获取 getter 方法
|
||||
Method getter = Methods.getGetterMethodByCache(dataClass, f.getName());
|
||||
if (!getter.getReturnType().equals(String.class)) {
|
||||
// 非 string 不进行脱敏操作
|
||||
return;
|
||||
}
|
||||
// 调用 getter 方法
|
||||
String value = Methods.invokeMethod(data, getter);
|
||||
if (value == null) {
|
||||
// 为 null 不进行脱敏操作
|
||||
return;
|
||||
}
|
||||
// 数据脱敏
|
||||
String desensitizeValue = this.valueDesensitize(value, d);
|
||||
// 获取 setter 方法
|
||||
Method setter = Methods.getSetterMethodByCache(dataClass, f.getName());
|
||||
// 调用 setter 方法
|
||||
Methods.invokeMethod(data, setter, desensitizeValue);
|
||||
});
|
||||
// 获取被标注脱敏字段的对象
|
||||
Map<Field, DoDesensitize> annotatedObjects = Annotations.getAnnotatedFields(dataClass, DoDesensitize.class, true);
|
||||
annotatedObjects.forEach((f, d) -> {
|
||||
// 获取 getter 方法
|
||||
Method getter = Methods.getGetterMethodByCache(dataClass, f.getName());
|
||||
// 调用 getter 方法
|
||||
Object value = Methods.invokeMethod(data, getter);
|
||||
if (value == null) {
|
||||
// 为 null 不进行脱敏操作
|
||||
return;
|
||||
}
|
||||
// 执行对象脱敏操作
|
||||
this.execute(value);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据脱敏
|
||||
*
|
||||
* @param value value
|
||||
* @param c 配置
|
||||
* @return 脱敏字符
|
||||
*/
|
||||
private String valueDesensitize(String value, Desensitize c) {
|
||||
return Desensitizes.mix(value, c.keepStart(), c.keepEnd(), c.replacer());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.orion.ops.framework.desensitize.core.processor;
|
||||
|
||||
import com.orion.lang.define.wrapper.HttpWrapper;
|
||||
import com.orion.lang.define.wrapper.RpcWrapper;
|
||||
|
||||
/**
|
||||
* wrapper 对象脱敏处理器
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/6/29 18:17
|
||||
*/
|
||||
public class WrapperDesensitizeProcessor extends ObjectDesensitizeProcessor {
|
||||
|
||||
@Override
|
||||
public void execute(Object data) {
|
||||
if (data instanceof HttpWrapper<?>) {
|
||||
super.execute(((HttpWrapper<?>) data).getData());
|
||||
} else if (data instanceof RpcWrapper<?>) {
|
||||
super.execute(data);
|
||||
} else {
|
||||
super.execute(data);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
com.orion.ops.framework.desensitize.config.OrionDesensitizeAutoConfiguration
|
||||
Reference in New Issue
Block a user