-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
836 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...t-desensitize/src/main/java/org/ballcat/desensitize/handler/IPDesensitizationHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
95 changes: 95 additions & 0 deletions
95
...desensitize/src/main/java/org/ballcat/desensitize/handler/RuleDesensitizationHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...esensitize/src/main/java/org/ballcat/desensitize/json/annotation/JsonRuleDesensitize.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 '*'; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,4 +59,9 @@ | |
*/ | ||
String maskString() default "*"; | ||
|
||
/** | ||
* 是否反转 | ||
*/ | ||
boolean reverse() default false; | ||
|
||
} |
Oops, something went wrong.