desensitization-spring-boot-autoconfigure


License

License

Categories

Categories

Spring Boot Container Microservices Auto Application Layer Libs Code Generators config Configuration
GroupId

GroupId

red.zyc
ArtifactId

ArtifactId

desensitization-spring-boot-autoconfigure
Last Version

Last Version

1.0.4
Release Date

Release Date

Type

Type

jar
Description

Description

desensitization-spring-boot-autoconfigure
desensitization-spring-boot-autoconfigure

Download desensitization-spring-boot-autoconfigure

How to add to project

<!-- https://jarcasting.com/artifacts/red.zyc/desensitization-spring-boot-autoconfigure/ -->
<dependency>
    <groupId>red.zyc</groupId>
    <artifactId>desensitization-spring-boot-autoconfigure</artifactId>
    <version>1.0.4</version>
</dependency>
// https://jarcasting.com/artifacts/red.zyc/desensitization-spring-boot-autoconfigure/
implementation 'red.zyc:desensitization-spring-boot-autoconfigure:1.0.4'
// https://jarcasting.com/artifacts/red.zyc/desensitization-spring-boot-autoconfigure/
implementation ("red.zyc:desensitization-spring-boot-autoconfigure:1.0.4")
'red.zyc:desensitization-spring-boot-autoconfigure:jar:1.0.4'
<dependency org="red.zyc" name="desensitization-spring-boot-autoconfigure" rev="1.0.4">
  <artifact name="desensitization-spring-boot-autoconfigure" type="jar" />
</dependency>
@Grapes(
@Grab(group='red.zyc', module='desensitization-spring-boot-autoconfigure', version='1.0.4')
)
libraryDependencies += "red.zyc" % "desensitization-spring-boot-autoconfigure" % "1.0.4"
[red.zyc/desensitization-spring-boot-autoconfigure "1.0.4"]

Dependencies

compile (6)

Group / Artifact Type Version
red.zyc : desensitization jar 2.4.0
org.springframework.boot : spring-boot-autoconfigure jar
org.springframework.boot : spring-boot-configuration-processor Optional jar
org.springframework : spring-web jar
org.springframework : spring-aop jar
org.aspectj : aspectjweaver jar

Project Modules

There are no modules declared in this project.

desensitization-spring-boot

desensitization 库集成到spring-boot中实现数据自动脱敏。 实现原理是基于spring-aop对全局方法进行拦截脱敏处理,默认会对当前spring-boot工程启动类所在的包及其子包下所有需要脱敏处理的方法进行拦截。 当然你也可以在spring的配置文件中通过desensitization开头的配置参数编写自己的切点表达式或者编写一个名称为desensitizationAdvisor的Advisor 添加到spring上下文中以便更好地控制脱敏。

用法

jdk版本

大于等于1.8

maven依赖

<dependency>
  <groupId>red.zyc.boot</groupId>
  <artifactId>desensitization-spring-boot-starter</artifactId>
  <version>1.0.7</version>
</dependency>

注意

默认情况下只会对基于Spring内置的ResponseEntity类型返回值的方法进行必要的脱敏处理。而通常情况下我们系统中都会自定义一个类似的响应实体

@Getter
@Setter
public class CustomizedResponse<T> {

    private T data;

    private String code;

    private String message;

    public CustomizedResponse() {}

    public CustomizedResponse(T data, String code, String message) {
        this.data = data;
        this.code = code;
        this.message = message;
    }

}

对自定义类型进行脱敏处理时我们需要配置一个类型解析器来解析该类型

@Configuration
public class DesensitizationConfig {

    @Bean
    public TypeResolver<CustomizedResponse<Object>, AnnotatedParameterizedType> typeResolver() {
        return new CustomizedResponseTypeResolver();
    }

    public static class CustomizedResponseTypeResolver implements TypeResolver<CustomizedResponse<Object>, AnnotatedParameterizedType>, AopInfrastructureBean {

        private final int order = TypeResolvers.randomOrder();

        @Override
        public CustomizedResponse<Object> resolve(CustomizedResponse<Object> response, AnnotatedParameterizedType annotatedParameterizedType) {
            AnnotatedType typeArgument = annotatedParameterizedType.getAnnotatedActualTypeArguments()[0];
            Object erased = TypeResolvers.resolve(response.getData(), typeArgument);
            return new CustomizedResponse<>(erased, response.getMessage(), response.getCode());
        }

        @Override
        public boolean support(Object value, AnnotatedType annotatedType) {
            return value instanceof CustomizedResponse && annotatedType instanceof AnnotatedParameterizedType;
        }

        @Override
        public int order() {
            return order;
        }
    }
}

该配置是用来解析CustomizedResponse类型的对象,通常情况下我们只需要对响应的实际数据(data)进行脱敏即可。 将上面的类型解析器添加到Spring上下文中之后,接下来我们只需将脱敏注解标记到需要脱敏的方法返回对象的泛型参数上就能完成CustomizedResponse类型数据的自动脱敏处理。

例子

脱敏ResponseEntity类型的数据

  1. 需要脱敏的方法
  2. 测试用例

脱敏CustomizedResponse类型的数据

  1. 需要脱敏的方法
  2. 测试用例

License

Apache License 2.0

Versions

Version
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0