From 2f6387288ab2db0afb81fd836b4ff2375a503a18 Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Wed, 17 Jun 2020 23:48:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=9B=B4=E6=8E=A5=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=95=8F=E6=84=9F=E8=AF=8D=E5=A4=84=E7=90=86=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/manager/SensitiveWordsManager.java | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) 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(); }