【优化】数据脱敏支持 Spring el 表达式,支持根据权限控制脱敏

This commit is contained in:
zhougang 2024-07-31 22:43:48 +08:00
parent 41ee237161
commit 08e1c69d10
24 changed files with 164 additions and 0 deletions

View File

@ -3,11 +3,15 @@ package cn.iocoder.yudao.framework.common.util.spring;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
@ -86,4 +90,21 @@ public class SpringExpressionUtils {
return result;
}
/**
* Bean 工厂解析 EL 表达式的结果
*
* @param beanFactory Bean 工程
* @param expressionString EL 表达式
* @return 执行界面
*/
public static Object parseExpression(BeanFactory beanFactory, String expressionString) {
if (StrUtil.isBlank(expressionString)) {
return null;
}
Expression expression = EXPRESSION_PARSER.parseExpression(expressionString);
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new BeanFactoryResolver(beanFactory));
return expression.getValue(context);
}
}

View File

@ -32,6 +32,11 @@
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<scope>provided</scope> <!-- 解决工具类 SpringExpressionUtils 加载的时候访问不到 org.aspectj.lang.JoinPoint 问题 -->
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>

View File

@ -33,4 +33,10 @@ public @interface EmailDesensitize {
* 比如example@gmail.com 脱敏之后为 e****@gmail.com
*/
String replacer() default "$1****$2";
/**
* el 表达式当执行 condition 返回 true 的时候跳过脱敏
*/
String condition() default "";
}

View File

@ -35,4 +35,10 @@ public @interface RegexDesensitize {
* 脱敏后字符串 ******456789
*/
String replacer() default "******";
/**
* el 表达式当执行 condition 返回 true 的时候跳过脱敏
*/
String condition() default "";
}

View File

@ -1,5 +1,7 @@
package cn.iocoder.yudao.framework.desensitize.core.regex.handler;
import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.common.util.spring.SpringExpressionUtils;
import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler;
import java.lang.annotation.Annotation;
@ -14,6 +16,10 @@ public abstract class AbstractRegexDesensitizationHandler<T extends Annotation>
@Override
public String desensitize(String origin, T annotation) {
Object expressionResult = SpringExpressionUtils.parseExpression(SpringUtil.getApplicationContext(), getCondition(annotation));
if (expressionResult instanceof Boolean && (Boolean) expressionResult) {
return origin;
}
String regex = getRegex(annotation);
String replacer = getReplacer(annotation);
return origin.replaceAll(regex, replacer);
@ -35,4 +41,12 @@ public abstract class AbstractRegexDesensitizationHandler<T extends Annotation>
*/
abstract String getReplacer(T annotation);
/**
* el 表达式
*
* @param annotation 注解信息
* @return el 表达式
*/
abstract String getCondition(T annotation);
}

View File

@ -18,4 +18,10 @@ public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitiza
String getReplacer(RegexDesensitize annotation) {
return annotation.replacer();
}
@Override
String getCondition(RegexDesensitize annotation) {
return annotation.condition();
}
}

View File

@ -19,4 +19,9 @@ public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHan
return annotation.replacer();
}
@Override
String getCondition(EmailDesensitize annotation) {
return annotation.condition();
}
}

View File

@ -37,4 +37,9 @@ public @interface BankCardDesensitize {
*/
String replacer() default "*";
/**
* el 表达式当执行 condition 返回 true 的时候跳过脱敏
*/
String condition() default "";
}

View File

@ -37,4 +37,9 @@ public @interface CarLicenseDesensitize {
*/
String replacer() default "*";
/**
* el 表达式当执行 condition 返回 true 的时候跳过脱敏
*/
String condition() default "";
}

View File

@ -37,4 +37,9 @@ public @interface ChineseNameDesensitize {
*/
String replacer() default "*";
/**
* el 表达式当执行 condition 返回 true 的时候跳过脱敏
*/
String condition() default "";
}

View File

@ -37,4 +37,9 @@ public @interface FixedPhoneDesensitize {
*/
String replacer() default "*";
/**
* el 表达式当执行 condition 返回 true 的时候跳过脱敏
*/
String condition() default "";
}

View File

