mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-22 23:31:52 +08:00
【优化】数据脱敏支持 Spring el 表达式,支持根据权限控制脱敏
This commit is contained in:
parent
41ee237161
commit
08e1c69d10
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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>
|
||||
|
@ -33,4 +33,10 @@ public @interface EmailDesensitize {
|
||||
* 比如:example@gmail.com 脱敏之后为 e****@gmail.com
|
||||
*/
|
||||
String replacer() default "$1****$2";
|
||||
|
||||
/**
|
||||
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
|
||||
*/
|
||||
String condition() default "";
|
||||
|
||||
}
|
||||
|
@ -35,4 +35,10 @@ public @interface RegexDesensitize {
|
||||
* 脱敏后字符串 ******456789
|
||||
*/
|
||||
String replacer() default "******";
|
||||
|
||||
/**
|
||||
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
|
||||
*/
|
||||
String condition() default "";
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
}
|
||||
|
@ -18,4 +18,10 @@ public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitiza
|
||||
String getReplacer(RegexDesensitize annotation) {
|
||||
return annotation.replacer();
|
||||
}
|
||||
|
||||
@Override
|
||||
String getCondition(RegexDesensitize annotation) {
|
||||
return annotation.condition();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -19,4 +19,9 @@ public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHan
|
||||
return annotation.replacer();
|
||||
}
|
||||
|
||||
@Override
|
||||
String getCondition(EmailDesensitize annotation) {
|
||||
return annotation.condition();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -37,4 +37,9 @@ public @interface BankCardDesensitize {
|
||||
*/
|
||||
String replacer() default "*";
|
||||
|
||||
/**
|
||||
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
|
||||
*/
|
||||
String condition() default "";
|
||||
|
||||
}
|
||||
|
@ -37,4 +37,9 @@ public @interface CarLicenseDesensitize {
|
||||
*/
|
||||
String replacer() default "*";
|
||||
|
||||
/**
|
||||
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
|
||||
*/
|
||||
String condition() default "";
|
||||
|
||||
}
|
||||
|
@ -37,4 +37,9 @@ public @interface ChineseNameDesensitize {
|
||||
*/
|
||||
String replacer() default "*";
|
||||
|
||||
/**
|
||||
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
|
||||
*/
|
||||
String condition() default "";
|
||||
|
||||
}
|
||||
|
@ -37,4 +37,9 @@ public @interface FixedPhoneDesensitize {
|
||||
*/
|
||||
String replacer() default "*";
|
||||
|
||||
/**
|
||||
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
|
||||
*/
|
||||
String condition() default "";
|
||||
|
||||
}
|
||||
|
@ -37,4 +37,9 @@ public @interface IdCardDesensitize {
|
||||
*/
|
||||
String replacer() default "*";
|
||||
|
||||
/**
|
||||
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
|
||||
*/
|
||||
String condition() default "";
|
||||
|
||||
}
|
||||
|
@ -37,4 +37,9 @@ public @interface MobileDesensitize {
|
||||
*/
|
||||
String replacer() default "*";
|
||||
|
||||
/**
|
||||
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
|
||||
*/
|
||||
String condition() default "";
|
||||
|
||||
}
|
||||
|
@ -39,4 +39,9 @@ public @interface PasswordDesensitize {
|
||||
*/
|
||||
String replacer() default "*";
|
||||
|
||||
/**
|
||||
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
|
||||
*/
|
||||
String condition() default "";
|
||||
|
||||
}
|
||||
|
@ -40,4 +40,10 @@ public @interface SliderDesensitize {
|
||||
* 前缀保留长度
|
||||
*/
|
||||
int prefixKeep() default 0;
|
||||
|
||||
/**
|
||||
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
|
||||
*/
|
||||
String condition() default "";
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
}
|
||||
|
@ -24,4 +24,9 @@ public class BankCardDesensitization extends AbstractSliderDesensitizationHandle
|
||||
return annotation.replacer();
|
||||
}
|
||||
|
||||
@Override
|
||||
String getCondition(BankCardDesensitize annotation) {
|
||||
return annotation.condition();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -22,4 +22,10 @@ public class CarLicenseDesensitization extends AbstractSliderDesensitizationHand
|
||||
String getReplacer(CarLicenseDesensitize annotation) {
|
||||
return annotation.replacer();
|
||||
}
|
||||
|
||||
@Override
|
||||
String getCondition(CarLicenseDesensitize annotation) {
|
||||
return annotation.condition();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -24,4 +24,9 @@ public class ChineseNameDesensitization extends AbstractSliderDesensitizationHan
|
||||
return annotation.replacer();
|
||||
}
|
||||
|
||||
@Override
|
||||
String getCondition(ChineseNameDesensitize annotation) {
|
||||
return annotation.condition();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -22,4 +22,10 @@ public class DefaultDesensitizationHandler extends AbstractSliderDesensitization
|
||||
String getReplacer(SliderDesensitize annotation) {
|
||||
return annotation.replacer();
|
||||
}
|
||||
|
||||
@Override
|
||||
String getCondition(SliderDesensitize annotation) {
|
||||
return annotation.condition();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -22,4 +22,10 @@ public class FixedPhoneDesensitization extends AbstractSliderDesensitizationHand
|
||||
String getReplacer(FixedPhoneDesensitize annotation) {
|
||||
return annotation.replacer();
|
||||
}
|
||||
|
||||
@Override
|
||||
String getCondition(FixedPhoneDesensitize annotation) {
|
||||
return annotation.condition();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -22,4 +22,10 @@ public class IdCardDesensitization extends AbstractSliderDesensitizationHandler<
|
||||
String getReplacer(IdCardDesensitize annotation) {
|
||||
return annotation.replacer();
|
||||
}
|
||||
|
||||
@Override
|
||||
String getCondition(IdCardDesensitize annotation) {
|
||||
return annotation.condition();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -23,4 +23,10 @@ public class MobileDesensitization extends AbstractSliderDesensitizationHandler<
|
||||
String getReplacer(MobileDesensitize annotation) {
|
||||
return annotation.replacer();
|
||||
}
|
||||
|
||||
@Override
|
||||
String getCondition(MobileDesensitize annotation) {
|
||||
return annotation.condition();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -22,4 +22,10 @@ public class PasswordDesensitization extends AbstractSliderDesensitizationHandle
|
||||
String getReplacer(PasswordDesensitize annotation) {
|
||||
return annotation.replacer();
|
||||
}
|
||||
|
||||
@Override
|
||||
String getCondition(PasswordDesensitize annotation) {
|
||||
return annotation.condition();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user