Skip to content

Commit

Permalink
✨ DesensitizedUtil简化使用
Browse files Browse the repository at this point in the history
  • Loading branch information
evil0th authored and Hccake committed Apr 12, 2024
1 parent fa9f97a commit a3be4eb
Show file tree
Hide file tree
Showing 14 changed files with 836 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
import org.ballcat.desensitize.enums.SlideDesensitizationTypeEnum;
import org.ballcat.desensitize.functions.DesensitizeFunction;
import org.ballcat.desensitize.handler.RegexDesensitizationHandler;
import org.ballcat.desensitize.handler.RuleDesensitizationHandler;
import org.ballcat.desensitize.handler.SimpleDesensitizationHandler;
import org.ballcat.desensitize.handler.SlideDesensitizationHandler;
import org.ballcat.desensitize.json.annotation.JsonRegexDesensitize;
import org.ballcat.desensitize.json.annotation.JsonRuleDesensitize;
import org.ballcat.desensitize.json.annotation.JsonSimpleDesensitize;
import org.ballcat.desensitize.json.annotation.JsonSlideDesensitize;

Expand Down Expand Up @@ -77,9 +79,18 @@ private AnnotationHandlerHolder() {
SlideDesensitizationTypeEnum type = an.type();
SlideDesensitizationHandler slideDesensitizationHandler = DesensitizationHandlerHolder
.getSlideDesensitizationHandler();
return SlideDesensitizationTypeEnum.CUSTOM.equals(type) ? slideDesensitizationHandler.handle(value,
an.leftPlainTextLen(), an.rightPlainTextLen(), an.maskString())
: slideDesensitizationHandler.handle(value, type);
return SlideDesensitizationTypeEnum.CUSTOM.equals(type)
? slideDesensitizationHandler.handle(value, an.leftPlainTextLen(), an.rightPlainTextLen(),
an.maskString(), an.reverse())
: slideDesensitizationHandler.handle(value, type, an.reverse());
});