@ -37,4 +37,9 @@ public @interface IdCardDesensitize {
*/
String replacer() default "*";
/**
* el 表达式当执行 condition 返回 true 的时候跳过脱敏
*/
String condition() default "";
}

View File

@ -37,4 +37,9 @@ public @interface MobileDesensitize {
*/
String replacer() default "*";
/**
* el 表达式当执行 condition 返回 true 的时候跳过脱敏
*/
String condition() default "";
}

View File

@ -39,4 +39,9 @@ public @interface PasswordDesensitize {
*/
String replacer() default "*";
/**
* el 表达式当执行 condition 返回 true 的时候跳过脱敏
*/
String condition() default "";
}

View File

@ -40,4 +40,10 @@ public @interface SliderDesensitize {
* 前缀保留长度
*/
int prefixKeep() default 0;
/**
* el 表达式当执行 condition 返回 true 的时候跳过脱敏
*/
String condition() default "";
}

View File

@ -1,5 +1,7 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.common.util.spring.SpringExpressionUtils;
import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler;
import java.lang.annotation.Annotation;
@ -14,6 +16,10 @@ public abstract class AbstractSliderDesensitizationHandler<T extends Annotation>
@Override
public String desensitize(String origin, T annotation) {
Object expressionResult = SpringExpressionUtils.parseExpression(SpringUtil.getApplicationContext(), getCondition(annotation));
if (expressionResult instanceof Boolean && (Boolean) expressionResult) {
return origin;
}
int prefixKeep = getPrefixKeep(annotation);
int suffixKeep = getSuffixKeep(annotation);
String replacer = getReplacer(annotation);
@ -75,4 +81,12 @@ public abstract class AbstractSliderDesensitizationHandler<T extends Annotation>
*/
abstract String getReplacer(T annotation);
/**
* el 表达式
*
* @param annotation 注解信息
* @return el 表达式
*/
abstract String getCondition(T annotation);
}

View File

@ -24,4 +24,9 @@ public class BankCardDesensitization extends AbstractSliderDesensitizationHandle
return annotation.replacer();
}
@Override
String getCondition(BankCardDesensitize annotation) {
return annotation.condition();
}
}

View File

@ -22,4 +22,10 @@ public class CarLicenseDesensitization extends AbstractSliderDesensitizationHand
String getReplacer(CarLicenseDesensitize annotation) {
return annotation.replacer();
}
@Override
String getCondition(CarLicenseDesensitize annotation) {
return annotation.condition();
}
}

View File

@ -24,4 +24,9 @@ public class ChineseNameDesensitization extends AbstractSliderDesensitizationHan
return annotation.replacer();
}
@Override
String getCondition(ChineseNameDesensitize annotation) {
return annotation.condition();
}
}

View File

@ -22,4 +22,10 @@ public class DefaultDesensitizationHandler extends AbstractSliderDesensitization
String getReplacer(SliderDesensitize annotation) {
return annotation.replacer();
}
@Override
String getCondition(SliderDesensitize annotation) {
return annotation.condition();
}
}

View File

@ -22,4 +22,10 @@ public class FixedPhoneDesensitization extends AbstractSliderDesensitizationHand
String getReplacer(FixedPhoneDesensitize annotation) {
return annotation.replacer();
}
@Override
String getCondition(FixedPhoneDesensitize annotation) {
return annotation.condition();
}
}

View File

@ -22,4 +22,10 @@ public class IdCardDesensitization extends AbstractSliderDesensitizationHandler<
String getReplacer(IdCardDesensitize annotation) {
return annotation.replacer();
}
@Override
String getCondition(IdCardDesensitize annotation) {
return annotation.condition();
}
}

View File

@ -23,4 +23,10 @@ public class MobileDesensitization extends AbstractSliderDesensitizationHandler<
String getReplacer(MobileDesensitize annotation) {
return annotation.replacer();
}
@Override
String getCondition(MobileDesensitize annotation) {
return annotation.condition();
}
}

View File

@ -22,4 +22,10 @@ public class PasswordDesensitization extends AbstractSliderDesensitizationHandle
String getReplacer(PasswordDesensitize annotation) {
return annotation.replacer();
}
@Override
String getCondition(PasswordDesensitize annotation) {
return annotation.condition();
}
}