Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
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;
Expand Down Expand Up @@ -86,4 +90,21 @@ public static Map<String, Object> parseExpressions(JoinPoint joinPoint, List<Str
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);
}

}
5 changes: 5 additions & 0 deletions yudao-framework/yudao-spring-boot-starter-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@
* 比如:example@gmail.com 脱敏之后为 e****@gmail.com
*/
String replacer() default "$1****$2";

/**
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
*/
String condition() default "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@
* 脱敏后字符串 ******456789
*/
String replacer() default "******";

/**
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
*/
String condition() default "";

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand All @@ -35,4 +41,12 @@ public String desensitize(String origin, T annotation) {
*/
abstract String getReplacer(T annotation);

/**
* el 表达式
*
* @param annotation 注解信息
* @return el 表达式
*/
abstract String getCondition(T annotation);

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ String getRegex(RegexDesensitize annotation) {
String getReplacer(RegexDesensitize annotation) {
return annotation.replacer();
}

@Override
String getCondition(RegexDesensitize annotation) {
return annotation.condition();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ String getReplacer(EmailDesensitize annotation) {
return annotation.replacer();
}

@Override
String getCondition(EmailDesensitize annotation) {
return annotation.condition();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@
*/
String replacer() default "*";

/**
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
*/
String condition() default "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@
*/
String replacer() default "*";

/**
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
*/
String condition() default "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@
*/
String replacer() default "*";

/**
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
*/
String condition() default "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@
*/
String replacer() default "*";

/**
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
*/
String condition() default "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@
*/
String replacer() default "*";

/**
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
*/
String condition() default "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@
*/
String replacer() default "*";

/**
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
*/
String condition() default "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@
*/
String replacer() default "*";

/**
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
*/
String condition() default "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,10 @@
* 前缀保留长度
*/
int prefixKeep() default 0;

/**
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
*/
String condition() default "";

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -75,4 +81,12 @@ private String buildReplacerByLength(String replacer, int length) {
*/
abstract String getReplacer(T annotation);

/**
* el 表达式
*
* @param annotation 注解信息
* @return el 表达式
*/
abstract String getCondition(T annotation);

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ String getReplacer(BankCardDesensitize annotation) {
return annotation.replacer();
}

@Override
String getCondition(BankCardDesensitize annotation) {
return annotation.condition();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ Integer getSuffixKeep(CarLicenseDesensitize annotation) {
String getReplacer(CarLicenseDesensitize annotation) {
return annotation.replacer();
}

@Override
String getCondition(CarLicenseDesensitize annotation) {
return annotation.condition();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ String getReplacer(ChineseNameDesensitize annotation) {
return annotation.replacer();
}

@Override
String getCondition(ChineseNameDesensitize annotation) {
return annotation.condition();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ Integer getSuffixKeep(SliderDesensitize annotation) {
String getReplacer(SliderDesensitize annotation) {
return annotation.replacer();
}

@Override
String getCondition(SliderDesensitize annotation) {
return annotation.condition();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ Integer getSuffixKeep(FixedPhoneDesensitize annotation) {
String getReplacer(FixedPhoneDesensitize annotation) {
return annotation.replacer();
}

@Override
String getCondition(FixedPhoneDesensitize annotation) {
return annotation.condition();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ Integer getSuffixKeep(IdCardDesensitize annotation) {
String getReplacer(IdCardDesensitize annotation) {
return annotation.replacer();
}

@Override
String getCondition(IdCardDesensitize annotation) {
return annotation.condition();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ Integer getSuffixKeep(MobileDesensitize annotation) {
String getReplacer(MobileDesensitize annotation) {
return annotation.replacer();
}

@Override
String getCondition(MobileDesensitize annotation) {
return annotation.condition();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ Integer getSuffixKeep(PasswordDesensitize annotation) {
String getReplacer(PasswordDesensitize annotation) {
return annotation.replacer();
}

@Override
String getCondition(PasswordDesensitize annotation) {
return annotation.condition();
}

}