Commit 55fec9e8ef38c30cd88c00b6a03510036b6ebda6
Committed by
GitHub
Merge pull request #605 from craftsman4j/dev-0712-sensitive
【优化】数据脱敏支持 Spring el 表达式,支持根据权限控制脱敏
Showing
24 changed files
with
164 additions
and
0 deletions
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/spring/SpringExpressionUtils.java
| ... | ... | @@ -3,11 +3,15 @@ package cn.iocoder.yudao.framework.common.util.spring; |
| 3 | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | 4 | import cn.hutool.core.map.MapUtil; |
| 5 | 5 | import cn.hutool.core.util.ArrayUtil; |
| 6 | +import cn.hutool.core.util.StrUtil; | |
| 6 | 7 | import org.aspectj.lang.JoinPoint; |
| 7 | 8 | import org.aspectj.lang.reflect.MethodSignature; |
| 9 | +import org.springframework.beans.factory.BeanFactory; | |
| 10 | +import org.springframework.context.expression.BeanFactoryResolver; | |
| 8 | 11 | import org.springframework.core.DefaultParameterNameDiscoverer; |
| 9 | 12 | import org.springframework.core.ParameterNameDiscoverer; |
| 10 | 13 | import org.springframework.expression.EvaluationContext; |
| 14 | +import org.springframework.expression.Expression; | |
| 11 | 15 | import org.springframework.expression.ExpressionParser; |
| 12 | 16 | import org.springframework.expression.spel.standard.SpelExpressionParser; |
| 13 | 17 | import org.springframework.expression.spel.support.StandardEvaluationContext; |
| ... | ... | @@ -86,4 +90,21 @@ public class SpringExpressionUtils { |
| 86 | 90 | return result; |
| 87 | 91 | } |
| 88 | 92 | |
| 93 | + /** | |
| 94 | + * 从 Bean 工厂,解析 EL 表达式的结果 | |
| 95 | + * | |
| 96 | + * @param beanFactory Bean 工程 | |
| 97 | + * @param expressionString EL 表达式 | |
| 98 | + * @return 执行界面 | |
| 99 | + */ | |
| 100 | + public static Object parseExpression(BeanFactory beanFactory, String expressionString) { | |
| 101 | + if (StrUtil.isBlank(expressionString)) { | |
| 102 | + return null; | |
| 103 | + } | |
| 104 | + Expression expression = EXPRESSION_PARSER.parseExpression(expressionString); | |
| 105 | + StandardEvaluationContext context = new StandardEvaluationContext(); | |
| 106 | + context.setBeanResolver(new BeanFactoryResolver(beanFactory)); | |
| 107 | + return expression.getValue(context); | |
| 108 | + } | |
| 109 | + | |
| 89 | 110 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-web/pom.xml
| ... | ... | @@ -32,6 +32,11 @@ |
| 32 | 32 | <artifactId>spring-boot-configuration-processor</artifactId> |
| 33 | 33 | <optional>true</optional> |
| 34 | 34 | </dependency> |
| 35 | + <dependency> | |
| 36 | + <groupId>org.aspectj</groupId> | |
| 37 | + <artifactId>aspectjweaver</artifactId> | |
| 38 | + <scope>provided</scope> <!-- 解决工具类 SpringExpressionUtils 加载的时候访问不到 org.aspectj.lang.JoinPoint 问题 --> | |
| 39 | + </dependency> | |
| 35 | 40 | |
| 36 | 41 | <dependency> |
| 37 | 42 | <groupId>com.github.xiaoymin</groupId> | ... | ... |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/RegexDesensitize.java
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/AbstractRegexDesensitizationHandler.java
| 1 | 1 | package cn.iocoder.yudao.framework.desensitize.core.regex.handler; |
| 2 | 2 | |
| 3 | +import cn.hutool.extra.spring.SpringUtil; | |
| 4 | +import cn.iocoder.yudao.framework.common.util.spring.SpringExpressionUtils; | |
| 3 | 5 | import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; |
| 4 | 6 | |
| 5 | 7 | import java.lang.annotation.Annotation; |
| ... | ... | @@ -14,6 +16,10 @@ public abstract class AbstractRegexDesensitizationHandler<T extends Annotation> |
| 14 | 16 | |
| 15 | 17 | @Override |
| 16 | 18 | public String desensitize(String origin, T annotation) { |
| 19 | + Object expressionResult = SpringExpressionUtils.parseExpression(SpringUtil.getApplicationContext(), getCondition(annotation)); | |
| 20 | + if (expressionResult instanceof Boolean && (Boolean) expressionResult) { | |
| 21 | + return origin; | |
| 22 | + } | |
| 17 | 23 | String regex = getRegex(annotation); |
| 18 | 24 | String replacer = getReplacer(annotation); |
| 19 | 25 | return origin.replaceAll(regex, replacer); |
| ... | ... | @@ -35,4 +41,12 @@ public abstract class AbstractRegexDesensitizationHandler<T extends Annotation> |
| 35 | 41 | */ |
| 36 | 42 | abstract String getReplacer(T annotation); |
| 37 | 43 | |
| 44 | + /** | |
| 45 | + * el 表达式 | |
| 46 | + * | |
| 47 | + * @param annotation 注解信息 | |
| 48 | + * @return el 表达式 | |
| 49 | + */ | |
| 50 | + abstract String getCondition(T annotation); | |
| 51 | + | |
| 38 | 52 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/DefaultRegexDesensitizationHandler.java
| ... | ... | @@ -18,4 +18,10 @@ public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitiza |
| 18 | 18 | String getReplacer(RegexDesensitize annotation) { |
| 19 | 19 | return annotation.replacer(); |
| 20 | 20 | } |
| 21 | + | |
| 22 | + @Override | |
| 23 | + String getCondition(RegexDesensitize annotation) { | |
| 24 | + return annotation.condition(); | |
| 25 | + } | |
| 26 | + | |
| 21 | 27 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/EmailDesensitizationHandler.java
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/BankCardDesensitize.java
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/CarLicenseDesensitize.java
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/ChineseNameDesensitize.java
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/FixedPhoneDesensitize.java
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/IdCardDesensitize.java
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/MobileDesensitize.java
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/PasswordDesensitize.java
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/SliderDesensitize.java
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AbstractSliderDesensitizationHandler.java
| 1 | 1 | package cn.iocoder.yudao.framework.desensitize.core.slider.handler; |
| 2 | 2 | |
| 3 | +import cn.hutool.extra.spring.SpringUtil; | |
| 4 | +import cn.iocoder.yudao.framework.common.util.spring.SpringExpressionUtils; | |
| 3 | 5 | import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; |
| 4 | 6 | |
| 5 | 7 | import java.lang.annotation.Annotation; |
| ... | ... | @@ -14,6 +16,10 @@ public abstract class AbstractSliderDesensitizationHandler<T extends Annotation> |
| 14 | 16 | |
| 15 | 17 | @Override |
| 16 | 18 | public String desensitize(String origin, T annotation) { |
| 19 | + Object expressionResult = SpringExpressionUtils.parseExpression(SpringUtil.getApplicationContext(), getCondition(annotation)); | |
| 20 | + if (expressionResult instanceof Boolean && (Boolean) expressionResult) { | |
| 21 | + return origin; | |
| 22 | + } | |
| 17 | 23 | int prefixKeep = getPrefixKeep(annotation); |
| 18 | 24 | int suffixKeep = getSuffixKeep(annotation); |
| 19 | 25 | String replacer = getReplacer(annotation); |
| ... | ... | @@ -75,4 +81,12 @@ public abstract class AbstractSliderDesensitizationHandler<T extends Annotation> |
| 75 | 81 | */ |
| 76 | 82 | abstract String getReplacer(T annotation); |
| 77 | 83 | |
| 84 | + /** | |
| 85 | + * el 表达式 | |
| 86 | + * | |
| 87 | + * @param annotation 注解信息 | |
| 88 | + * @return el 表达式 | |
| 89 | + */ | |
| 90 | + abstract String getCondition(T annotation); | |
| 91 | + | |
| 78 | 92 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/BankCardDesensitization.java
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/CarLicenseDesensitization.java
| ... | ... | @@ -22,4 +22,10 @@ public class CarLicenseDesensitization extends AbstractSliderDesensitizationHand |
| 22 | 22 | String getReplacer(CarLicenseDesensitize annotation) { |
| 23 | 23 | return annotation.replacer(); |
| 24 | 24 | } |
| 25 | + | |
| 26 | + @Override | |
| 27 | + String getCondition(CarLicenseDesensitize annotation) { | |
| 28 | + return annotation.condition(); | |
| 29 | + } | |
| 30 | + | |
| 25 | 31 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/ChineseNameDesensitization.java
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/DefaultDesensitizationHandler.java
| ... | ... | @@ -22,4 +22,10 @@ public class DefaultDesensitizationHandler extends AbstractSliderDesensitization |
| 22 | 22 | String getReplacer(SliderDesensitize annotation) { |
| 23 | 23 | return annotation.replacer(); |
| 24 | 24 | } |
| 25 | + | |
| 26 | + @Override | |
| 27 | + String getCondition(SliderDesensitize annotation) { | |
| 28 | + return annotation.condition(); | |
| 29 | + } | |
| 30 | + | |
| 25 | 31 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/FixedPhoneDesensitization.java
| ... | ... | @@ -22,4 +22,10 @@ public class FixedPhoneDesensitization extends AbstractSliderDesensitizationHand |
| 22 | 22 | String getReplacer(FixedPhoneDesensitize annotation) { |
| 23 | 23 | return annotation.replacer(); |
| 24 | 24 | } |
| 25 | + | |
| 26 | + @Override | |
| 27 | + String getCondition(FixedPhoneDesensitize annotation) { | |
| 28 | + return annotation.condition(); | |
| 29 | + } | |
| 30 | + | |
| 25 | 31 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/IdCardDesensitization.java
| ... | ... | @@ -22,4 +22,10 @@ public class IdCardDesensitization extends AbstractSliderDesensitizationHandler< |
| 22 | 22 | String getReplacer(IdCardDesensitize annotation) { |
| 23 | 23 | return annotation.replacer(); |
| 24 | 24 | } |
| 25 | + | |
| 26 | + @Override | |
| 27 | + String getCondition(IdCardDesensitize annotation) { | |
| 28 | + return annotation.condition(); | |
| 29 | + } | |
| 30 | + | |
| 25 | 31 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/MobileDesensitization.java
| ... | ... | @@ -23,4 +23,10 @@ public class MobileDesensitization extends AbstractSliderDesensitizationHandler< |
| 23 | 23 | String getReplacer(MobileDesensitize annotation) { |
| 24 | 24 | return annotation.replacer(); |
| 25 | 25 | } |
| 26 | + | |
| 27 | + @Override | |
| 28 | + String getCondition(MobileDesensitize annotation) { | |
| 29 | + return annotation.condition(); | |
| 30 | + } | |
| 31 | + | |
| 26 | 32 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/PasswordDesensitization.java
| ... | ... | @@ -22,4 +22,10 @@ public class PasswordDesensitization extends AbstractSliderDesensitizationHandle |
| 22 | 22 | String getReplacer(PasswordDesensitize annotation) { |
| 23 | 23 | return annotation.replacer(); |
| 24 | 24 | } |
| 25 | + | |
| 26 | + @Override | |
| 27 | + String getCondition(PasswordDesensitize annotation) { | |
| 28 | + return annotation.condition(); | |
| 29 | + } | |
| 30 | + | |
| 25 | 31 | } | ... | ... |