this.annotationHandlers.put(JsonRuleDesensitize.class, (annotation, value) -> {
// 规则类型脱敏处理
JsonRuleDesensitize an = (JsonRuleDesensitize) annotation;
RuleDesensitizationHandler ruleDesensitizationHandler = DesensitizationHandlerHolder
.getRuleDesensitizationHandler();
return ruleDesensitizationHandler.handle(value, an.maskChar(), an.reverse(), an.rule());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.ballcat.desensitize.handler.DesensitizationHandler;
import org.ballcat.desensitize.handler.RegexDesensitizationHandler;
import org.ballcat.desensitize.handler.RuleDesensitizationHandler;
import org.ballcat.desensitize.handler.SimpleDesensitizationHandler;
import org.ballcat.desensitize.handler.SlideDesensitizationHandler;

Expand All @@ -47,6 +48,8 @@ private DesensitizationHandlerHolder() {
this.desensitizationHandlerMap.put(SlideDesensitizationHandler.class, new SlideDesensitizationHandler());
// 正则脱敏处理器
this.desensitizationHandlerMap.put(RegexDesensitizationHandler.class, new RegexDesensitizationHandler());
// 基于规则脱敏处理器
this.desensitizationHandlerMap.put(RuleDesensitizationHandler.class, new RuleDesensitizationHandler());
// SPI 加载所有的 Simple脱敏类型处理
ServiceLoader<SimpleDesensitizationHandler> loadedDrivers = ServiceLoader
.load(SimpleDesensitizationHandler.class);
Expand Down Expand Up @@ -79,6 +82,14 @@ public static SlideDesensitizationHandler getSlideDesensitizationHandler() {
return (SlideDesensitizationHandler) INSTANCE.desensitizationHandlerMap.get(SlideDesensitizationHandler.class);
}

/**
* 获取 RuleDesensitizationHandler
* @return 处理器实例
*/
public static RuleDesensitizationHandler getRuleDesensitizationHandler() {
return (RuleDesensitizationHandler) INSTANCE.desensitizationHandlerMap.get(RuleDesensitizationHandler.class);
}

/**
* 获取指定的 SimpleDesensitizationHandler
* @param handlerClass SimpleDesensitizationHandler的实现类
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.ballcat.desensitize.handler;

/**
* 【IP】IPv4返回10.*.*.*,IPv6返回2001:*:*:*:*:*:*:*
*
* @author evil0th Create on 2024/4/12
*/
public class IPDesensitizationHandler implements SimpleDesensitizationHandler {

/**
* 脱敏处理
* @param origin 原始IP
* @return 脱敏处理后的IP
*/
@Override
public String handle(String origin) {
if (null == origin) {
return null;
}
int index = origin.indexOf(".");
if (index > 0) {
return origin.substring(0, index) + ".*.*.*";
}
index = origin.indexOf(":");
if (index > 0) {
return origin.substring(0, index) + ":*:*:*:*:*:*:*";
}
return origin;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.ballcat.desensitize.handler;

import org.ballcat.desensitize.rule.DesensitizeRule;

/**
* 基于规则的替换脱敏处理器,如指定"3-6,8,10-"表示第4,5,6,7,9,11以及11之后的位替换处理
*
* @author evil0th Create on 2024/4/12
*/
public class RuleDesensitizationHandler implements DesensitizationHandler {

/**
* 基于规则的替换字符串
*
* <pre>
* handle("43012319990101432X", "1", "4-6", "9-")) = "4*01***99*********"
* </pre>
* @param input 输入字符串
* @param rule 规则
* @return 脱敏字符串
*/
public String handle(String input, String... rule) {
return handle(input, false, rule);
}

/**
* 基于规则的替换字符串(支持反转)
*
* <pre>
* handle("43012319990101432X", true, "1", "4-6", "9-")) = "4*01***99*********"
* </pre>
* @param input 输入字符串
* @param rule 规则
* @param reverse 是否反转规则
* @return 脱敏字符串
*/
public String handle(String input, boolean reverse, String... rule) {
return handle(input, '*', reverse, rule);
}

/**
* 基于规则的替换字符串
*
* <pre>
* handle("43012319990101432X", '-', false, "1", "4-6", "9-")) = "4-01---99---------"
* handle("43012319990101432X", '-', true, "1", "4-6", "9-")) = "-3--231--90101432X"
* </pre>
* @param input 输入字符串
* @param rule 规则。{@link DesensitizeRule}
* @param symbol 符号,默认*
* @param reverse 是否反转规则
* @return 脱敏字符串
*/
public String handle(String input, char symbol, boolean reverse, String... rule) {
return handle(input, symbol, reverse, DesensitizeRule.analysis(rule));
}

/**
* 基于规则的替换字符串
* @param origin 输入字符串
* @param rule 规则。{@link DesensitizeRule}
* @param symbol 符号,默认*
* @param reverse 是否反转规则
* @return 脱敏字符串
*/
private String handle(String origin, char symbol, boolean reverse, DesensitizeRule rule) {
if (origin == null) {
return null;
}
char[] clearChars = origin.toCharArray();
for (int i = 0; i < clearChars.length; ++i) {
if (reverse ^ rule.isIn(i)) {
clearChars[i] = symbol;
}
}
return new String(clearChars);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,19 @@ public class SlideDesensitizationHandler implements DesensitizationHandler {
* @return 脱敏后的字符串
*/
public String handle(String origin, int leftPlainTextLen, int rightPlainTextLen) {
return this.handle(origin, leftPlainTextLen, rightPlainTextLen, "*");
return this.handle(origin, leftPlainTextLen, rightPlainTextLen, false);
}

/**
* 滑动脱敏
* @param origin 原文
* @param leftPlainTextLen 处理完后左边的明文数
* @param rightPlainTextLen 处理完后右边的明文数
* @param reverse 是否反转
* @return 脱敏后的字符串
*/
public String handle(String origin, int leftPlainTextLen, int rightPlainTextLen, boolean reverse) {
return this.handle(origin, leftPlainTextLen, rightPlainTextLen, "*", reverse);
}

/**
Expand All @@ -46,6 +58,20 @@ public String handle(String origin, int leftPlainTextLen, int rightPlainTextLen)
* @return 脱敏后的字符串
*/
public String handle(String origin, int leftPlainTextLen, int rightPlainTextLen, String maskString) {
return this.handle(origin, leftPlainTextLen, rightPlainTextLen, maskString, false);
}

/**
* 滑动脱敏
* @param origin 原文
* @param leftPlainTextLen 处理完后左边的明文数
* @param rightPlainTextLen 处理完后右边的明文数
* @param maskString 原文窗口内每个字符被替换后的字符串
* @param reverse 是否反转
* @return 脱敏后的字符串
*/
public String handle(String origin, int leftPlainTextLen, int rightPlainTextLen, String maskString,
boolean reverse) {
if (origin == null) {
return null;
}
Expand All @@ -56,10 +82,10 @@ public String handle(String origin, int leftPlainTextLen, int rightPlainTextLen,
for (int i = 0; i < length; i++) {
// 明文位内则明文显示
if (i < leftPlainTextLen || i > (length - rightPlainTextLen - 1)) {
sb.append(chars[i]);
sb.append(reverse ? maskString : chars[i]);
}
else {
sb.append(maskString);
sb.append(reverse ? chars[i] : maskString);
}
}
return sb.toString();
Expand All @@ -72,7 +98,19 @@ public String handle(String origin, int leftPlainTextLen, int rightPlainTextLen,
* @return 脱敏后的字符串
*/
public String handle(String value, SlideDesensitizationTypeEnum type) {
return this.handle(value, type.getLeftPlainTextLen(), type.getRightPlainTextLen(), type.getMaskString());
return this.handle(value, type, false);
}

/**
* 根据指定枚举类型进行滑动脱敏
* @param value 原文
* @param type 滑动脱敏类型
* @param reverse 是否反转
* @return 脱敏后的字符串
*/
public String handle(String value, SlideDesensitizationTypeEnum type, boolean reverse) {
return this.handle(value, type.getLeftPlainTextLen(), type.getRightPlainTextLen(), type.getMaskString(),
reverse);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.ballcat.desensitize.json.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.ballcat.desensitize.handler.RuleDesensitizationHandler;
import org.ballcat.desensitize.rule.DesensitizeRule;

/**
* Jackson Filed 序列化脱敏注解, 对应基于规则脱敏处理器对值进行脱敏处理
*
* @author evil0th Create on 2024/4/12
* @see RuleDesensitizationHandler
*/
@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface JsonRuleDesensitize {

/**
* 脱敏规则
* @return type
* @see DesensitizeRule
*/
String[] rule();

/**
* 是否反转规则
*/
boolean reverse() default false;

/**
* 替换的字符串
*/
char maskChar() default '*';

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

/**
* 是否反转
*/
boolean reverse() default false;

}
Loading

0 comments on commit a3be4eb

Please sign in to comment.