mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-27 01:32:03 +08:00
【优化】数据脱敏支持 Spring el 表达式,支持根据权限控制脱敏
This commit is contained in:
parent
8c2b8ed894
commit
7ba3b12313
@ -4,9 +4,9 @@ import cn.hutool.core.collection.CollUtil;
|
|||||||
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.map.MapUtil;
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.hutool.extra.spring.SpringUtil;
|
||||||
import org.aspectj.lang.JoinPoint;
|
import org.aspectj.lang.JoinPoint;
|
||||||
import org.aspectj.lang.reflect.MethodSignature;
|
import org.aspectj.lang.reflect.MethodSignature;
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
|
||||||
import org.springframework.context.expression.BeanFactoryResolver;
|
import org.springframework.context.expression.BeanFactoryResolver;
|
||||||
import org.springframework.core.DefaultParameterNameDiscoverer;
|
import org.springframework.core.DefaultParameterNameDiscoverer;
|
||||||
import org.springframework.core.ParameterNameDiscoverer;
|
import org.springframework.core.ParameterNameDiscoverer;
|
||||||
@ -93,17 +93,16 @@ public class SpringExpressionUtils {
|
|||||||
/**
|
/**
|
||||||
* 从 Bean 工厂,解析 EL 表达式的结果
|
* 从 Bean 工厂,解析 EL 表达式的结果
|
||||||
*
|
*
|
||||||
* @param beanFactory Bean 工程
|
|
||||||
* @param expressionString EL 表达式
|
* @param expressionString EL 表达式
|
||||||
* @return 执行界面
|
* @return 执行界面
|
||||||
*/
|
*/
|
||||||
public static Object parseExpression(BeanFactory beanFactory, String expressionString) {
|
public static Object parseExpression(String expressionString) {
|
||||||
if (StrUtil.isBlank(expressionString)) {
|
if (StrUtil.isBlank(expressionString)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Expression expression = EXPRESSION_PARSER.parseExpression(expressionString);
|
Expression expression = EXPRESSION_PARSER.parseExpression(expressionString);
|
||||||
StandardEvaluationContext context = new StandardEvaluationContext();
|
StandardEvaluationContext context = new StandardEvaluationContext();
|
||||||
context.setBeanResolver(new BeanFactoryResolver(beanFactory));
|
context.setBeanResolver(new BeanFactoryResolver(SpringUtil.getApplicationContext()));
|
||||||
return expression.getValue(context);
|
return expression.getValue(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package cn.iocoder.yudao.framework.desensitize.core.base.handler;
|
package cn.iocoder.yudao.framework.desensitize.core.base.handler;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ReflectUtil;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -18,4 +20,21 @@ public interface DesensitizationHandler<T extends Annotation> {
|
|||||||
*/
|
*/
|
||||||
String desensitize(String origin, T annotation);
|
String desensitize(String origin, T annotation);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否禁用脱敏的 Spring EL 表达式
|
||||||
|
*
|
||||||
|
* 如果返回 true 则跳过脱敏
|
||||||
|
*
|
||||||
|
* @param annotation 注解信息
|
||||||
|
* @return 是否禁用脱敏的 Spring EL 表达式
|
||||||
|
*/
|
||||||
|
default String getDisable(T annotation) {
|
||||||
|
// 约定:默认就是 enable() 属性。如果不符合,子类重写
|
||||||
|
try {
|
||||||
|
return (String) ReflectUtil.invoke(annotation, "disable");
|
||||||
|
} catch (Exception ex) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -35,8 +35,10 @@ public @interface EmailDesensitize {
|
|||||||
String replacer() default "$1****$2";
|
String replacer() default "$1****$2";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
|
* 是否禁用脱敏
|
||||||
|
*
|
||||||
|
* 支持 Spring EL 表达式,如果返回 true 则跳过脱敏
|
||||||
*/
|
*/
|
||||||
String condition() default "";
|
String disable() default "";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -37,8 +37,10 @@ public @interface RegexDesensitize {
|
|||||||
String replacer() default "******";
|
String replacer() default "******";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
|
* 是否禁用脱敏
|
||||||
|
*
|
||||||
|
* 支持 Spring EL 表达式,如果返回 true 则跳过脱敏
|
||||||
*/
|
*/
|
||||||
String condition() default "";
|
String disable() default "";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package cn.iocoder.yudao.framework.desensitize.core.regex.handler;
|
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.common.util.spring.SpringExpressionUtils;
|
||||||
import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler;
|
import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler;
|
||||||
|
|
||||||
@ -16,10 +15,13 @@ public abstract class AbstractRegexDesensitizationHandler<T extends Annotation>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String desensitize(String origin, T annotation) {
|
public String desensitize(String origin, T annotation) {
|
||||||
Object expressionResult = SpringExpressionUtils.parseExpression(SpringUtil.getApplicationContext(), getCondition(annotation));
|
// 1. 判断是否禁用脱敏
|
||||||
if (expressionResult instanceof Boolean && (Boolean) expressionResult) {
|
Object disable = SpringExpressionUtils.parseExpression(getDisable(annotation));
|
||||||
|
if (Boolean.TRUE.equals(disable)) {
|
||||||
return origin;
|
return origin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2. 执行脱敏
|
||||||
String regex = getRegex(annotation);
|
String regex = getRegex(annotation);
|
||||||
String replacer = getReplacer(annotation);
|
String replacer = getReplacer(annotation);
|
||||||
return origin.replaceAll(regex, replacer);
|
return origin.replaceAll(regex, replacer);
|
||||||
@ -41,12 +43,4 @@ public abstract class AbstractRegexDesensitizationHandler<T extends Annotation>
|
|||||||
*/
|
*/
|
||||||
abstract String getReplacer(T annotation);
|
abstract String getReplacer(T annotation);
|
||||||
|
|
||||||
/**
|
|
||||||
* el 表达式
|
|
||||||
*
|
|
||||||
* @param annotation 注解信息
|
|
||||||
* @return el 表达式
|
|
||||||
*/
|
|
||||||
abstract String getCondition(T annotation);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -20,8 +20,8 @@ public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitiza
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
String getCondition(RegexDesensitize annotation) {
|
public String getDisable(RegexDesensitize annotation) {
|
||||||
return annotation.condition();
|
return annotation.disable();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -19,9 +19,4 @@ public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHan
|
|||||||
return annotation.replacer();
|
return annotation.replacer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
String getCondition(EmailDesensitize annotation) {
|
|
||||||
return annotation.condition();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -38,8 +38,10 @@ public @interface BankCardDesensitize {
|
|||||||
String replacer() default "*";
|
String replacer() default "*";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
|
* 是否禁用脱敏
|
||||||
|
*
|
||||||
|
* 支持 Spring EL 表达式,如果返回 true 则跳过脱敏
|
||||||
*/
|
*/
|
||||||
String condition() default "";
|
String disable() default "";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -38,8 +38,10 @@ public @interface CarLicenseDesensitize {
|
|||||||
String replacer() default "*";
|
String replacer() default "*";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
|
* 是否禁用脱敏
|
||||||
|
*
|
||||||
|
* 支持 Spring EL 表达式,如果返回 true 则跳过脱敏
|
||||||
*/
|
*/
|
||||||
String condition() default "";
|
String disable() default "";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -38,8 +38,10 @@ public @interface ChineseNameDesensitize {
|
|||||||
String replacer() default "*";
|
String replacer() default "*";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
|
* 是否禁用脱敏
|
||||||
|
*
|
||||||
|
* 支持 Spring EL 表达式,如果返回 true 则跳过脱敏
|
||||||
*/
|
*/
|
||||||
String condition() default "";
|
String disable() default "";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -38,8 +38,10 @@ public @interface FixedPhoneDesensitize {
|
|||||||
String replacer() default "*";
|
String replacer() default "*";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
|
* 是否禁用脱敏
|
||||||
|
*
|
||||||
|
* 支持 Spring EL 表达式,如果返回 true 则跳过脱敏
|
||||||
*/
|
*/
|
||||||
String condition() default "";
|
String disable() default "";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -38,8 +38,10 @@ public @interface IdCardDesensitize {
|
|||||||
String replacer() default "*";
|
String replacer() default "*";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
|
* 是否禁用脱敏
|
||||||
|
*
|
||||||
|
* 支持 Spring EL 表达式,如果返回 true 则跳过脱敏
|
||||||
*/
|
*/
|
||||||
String condition() default "";
|
String disable() default "";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -38,8 +38,10 @@ public @interface MobileDesensitize {
|
|||||||
String replacer() default "*";
|
String replacer() default "*";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
|
* 是否禁用脱敏
|
||||||
|
*
|
||||||
|
* 支持 Spring EL 表达式,如果返回 true 则跳过脱敏
|
||||||
*/
|
*/
|
||||||
String condition() default "";
|
String disable() default "";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -40,8 +40,10 @@ public @interface PasswordDesensitize {
|
|||||||
String replacer() default "*";
|
String replacer() default "*";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
|
* 是否禁用脱敏
|
||||||
|
*
|
||||||
|
* 支持 Spring EL 表达式,如果返回 true 则跳过脱敏
|
||||||
*/
|
*/
|
||||||
String condition() default "";
|
String disable() default "";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -42,8 +42,10 @@ public @interface SliderDesensitize {
|
|||||||
int prefixKeep() default 0;
|
int prefixKeep() default 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* el 表达式,当执行 condition 返回 true 的时候,跳过脱敏
|
* 是否禁用脱敏
|
||||||
|
*
|
||||||
|
* 支持 Spring EL 表达式,如果返回 true 则跳过脱敏
|
||||||
*/
|
*/
|
||||||
String condition() default "";
|
String disable() default "";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
|
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.common.util.spring.SpringExpressionUtils;
|
||||||
import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler;
|
import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler;
|
||||||
|
|
||||||
@ -16,10 +15,13 @@ public abstract class AbstractSliderDesensitizationHandler<T extends Annotation>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String desensitize(String origin, T annotation) {
|
public String desensitize(String origin, T annotation) {
|
||||||
Object expressionResult = SpringExpressionUtils.parseExpression(SpringUtil.getApplicationContext(), getCondition(annotation));
|
// 1. 判断是否禁用脱敏
|
||||||
if (expressionResult instanceof Boolean && (Boolean) expressionResult) {
|
Object disable = SpringExpressionUtils.parseExpression(getDisable(annotation));
|
||||||
|
if (Boolean.FALSE.equals(disable)) {
|
||||||
return origin;
|
return origin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2. 执行脱敏
|
||||||
int prefixKeep = getPrefixKeep(annotation);
|
int prefixKeep = getPrefixKeep(annotation);
|
||||||
int suffixKeep = getSuffixKeep(annotation);
|
int suffixKeep = getSuffixKeep(annotation);
|
||||||
String replacer = getReplacer(annotation);
|
String replacer = getReplacer(annotation);
|
||||||
@ -81,12 +83,4 @@ public abstract class AbstractSliderDesensitizationHandler<T extends Annotation>
|
|||||||
*/
|
*/
|
||||||
abstract String getReplacer(T annotation);
|
abstract String getReplacer(T annotation);
|
||||||
|
|
||||||
/**
|
|
||||||
* el 表达式
|
|
||||||
*
|
|
||||||
* @param annotation 注解信息
|
|
||||||
* @return el 表达式
|
|
||||||
*/
|
|
||||||
abstract String getCondition(T annotation);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -25,8 +25,8 @@ public class BankCardDesensitization extends AbstractSliderDesensitizationHandle
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
String getCondition(BankCardDesensitize annotation) {
|
public String getDisable(BankCardDesensitize annotation) {
|
||||||
return annotation.condition();
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.CarLicenseD
|
|||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
public class CarLicenseDesensitization extends AbstractSliderDesensitizationHandler<CarLicenseDesensitize> {
|
public class CarLicenseDesensitization extends AbstractSliderDesensitizationHandler<CarLicenseDesensitize> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
Integer getPrefixKeep(CarLicenseDesensitize annotation) {
|
Integer getPrefixKeep(CarLicenseDesensitize annotation) {
|
||||||
return annotation.prefixKeep();
|
return annotation.prefixKeep();
|
||||||
@ -24,8 +25,8 @@ public class CarLicenseDesensitization extends AbstractSliderDesensitizationHand
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
String getCondition(CarLicenseDesensitize annotation) {
|
public String getDisable(CarLicenseDesensitize annotation) {
|
||||||
return annotation.condition();
|
return annotation.disable();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -24,9 +24,4 @@ public class ChineseNameDesensitization extends AbstractSliderDesensitizationHan
|
|||||||
return annotation.replacer();
|
return annotation.replacer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
String getCondition(ChineseNameDesensitize annotation) {
|
|
||||||
return annotation.condition();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.SliderDesen
|
|||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
public class DefaultDesensitizationHandler extends AbstractSliderDesensitizationHandler<SliderDesensitize> {
|
public class DefaultDesensitizationHandler extends AbstractSliderDesensitizationHandler<SliderDesensitize> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
Integer getPrefixKeep(SliderDesensitize annotation) {
|
Integer getPrefixKeep(SliderDesensitize annotation) {
|
||||||
return annotation.prefixKeep();
|
return annotation.prefixKeep();
|
||||||
@ -23,9 +24,4 @@ public class DefaultDesensitizationHandler extends AbstractSliderDesensitization
|
|||||||
return annotation.replacer();
|
return annotation.replacer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
String getCondition(SliderDesensitize annotation) {
|
|
||||||
return annotation.condition();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.FixedPhoneD
|
|||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
public class FixedPhoneDesensitization extends AbstractSliderDesensitizationHandler<FixedPhoneDesensitize> {
|
public class FixedPhoneDesensitization extends AbstractSliderDesensitizationHandler<FixedPhoneDesensitize> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
Integer getPrefixKeep(FixedPhoneDesensitize annotation) {
|
Integer getPrefixKeep(FixedPhoneDesensitize annotation) {
|
||||||
return annotation.prefixKeep();
|
return annotation.prefixKeep();
|
||||||
@ -23,9 +24,4 @@ public class FixedPhoneDesensitization extends AbstractSliderDesensitizationHand
|
|||||||
return annotation.replacer();
|
return annotation.replacer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
String getCondition(FixedPhoneDesensitize annotation) {
|
|
||||||
return annotation.condition();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -23,9 +23,4 @@ public class IdCardDesensitization extends AbstractSliderDesensitizationHandler<
|
|||||||
return annotation.replacer();
|
return annotation.replacer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
String getCondition(IdCardDesensitize annotation) {
|
|
||||||
return annotation.condition();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -24,9 +24,4 @@ public class MobileDesensitization extends AbstractSliderDesensitizationHandler<
|
|||||||
return annotation.replacer();
|
return annotation.replacer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
String getCondition(MobileDesensitize annotation) {
|
|
||||||
return annotation.condition();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -23,9 +23,4 @@ public class PasswordDesensitization extends AbstractSliderDesensitizationHandle
|
|||||||
return annotation.replacer();
|
return annotation.replacer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
String getCondition(PasswordDesensitize annotation) {
|
|
||||||
return annotation.condition();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user