Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/allurx/blur-spring-boot
Integration of the blur library with Spring Boot
https://github.com/allurx/blur-spring-boot
aop desensitization spring-boot
Last synced: 4 days ago
JSON representation
Integration of the blur library with Spring Boot
- Host: GitHub
- URL: https://github.com/allurx/blur-spring-boot
- Owner: allurx
- License: apache-2.0
- Created: 2019-12-04T14:41:12.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-11-08T04:58:44.000Z (about 2 months ago)
- Last Synced: 2024-12-10T14:32:13.133Z (13 days ago)
- Topics: aop, desensitization, spring-boot
- Language: Java
- Homepage:
- Size: 106 KB
- Stars: 40
- Watchers: 2
- Forks: 12
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# blur-spring-boot
This project integrates the [blur](https://github.com/allurx/blur) library into Spring Boot to enable automatic data blurring. Using Spring AOP, this library globally intercepts targeted methods to apply blurring (data masking) by default on all methods within the package of the Spring Boot application's main class and its sub-packages.
To further customize the blurring, you can specify a custom pointcut expression using `blur`-prefixed parameters in the Spring configuration file. Alternatively, you can define an `Advisor` named `blurAdvisor` in the Spring context to gain more granular control over the blurring process.
# Usage
## Spring Boot Version
3.3.5
## Maven Dependency
```xml
io.allurx
blur-spring-boot-starter
${latest version}```
## Notes
By default, this library enables blurring for methods returning Spring’s `ResponseEntity` type only. If your application uses a custom response entity, such as:
```java
public class CustomizedResponse {public T data;
public String code;
public String message;
public CustomizedResponse() {}
public CustomizedResponse(T data, String code, String message) {
this.data = data;
this.code = code;
this.message = message;
}}
```
then you’ll need to configure a type parser to handle custom types for blurring.
```java
@Configuration
public class BlurConfig {/**
* Registers the {@code CustomizedResponseTypeParser} as a bean in the Spring context.
*
* @return {@link CustomizedResponseTypeParser}
*/
@Bean
public TypeParser, AnnotatedParameterizedType> typeParser() {
return new CustomizedResponseTypeParser();
}/**
* Custom type parser to handle {@code CustomizedResponse} type data.
*/
public static class CustomizedResponseTypeParser
implements TypeParser, AnnotatedParameterizedType>, AopInfrastructureBean {private final int order = AnnotationParser.randomOrder();
@Override
public CustomizedResponse parse(CustomizedResponse response, AnnotatedParameterizedType type) {
AnnotatedType typeArgument = type.getAnnotatedActualTypeArguments()[0];
Object blurredData = AnnotationParser.parse(response.getData(), typeArgument);
return new CustomizedResponse<>(blurredData, 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;
}
}
}
```
This configuration blurs `CustomizedResponse` type objects,
typically applying blurring only to the actual data (`data`) within the response body.
After adding this type parser to the Spring context,
you need only annotate the generic parameter of the return object in the method with the blurring annotation to enable automatic blurring for `CustomizedResponse` type data.## Examples
### Blurring `ResponseEntity` Type Data
1. [Blurred method example](blur-spring-boot-samples/blur-spring-boot-sample-web/src/main/java/io/allurx/blur/spring/boot/sample/web/controller/ResponseEntityBlurController.java)
2. [Related test cases](blur-spring-boot-samples/blur-spring-boot-sample-web/src/test/java/io/allurx/blur/spring/boot/sample/web/test/ResponseEntityBlurTest.java)### Blurring `CustomizedResponse` Type Data
1. [Blurred method example](blur-spring-boot-samples/blur-spring-boot-sample-web/src/main/java/io/allurx/blur/spring/boot/sample/web/controller/CustomizedResponseBlurController.java)
2. [Related test cases](blur-spring-boot-samples/blur-spring-boot-sample-web/src/test/java/io/allurx/blur/spring/boot/sample/web/test/CustomizedResponseBlurTest.java)# License
[Apache License 2.0](LICENSE.txt)