-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Renamed groupId from 'red.zyc' to 'io.allurx' and updated package str…
…ucture accordingly.
- Loading branch information
Showing
42 changed files
with
1,398 additions
and
836 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...nfigure/src/main/java/io/allurx/blur/spring/boot/autoconfigure/BlurAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright 2024 allurx | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.allurx.blur.spring.boot.autoconfigure; | ||
|
||
import io.allurx.annotation.parser.type.TypeParser; | ||
import org.springframework.aop.Advisor; | ||
import org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.util.Assert; | ||
|
||
import java.lang.reflect.AnnotatedParameterizedType; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Autoconfiguration class for enabling blur functionality, which includes data masking, | ||
* obfuscating, and anonymizing capabilities. Configures default beans if not already defined. | ||
* <p> | ||
* This configuration class loads properties from {@link BlurProperties} and sets up | ||
* aspect-oriented data handling for specified pointcuts. | ||
* </p> | ||
* | ||
* @author allurx | ||
*/ | ||
@AutoConfiguration | ||
@EnableConfigurationProperties(BlurProperties.class) | ||
public class BlurAutoConfiguration { | ||
|
||
private static final String BLUR_ADVISOR = "blurAdvisor"; | ||
private final BlurProperties blurProperties; | ||
static final ThreadLocal<SpringApplication> SPRING_APPLICATION_HOLDER = new ThreadLocal<>(); | ||
|
||
/** | ||
* Constructor for {@link BlurAutoConfiguration}. | ||
* | ||
* @param blurProperties properties configuration for blur functionality | ||
*/ | ||
public BlurAutoConfiguration(BlurProperties blurProperties) { | ||
this.blurProperties = blurProperties; | ||
} | ||
|
||
/** | ||
* Defines a bean for the blur advisor, which applies data masking and obfuscation | ||
* advice to methods matched by the pointcut expression. | ||
* | ||
* @return a configured {@link Advisor} with pointcut and advice set up | ||
*/ | ||
@Bean | ||
@ConditionalOnMissingBean(name = BLUR_ADVISOR) | ||
public Advisor blurAdvisor() { | ||
AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor(); | ||
advisor.setAdvice(new BlurMethodInterceptor()); | ||
advisor.setExpression(pointcutExpression()); | ||
return advisor; | ||
} | ||
|
||
/** | ||
* Registers a type parser bean for {@link ResponseEntity} to handle annotated type | ||
* resolution during runtime, enabling enhanced blur functionality. | ||
* | ||
* @return a {@link TypeParser} implementation for {@link ResponseEntity} types | ||
*/ | ||
@Bean | ||
public TypeParser<ResponseEntity<Object>, AnnotatedParameterizedType> responseEntityResolver() { | ||
return new ResponseEntityTypeParser(); | ||
} | ||
|
||
/** | ||
* Retrieves the pointcut expression used for data blur application. If a custom | ||
* expression is not configured, defaults to targeting all methods in the main application package. | ||
* | ||
* @return the pointcut expression string for data blur operations | ||
*/ | ||
private String pointcutExpression() { | ||
SpringApplication springApplication = SPRING_APPLICATION_HOLDER.get(); | ||
Assert.notNull(springApplication, () -> "Failed to retrieve SpringApplication. The current project may not be a Spring Boot application."); | ||
return Optional.ofNullable(blurProperties.getPointcutExpression()) | ||
.orElse("execution(* " + springApplication.getMainApplicationClass().getPackage().getName() + "..*.*(..))"); | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
...nfigure/src/main/java/io/allurx/blur/spring/boot/autoconfigure/BlurMethodInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright 2024 allurx | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.allurx.blur.spring.boot.autoconfigure; | ||
|
||
import io.allurx.annotation.parser.handler.Parse; | ||
import io.allurx.annotation.parser.type.Cascade; | ||
import io.allurx.blur.Blur; | ||
import io.allurx.kit.base.reflection.AnnotatedTypeToken; | ||
import org.aopalliance.intercept.MethodInterceptor; | ||
import org.aopalliance.intercept.MethodInvocation; | ||
|
||
import java.lang.reflect.AnnotatedArrayType; | ||
import java.lang.reflect.AnnotatedParameterizedType; | ||
import java.lang.reflect.AnnotatedType; | ||
import java.lang.reflect.AnnotatedTypeVariable; | ||
import java.lang.reflect.AnnotatedWildcardType; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Parameter; | ||
import java.util.Arrays; | ||
import java.util.stream.IntStream; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Interceptor for applying data blur functionality on method arguments and return values. | ||
* This interceptor applies masking, obfuscation, and anonymization based on annotation-driven rules. | ||
* <p> | ||
* It dynamically checks for blur-related annotations on method parameters and return types, | ||
* ensuring only annotated data is processed, which optimizes performance by avoiding unnecessary processing. | ||
* </p> | ||
* | ||
* @author allurx | ||
* @see Blur | ||
* @see MethodInterceptor | ||
* @see AnnotatedTypeToken | ||
* @see Parse | ||
* @see Cascade | ||
*/ | ||
public class BlurMethodInterceptor implements MethodInterceptor { | ||
|
||
|
||
/** | ||
* Default constructor | ||
*/ | ||
public BlurMethodInterceptor() { | ||
} | ||
|
||
@Override | ||
public Object invoke(MethodInvocation invocation) throws Throwable { | ||
Method method = invocation.getMethod(); | ||
Object[] arguments = invocation.getArguments(); | ||
Parameter[] parameters = method.getParameters(); | ||
|
||
IntStream.range(0, parameters.length) | ||
.filter(i -> requiresBlur(parameters[i].getAnnotatedType())) | ||
.forEach(i -> arguments[i] = Blur.blur(arguments[i], AnnotatedTypeToken.of(parameters[i].getAnnotatedType()))); | ||
|
||
Object proceed = invocation.proceed(); | ||
AnnotatedType returnType = method.getAnnotatedReturnType(); | ||
|
||
return requiresBlur(returnType) ? Blur.blur(proceed, AnnotatedTypeToken.of(returnType)) : proceed; | ||
} | ||
|
||
/** | ||
* Determines if an object, identified by its {@link AnnotatedType}, requires blurring. | ||
* Objects marked with blur-related annotations are processed, while others are skipped | ||
* to maintain performance by avoiding redundant processing or object creation. | ||
* <p> | ||
* Although using a dedicated {@code @Blur} annotation might simplify identification, | ||
* this method avoids extra annotations by dynamically analyzing the presence of blur-triggering annotations. | ||
* </p> | ||
* | ||
* @param annotatedType the {@link AnnotatedType} of the object to evaluate | ||
* @return {@code true} if the object requires blurring, {@code false} otherwise | ||
*/ | ||
private boolean requiresBlur(AnnotatedType annotatedType) { | ||
return Arrays.stream(annotatedType.getDeclaredAnnotations()).anyMatch(annotation -> annotation.annotationType().isAnnotationPresent(Parse.class)) || | ||
annotatedType.getDeclaredAnnotation(Cascade.class) != null || | ||
switch (annotatedType) { | ||
case AnnotatedTypeVariable annotatedTypeVariable -> | ||
Arrays.stream(annotatedTypeVariable.getAnnotatedBounds()).anyMatch(this::requiresBlur); | ||
case AnnotatedWildcardType annotatedWildcardType -> | ||
Stream.of(annotatedWildcardType.getAnnotatedUpperBounds(), annotatedWildcardType.getAnnotatedLowerBounds()) | ||
.flatMap(Arrays::stream) | ||
.anyMatch(this::requiresBlur); | ||
case AnnotatedParameterizedType annotatedParameterizedType -> | ||
Arrays.stream(annotatedParameterizedType.getAnnotatedActualTypeArguments()) | ||
.anyMatch(this::requiresBlur); | ||
case AnnotatedArrayType annotatedArrayType -> | ||
requiresBlur(annotatedArrayType.getAnnotatedGenericComponentType()); | ||
default -> false; | ||
}; | ||
} | ||
|
||
} |
Oops, something went wrong.