diff --git a/cloud-common-plugin-sensitive/src/main/java/com/cm/plugin/manager/SensitiveWordsManager.java b/cloud-common-plugin-sensitive/src/main/java/com/cm/plugin/manager/SensitiveWordsManager.java index 20bfdb5..cf66514 100644 --- a/cloud-common-plugin-sensitive/src/main/java/com/cm/plugin/manager/SensitiveWordsManager.java +++ b/cloud-common-plugin-sensitive/src/main/java/com/cm/plugin/manager/SensitiveWordsManager.java @@ -1,17 +1,13 @@ package com.cm.plugin.manager; -import com.alibaba.druid.sql.visitor.functions.Char; import org.apache.commons.lang3.StringUtils; -import org.omg.CORBA.OBJ_ADAPTER; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; /** * When you feel like quitting. Think about why you started @@ -123,6 +119,33 @@ public class SensitiveWordsManager { return sensitiveResult; } + /** + * 返回敏感结果,替换成相应字符 + * + * @param content + * @param replace + * @return + */ + public String getSensitiveWordsResultWithReplace(String content, char replace) { + SensitiveResult sensitiveResult = getSensitiveWordsResult(content); + if (sensitiveResult == null) { + return null; + } + List sensitiveWordsResults = sensitiveResult.getSensitiveWordsResults(); + if (sensitiveWordsResults.isEmpty()) { + return sensitiveResult.getSensitive(); + } + for (SensitiveWordsResult sensitiveWordsResult : sensitiveWordsResults) { + String sensitiveWord = sensitiveWordsResult.getSensitiveWord(); + StringBuilder replaceSB = new StringBuilder(); + for (int i = 0; i < sensitiveWord.length(); i++) { + replaceSB.append(replace); + } + sensitiveResult.setSensitive(sensitiveResult.getSensitive().replaceAll(sensitiveWord, replaceSB.toString())); + } + return sensitiveResult.getSensitive(); + } + private static class SensitiveWordsManagerBuilder { public static SensitiveWordsManager sensitiveWordsManager = new SensitiveWordsManager(); }