Code Monkey home page Code Monkey logo

desensitization-spring-boot's Introduction

desensitization-spring-boot

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

用法

spring-boot版本

3.1.5

maven依赖

<dependency>
    <groupId>red.zyc.boot</groupId>
    <artifactId>desensitization-spring-boot-starter</artifactId>
    <version>2.0.0</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 TypeParser<CustomizedResponse<Object>, AnnotatedParameterizedType> typeParser() {
        return new CustomizedResponseTypeParser();
    }
    
    public static class CustomizedResponseTypeParser implements TypeParser<CustomizedResponse<Object>, AnnotatedParameterizedType>, AopInfrastructureBean {

        private final int order = AnnotationParser.randomOrder();

        @Override
        public CustomizedResponse<Object> parse(CustomizedResponse<Object> response, AnnotatedParameterizedType annotatedParameterizedType) {
            AnnotatedType typeArgument = annotatedParameterizedType.getAnnotatedActualTypeArguments()[0];
            Object erased = AnnotationParser.parse(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

desensitization-spring-boot's People

Contributors

allurx avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.