lines = wordData.getWordData();
@@ -65,8 +65,9 @@ public class SensitiveWordBs {
/**
* 新建验è¯å®žä¾‹
- *
+ *
* double-lock
+ *
* @return this
* @since 0.0.1
*/
@@ -81,16 +82,40 @@ public class SensitiveWordBs {
/**
* 设置是å¦å¯åŠ¨æ•°å—æ£€æµ‹
+ *
* @param enableNumCheck æ•°å—æ£€æµ‹
* @since 0.0.11
*/
public SensitiveWordBs enableNumCheck(boolean enableNumCheck) {
- this.context.sensitiveNumCheck(enableNumCheck);
+ this.context.sensitiveCheckNum(enableNumCheck);
return this;
-}
+ }
+
+ /**
+ * 设置是å¦å¯åЍ email 检测
+ *
+ * @param enableEmailCheck email 检测
+ * @since 0.0.11
+ */
+ public SensitiveWordBs enableEmailCheck(boolean enableEmailCheck) {
+ this.context.sensitiveCheckEmail(enableEmailCheck);
+ return this;
+ }
+
+ /**
+ * 设置是å¦å¯åЍ url 检测
+ *
+ * @param enableUrlCheck url 检测
+ * @since 0.0.12
+ */
+ public SensitiveWordBs enableUrlCheck(boolean enableUrlCheck) {
+ this.context.sensitiveCheckUrl(enableUrlCheck);
+ return this;
+ }
/**
* 构建默认的上下文
+ *
* @return 结果
* @since 0.0.4
*/
@@ -105,13 +130,16 @@ public class SensitiveWordBs {
wordContext.ignoreRepeat(true);
// 开坿 ¡éªŒ
- wordContext.sensitiveNumCheck(true);
- wordContext.sensitiveEmailCheck(true);
+ wordContext.sensitiveCheckNum(true);
+ wordContext.sensitiveCheckEmail(true);
+ wordContext.sensitiveCheckUrl(true);
return wordContext;
}
+
/**
* 是å¦åŒ…嫿•感è¯
+ *
* @param target ç›®æ ‡å—符串
* @return 是å¦
* @since 0.0.1
@@ -124,6 +152,7 @@ public class SensitiveWordBs {
* è¿”å›žæ‰€æœ‰çš„æ•æ„Ÿè¯
* 1. 这里是默认去é‡çš„,且是有åºçš„。
* 2. 如果ä¸å˜åœ¨ï¼Œè¿”回空列表
+ *
* @param target ç›®æ ‡å—符串
* @return æ•æ„Ÿè¯åˆ—表
* @since 0.0.1
@@ -135,6 +164,7 @@ public class SensitiveWordBs {
/**
* è¿”å›žç¬¬ä¸€ä¸ªæ•æ„Ÿè¯
* (1)如果ä¸å˜åœ¨ï¼Œåˆ™è¿”回 {@code null}
+ *
* @param target ç›®æ ‡å—符串
* @return æ•æ„Ÿè¯
* @since 0.0.1
@@ -145,7 +175,8 @@ public class SensitiveWordBs {
/**
* æ›¿æ¢æ‰€æœ‰å†…容
- * @param target ç›®æ ‡å—符串
+ *
+ * @param target ç›®æ ‡å—符串
* @param replaceChar 替æ¢ä¸ºçš„ char
* @return 替æ¢åŽç»“æžœ
* @since 0.0.2
@@ -157,12 +188,13 @@ public class SensitiveWordBs {
/**
* æ›¿æ¢æ‰€æœ‰å†…容
* 1. é»˜è®¤ä½¿ç”¨ç©ºæ ¼æ›¿æ¢ï¼Œé¿å…æ˜Ÿå·æ”¹å˜ md çš„æ ¼å¼ã€‚
+ *
* @param target ç›®æ ‡å—符串
* @return 替æ¢åŽç»“æžœ
* @since 0.0.2
*/
public String replace(final String target) {
- return this.replace(target, CharConst.BLANK);
+ return this.replace(target, CharConst.STAR);
}
}
diff --git a/src/main/java/com/github/houbb/sensitive/word/bs/SensitiveWordContext.java b/src/main/java/com/github/houbb/sensitive/word/bs/SensitiveWordContext.java
index b611f62..2a1ad9d 100644
--- a/src/main/java/com/github/houbb/sensitive/word/bs/SensitiveWordContext.java
+++ b/src/main/java/com/github/houbb/sensitive/word/bs/SensitiveWordContext.java
@@ -39,7 +39,7 @@ public class SensitiveWordContext implements IWordContext {
* 是å¦è¿›è¡Œæ•æ„Ÿæ•°å—æ£€æµ‹
* @since 0.0.6
*/
- private boolean sensitiveNumCheck;
+ private boolean sensitiveCheckNum;
/**
* 是å¦å¿½ç•¥ä¸æ–‡ç¹ç®€ä½“
@@ -63,7 +63,13 @@ public class SensitiveWordContext implements IWordContext {
* 是å¦è¿›è¡Œé‚®ç®±æµ‹è¯•
* @since 0.0.9
*/
- private boolean sensitiveEmailCheck;
+ private boolean sensitiveCheckEmail;
+
+ /**
+ * 是å¦è¿›è¡Œ url 测试
+ * @since 0.0.12
+ */
+ private boolean sensitiveCheckUrl;
/**
* ç§æœ‰åŒ–æž„é€ å™¨
@@ -126,13 +132,13 @@ public class SensitiveWordContext implements IWordContext {
}
@Override
- public boolean sensitiveNumCheck() {
- return sensitiveNumCheck;
+ public boolean sensitiveCheckNum() {
+ return sensitiveCheckNum;
}
@Override
- public SensitiveWordContext sensitiveNumCheck(boolean sensitiveNumCheck) {
- this.sensitiveNumCheck = sensitiveNumCheck;
+ public SensitiveWordContext sensitiveCheckNum(boolean sensitiveCheckNum) {
+ this.sensitiveCheckNum = sensitiveCheckNum;
return this;
}
@@ -170,14 +176,24 @@ public class SensitiveWordContext implements IWordContext {
}
@Override
- public boolean sensitiveEmailCheck() {
- return sensitiveEmailCheck;
+ public boolean sensitiveCheckEmail() {
+ return sensitiveCheckEmail;
}
@Override
- public SensitiveWordContext sensitiveEmailCheck(boolean sensitiveEmailCheck) {
- this.sensitiveEmailCheck = sensitiveEmailCheck;
+ public SensitiveWordContext sensitiveCheckEmail(boolean sensitiveCheckEmail) {
+ this.sensitiveCheckEmail = sensitiveCheckEmail;
return this;
}
+ @Override
+ public boolean sensitiveCheckUrl() {
+ return sensitiveCheckUrl;
+ }
+
+ @Override
+ public SensitiveWordContext sensitiveCheckUrl(boolean sensitiveCheckUrl) {
+ this.sensitiveCheckUrl = sensitiveCheckUrl;
+ return this;
+ }
}
diff --git a/src/main/java/com/github/houbb/sensitive/word/api/ISensitiveCheck.java b/src/main/java/com/github/houbb/sensitive/word/support/check/ISensitiveCheck.java
similarity index 71%
rename from src/main/java/com/github/houbb/sensitive/word/api/ISensitiveCheck.java
rename to src/main/java/com/github/houbb/sensitive/word/support/check/ISensitiveCheck.java
index 08a3eee..a07578c 100644
--- a/src/main/java/com/github/houbb/sensitive/word/api/ISensitiveCheck.java
+++ b/src/main/java/com/github/houbb/sensitive/word/support/check/ISensitiveCheck.java
@@ -1,5 +1,6 @@
-package com.github.houbb.sensitive.word.api;
+package com.github.houbb.sensitive.word.support.check;
+import com.github.houbb.sensitive.word.api.IWordContext;
import com.github.houbb.sensitive.word.constant.enums.ValidModeEnum;
/**
@@ -33,9 +34,9 @@ public interface ISensitiveCheck {
* @return æ•æ„Ÿä¿¡æ¯å¯¹åº”的长度
* @since 0.0.5
*/
- int checkSensitive(final String txt,
- final int beginIndex,
- final ValidModeEnum validModeEnum,
- final IWordContext context);
+ SensitiveCheckResult sensitiveCheck(final String txt,
+ final int beginIndex,
+ final ValidModeEnum validModeEnum,
+ final IWordContext context);
}
diff --git a/src/main/java/com/github/houbb/sensitive/word/support/check/SensitiveCheckChain.java b/src/main/java/com/github/houbb/sensitive/word/support/check/SensitiveCheckChain.java
deleted file mode 100644
index e0c45e6..0000000
--- a/src/main/java/com/github/houbb/sensitive/word/support/check/SensitiveCheckChain.java
+++ /dev/null
@@ -1,53 +0,0 @@
-package com.github.houbb.sensitive.word.support.check;
-
-import com.github.houbb.heaven.annotation.ThreadSafe;
-import com.github.houbb.heaven.support.instance.impl.Instances;
-import com.github.houbb.heaven.util.guava.Guavas;
-import com.github.houbb.sensitive.word.api.ISensitiveCheck;
-import com.github.houbb.sensitive.word.api.IWordContext;
-import com.github.houbb.sensitive.word.constant.enums.ValidModeEnum;
-
-import java.util.List;
-
-/**
- * æ•æ„Ÿè¯æ£€æµ‹è´£ä»»é“¾æ¨¡å¼
- *
- * 这里å¯ä»¥æä¾›ä¸€ä¸ªå…¬å…±çš„父类。
- *
- *
- * DFA 算法的优化å¯ä»¥å‚考论文:
- * ã€DFA 算法】å„ç§è®ºæ–‡ã€‚
- *
- * @author binbin.hou
- * @since 0.0.5
- */
-@ThreadSafe
-public class SensitiveCheckChain implements ISensitiveCheck {
-
- @Override
- public int checkSensitive(String txt, int beginIndex, ValidModeEnum validModeEnum, IWordContext context) {
- // åˆå§‹åŒ–责任链
- List sensitiveChecks = Guavas.newArrayList();
- // é»˜è®¤æ·»åŠ æ•æ„Ÿè¯æ ¡éªŒ
- sensitiveChecks.add(Instances.singleton(SensitiveWordCheck.class));
- if(context.sensitiveNumCheck()) {
- sensitiveChecks.add(Instances.singleton(SensitiveNumCheck.class));
- }
- if(context.sensitiveEmailCheck()) {
- sensitiveChecks.add(Instances.singleton(SensitiveEmailCheck.class));
- }
-
- // 循环调用
- for(ISensitiveCheck sensitiveCheck : sensitiveChecks) {
- int result = sensitiveCheck.checkSensitive(txt, beginIndex, validModeEnum, context);
-
- if(result > 0) {
- return result;
- }
- }
-
- // 默认返回 0
- return 0;
- }
-
-}
diff --git a/src/main/java/com/github/houbb/sensitive/word/support/check/SensitiveCheckResult.java b/src/main/java/com/github/houbb/sensitive/word/support/check/SensitiveCheckResult.java
new file mode 100644
index 0000000..88d24fb
--- /dev/null
+++ b/src/main/java/com/github/houbb/sensitive/word/support/check/SensitiveCheckResult.java
@@ -0,0 +1,64 @@
+package com.github.houbb.sensitive.word.support.check;
+
+/**
+ * æ•æ„Ÿä¿¡æ¯ç›‘测接å£ç»“æžœ
+ *
+ * å¯ä»¥ä½¿ç”¨è´£ä»»é“¾çš„æ¨¡å¼ï¼Œå¾ªçŽ¯è°ƒç”¨ã€‚
+ * @author binbin.hou
+ * @since 0.0.12
+ */
+public class SensitiveCheckResult {
+
+ /**
+ * ä¸‹æ ‡
+ * @since 0.0.12
+ */
+ private int index;
+
+ /**
+ * 检测类
+ * @since 0.0.12
+ */
+ private Class extends ISensitiveCheck> checkClass;
+
+ /**
+ * 实例化
+ * @param index 返回索引
+ * @param checkClass 验è¯ç±»
+ * @return 结果
+ * @since 0.0.12
+ */
+ public static SensitiveCheckResult of(final int index,
+ final Class extends ISensitiveCheck> checkClass) {
+ SensitiveCheckResult result = new SensitiveCheckResult();
+ result.index(index).checkClass(checkClass);
+ return result;
+ }
+
+ public int index() {
+ return index;
+ }
+
+ public SensitiveCheckResult index(int index) {
+ this.index = index;
+ return this;
+ }
+
+ public Class extends ISensitiveCheck> checkClass() {
+ return checkClass;
+ }
+
+ public SensitiveCheckResult checkClass(Class extends ISensitiveCheck> checkClass) {
+ this.checkClass = checkClass;
+ return this;
+ }
+
+ @Override
+ public String toString() {
+ return "SensitiveCheckResult{" +
+ "index=" + index +
+ ", checkClass=" + checkClass +
+ '}';
+ }
+
+}
diff --git a/src/main/java/com/github/houbb/sensitive/word/support/check/impl/SensitiveCheckChain.java b/src/main/java/com/github/houbb/sensitive/word/support/check/impl/SensitiveCheckChain.java
new file mode 100644
index 0000000..2d099fa
--- /dev/null
+++ b/src/main/java/com/github/houbb/sensitive/word/support/check/impl/SensitiveCheckChain.java
@@ -0,0 +1,58 @@
+package com.github.houbb.sensitive.word.support.check.impl;
+
+import com.github.houbb.heaven.annotation.ThreadSafe;
+import com.github.houbb.heaven.support.instance.impl.Instances;
+import com.github.houbb.heaven.util.guava.Guavas;
+import com.github.houbb.sensitive.word.api.IWordContext;
+import com.github.houbb.sensitive.word.constant.enums.ValidModeEnum;
+import com.github.houbb.sensitive.word.support.check.ISensitiveCheck;
+import com.github.houbb.sensitive.word.support.check.SensitiveCheckResult;
+
+import java.util.List;
+
+/**
+ * æ•æ„Ÿè¯æ£€æµ‹è´£ä»»é“¾æ¨¡å¼
+ *
+ * 这里å¯ä»¥æä¾›ä¸€ä¸ªå…¬å…±çš„父类。
+ *
+ *
+ * DFA 算法的优化å¯ä»¥å‚考论文:
+ * ã€DFA 算法】å„ç§è®ºæ–‡ã€‚
+ *
+ * @author binbin.hou
+ * @since 0.0.5
+ */
+@ThreadSafe
+public class SensitiveCheckChain implements ISensitiveCheck {
+
+ @Override
+ public SensitiveCheckResult sensitiveCheck(String txt, int beginIndex, ValidModeEnum validModeEnum, IWordContext context) {
+ // åˆå§‹åŒ–责任链
+ List sensitiveChecks = Guavas.newArrayList();
+ // é»˜è®¤æ·»åŠ æ•æ„Ÿè¯æ ¡éªŒ
+ sensitiveChecks.add(Instances.singleton(SensitiveCheckWord.class));
+ if(context.sensitiveCheckNum()) {
+ sensitiveChecks.add(Instances.singleton(SensitiveCheckNum.class));
+ }
+ if(context.sensitiveCheckEmail()) {
+ sensitiveChecks.add(Instances.singleton(SensitiveCheckEmail.class));
+ }
+ if(context.sensitiveCheckUrl()) {
+ sensitiveChecks.add(Instances.singleton(SensitiveCheckUrl.class));
+ }
+
+ // 循环调用
+ for(ISensitiveCheck sensitiveCheck : sensitiveChecks) {
+ SensitiveCheckResult result = sensitiveCheck.sensitiveCheck(txt, beginIndex, validModeEnum, context);
+
+ if(result.index() > 0) {
+ return result;
+ }
+ }
+
+ // 这里直接进行æ£åˆ™è¡¨è¾¾å¼ç›¸å…³çš„调用。
+ // 默认返回 0
+ return SensitiveCheckResult.of(0, SensitiveCheckChain.class);
+ }
+
+}
diff --git a/src/main/java/com/github/houbb/sensitive/word/support/check/SensitiveEmailCheck.java b/src/main/java/com/github/houbb/sensitive/word/support/check/impl/SensitiveCheckEmail.java
similarity index 83%
rename from src/main/java/com/github/houbb/sensitive/word/support/check/SensitiveEmailCheck.java
rename to src/main/java/com/github/houbb/sensitive/word/support/check/impl/SensitiveCheckEmail.java
index 0325212..3718b5c 100644
--- a/src/main/java/com/github/houbb/sensitive/word/support/check/SensitiveEmailCheck.java
+++ b/src/main/java/com/github/houbb/sensitive/word/support/check/impl/SensitiveCheckEmail.java
@@ -1,12 +1,13 @@
-package com.github.houbb.sensitive.word.support.check;
+package com.github.houbb.sensitive.word.support.check.impl;
import com.github.houbb.heaven.annotation.ThreadSafe;
import com.github.houbb.heaven.support.instance.impl.Instances;
import com.github.houbb.heaven.util.lang.CharUtil;
import com.github.houbb.heaven.util.util.regex.RegexUtil;
-import com.github.houbb.sensitive.word.api.ISensitiveCheck;
import com.github.houbb.sensitive.word.api.IWordContext;
import com.github.houbb.sensitive.word.constant.enums.ValidModeEnum;
+import com.github.houbb.sensitive.word.support.check.ISensitiveCheck;
+import com.github.houbb.sensitive.word.support.check.SensitiveCheckResult;
import com.github.houbb.sensitive.word.support.format.CharFormatChain;
/**
@@ -25,10 +26,10 @@ import com.github.houbb.sensitive.word.support.format.CharFormatChain;
* @since 0.0.9
*/
@ThreadSafe
-public class SensitiveEmailCheck implements ISensitiveCheck {
+public class SensitiveCheckEmail implements ISensitiveCheck {
@Override
- public int checkSensitive(String txt, int beginIndex, ValidModeEnum validModeEnum, IWordContext context) {
+ public SensitiveCheckResult sensitiveCheck(String txt, int beginIndex, ValidModeEnum validModeEnum, IWordContext context) {
// è®°å½•æ•æ„Ÿè¯çš„长度
int lengthCount = 0;
int actualLength = 0;
@@ -59,7 +60,7 @@ public class SensitiveEmailCheck implements ISensitiveCheck {
}
}
- return actualLength;
+ return SensitiveCheckResult.of(actualLength, SensitiveCheckEmail.class);
}
/**
diff --git a/src/main/java/com/github/houbb/sensitive/word/support/check/SensitiveNumCheck.java b/src/main/java/com/github/houbb/sensitive/word/support/check/impl/SensitiveCheckNum.java
similarity index 76%
rename from src/main/java/com/github/houbb/sensitive/word/support/check/SensitiveNumCheck.java
rename to src/main/java/com/github/houbb/sensitive/word/support/check/impl/SensitiveCheckNum.java
index 166cefb..74bd783 100644
--- a/src/main/java/com/github/houbb/sensitive/word/support/check/SensitiveNumCheck.java
+++ b/src/main/java/com/github/houbb/sensitive/word/support/check/impl/SensitiveCheckNum.java
@@ -1,17 +1,12 @@
-package com.github.houbb.sensitive.word.support.check;
+package com.github.houbb.sensitive.word.support.check.impl;
import com.github.houbb.heaven.annotation.ThreadSafe;
import com.github.houbb.heaven.support.instance.impl.Instances;
-import com.github.houbb.heaven.util.io.FileUtil;
-import com.github.houbb.heaven.util.lang.NumUtil;
-import com.github.houbb.heaven.util.lang.StringUtil;
-import com.github.houbb.sensitive.word.api.ISensitiveCheck;
import com.github.houbb.sensitive.word.api.IWordContext;
import com.github.houbb.sensitive.word.constant.enums.ValidModeEnum;
+import com.github.houbb.sensitive.word.support.check.ISensitiveCheck;
+import com.github.houbb.sensitive.word.support.check.SensitiveCheckResult;
import com.github.houbb.sensitive.word.support.format.CharFormatChain;
-import com.github.houbb.sensitive.word.support.format.IgnoreNumStyleCharFormat;
-
-import java.util.List;
/**
* æ•æ„Ÿè¯ç›‘测实现
@@ -21,10 +16,10 @@ import java.util.List;
* @since 0.0.5
*/
@ThreadSafe
-public class SensitiveNumCheck implements ISensitiveCheck {
+public class SensitiveCheckNum implements ISensitiveCheck {
@Override
- public int checkSensitive(String txt, int beginIndex, ValidModeEnum validModeEnum, IWordContext context) {
+ public SensitiveCheckResult sensitiveCheck(String txt, int beginIndex, ValidModeEnum validModeEnum, IWordContext context) {
// è®°å½•æ•æ„Ÿè¯çš„长度
int lengthCount = 0;
int actualLength = 0;
@@ -55,7 +50,7 @@ public class SensitiveNumCheck implements ISensitiveCheck {
}
}
- return actualLength;
+ return SensitiveCheckResult.of(actualLength, SensitiveCheckNum.class);
}
/**
diff --git a/src/main/java/com/github/houbb/sensitive/word/support/check/impl/SensitiveCheckUrl.java b/src/main/java/com/github/houbb/sensitive/word/support/check/impl/SensitiveCheckUrl.java
new file mode 100644
index 0000000..d5760fb
--- /dev/null
+++ b/src/main/java/com/github/houbb/sensitive/word/support/check/impl/SensitiveCheckUrl.java
@@ -0,0 +1,82 @@
+package com.github.houbb.sensitive.word.support.check.impl;
+
+import com.github.houbb.heaven.annotation.CommonEager;
+import com.github.houbb.heaven.annotation.ThreadSafe;
+import com.github.houbb.heaven.support.instance.impl.Instances;
+import com.github.houbb.heaven.util.lang.CharUtil;
+import com.github.houbb.heaven.util.util.regex.RegexUtil;
+import com.github.houbb.sensitive.word.api.IWordContext;
+import com.github.houbb.sensitive.word.constant.enums.ValidModeEnum;
+import com.github.houbb.sensitive.word.support.check.ISensitiveCheck;
+import com.github.houbb.sensitive.word.support.check.SensitiveCheckResult;
+import com.github.houbb.sensitive.word.support.format.CharFormatChain;
+
+/**
+ * URL æ£åˆ™è¡¨è¾¾å¼æ£€æµ‹å®žçŽ°ã€‚
+ *
+ * 也å¯ä»¥ä¸¥æ ¼çš„ä¿ç•™ä¸‹æ¥ã€‚
+ *
+ * (1ï¼‰æš‚æ—¶å…ˆç²—ç•¥çš„å¤„ç† web-site
+ * (2)如果网å€çš„æœ€åŽä¸ºå›¾ç‰‡ç±»åž‹ï¼Œåˆ™è·³è¿‡ã€‚
+ * (3)长度超过 70,直接结æŸã€‚
+ *
+ * @author binbin.hou
+ * @since 0.0.9
+ */
+@ThreadSafe
+public class SensitiveCheckUrl implements ISensitiveCheck {
+
+ /**
+ * 最长的网å€é•¿åº¦
+ * @since 0.0.12
+ */
+ private static final int MAX_WEB_SITE_LEN = 70;
+
+ @Override
+ public SensitiveCheckResult sensitiveCheck(String txt, int beginIndex, ValidModeEnum validModeEnum, IWordContext context) {
+ // è®°å½•æ•æ„Ÿè¯çš„长度
+ int lengthCount = 0;
+ int actualLength = 0;
+
+ StringBuilder stringBuilder = new StringBuilder();
+ // è¿™é‡Œå·æ‡’直接使用 String 拼接,然åŽç»“åˆæ£åˆ™è¡¨è¾¾å¼ã€‚
+ // DFA 本质就å¯ä»¥åšæ£åˆ™è¡¨è¾¾å¼ï¼Œè¿™æ ·å®žçްä¸å…性能会差一些。
+ // åŽæœŸå¦‚果有想法,对 DFA è¿›ä¸€æ¥æ·±å…¥å¦ä¹ åŽï¼Œå°†è¿›è¡Œä¼˜åŒ–。
+ for(int i = beginIndex; i < txt.length(); i++) {
+ char currentChar = txt.charAt(i);
+ char mappingChar = Instances.singleton(CharFormatChain.class)
+ .format(currentChar, context);
+
+ if(CharUtil.isWebSiteChar(mappingChar)
+ && lengthCount <= MAX_WEB_SITE_LEN) {
+ lengthCount++;
+ stringBuilder.append(currentChar);
+
+ if(isCondition(stringBuilder.toString())) {
+ actualLength = lengthCount;
+
+ // 是å¦é历全部匹é…的模å¼
+ if(ValidModeEnum.FAIL_FAST.equals(validModeEnum)) {
+ break;
+ }
+ }
+ } else {
+ break;
+ }
+ }
+
+ return SensitiveCheckResult.of(actualLength, SensitiveCheckUrl.class);
+ }
+
+ /**
+ * 这里指定一个阈值æ¡ä»¶
+ * @param string 长度
+ * @return æ˜¯å¦æ»¡è¶³æ¡ä»¶
+ * @since 0.0.12
+ */
+ private boolean isCondition(final String string) {
+ return RegexUtil.isWebSite(string);
+ }
+
+
+}
diff --git a/src/main/java/com/github/houbb/sensitive/word/support/check/SensitiveWordCheck.java b/src/main/java/com/github/houbb/sensitive/word/support/check/impl/SensitiveCheckWord.java
similarity index 87%
rename from src/main/java/com/github/houbb/sensitive/word/support/check/SensitiveWordCheck.java
rename to src/main/java/com/github/houbb/sensitive/word/support/check/impl/SensitiveCheckWord.java
index c8206a6..9b6a364 100644
--- a/src/main/java/com/github/houbb/sensitive/word/support/check/SensitiveWordCheck.java
+++ b/src/main/java/com/github/houbb/sensitive/word/support/check/impl/SensitiveCheckWord.java
@@ -1,12 +1,13 @@
-package com.github.houbb.sensitive.word.support.check;
+package com.github.houbb.sensitive.word.support.check.impl;
import com.github.houbb.heaven.annotation.ThreadSafe;
import com.github.houbb.heaven.support.instance.impl.Instances;
import com.github.houbb.heaven.util.lang.ObjectUtil;
-import com.github.houbb.sensitive.word.api.ISensitiveCheck;
import com.github.houbb.sensitive.word.api.IWordContext;
import com.github.houbb.sensitive.word.constant.AppConst;
import com.github.houbb.sensitive.word.constant.enums.ValidModeEnum;
+import com.github.houbb.sensitive.word.support.check.ISensitiveCheck;
+import com.github.houbb.sensitive.word.support.check.SensitiveCheckResult;
import com.github.houbb.sensitive.word.support.format.CharFormatChain;
import java.util.Map;
@@ -17,10 +18,10 @@ import java.util.Map;
* @since 0.0.5
*/
@ThreadSafe
-public class SensitiveWordCheck implements ISensitiveCheck {
+public class SensitiveCheckWord implements ISensitiveCheck {
@Override
- public int checkSensitive(String txt, int beginIndex, ValidModeEnum validModeEnum, IWordContext context) {
+ public SensitiveCheckResult sensitiveCheck(String txt, int beginIndex, ValidModeEnum validModeEnum, IWordContext context) {
Map nowMap = context.sensitiveWordMap();
// è®°å½•æ•æ„Ÿè¯çš„长度
@@ -53,7 +54,7 @@ public class SensitiveWordCheck implements ISensitiveCheck {
}
}
- return actualLength;
+ return SensitiveCheckResult.of(actualLength, SensitiveCheckWord.class);
}
/**
diff --git a/src/main/java/com/github/houbb/sensitive/word/support/map/SensitiveWordMap.java b/src/main/java/com/github/houbb/sensitive/word/support/map/SensitiveWordMap.java
index c233821..35e5295 100644
--- a/src/main/java/com/github/houbb/sensitive/word/support/map/SensitiveWordMap.java
+++ b/src/main/java/com/github/houbb/sensitive/word/support/map/SensitiveWordMap.java
@@ -3,6 +3,7 @@ package com.github.houbb.sensitive.word.support.map;
import com.github.houbb.heaven.annotation.ThreadSafe;
import com.github.houbb.heaven.support.instance.impl.Instances;
import com.github.houbb.heaven.util.guava.Guavas;
+import com.github.houbb.heaven.util.io.FileUtil;
import com.github.houbb.heaven.util.lang.CharUtil;
import com.github.houbb.heaven.util.lang.ObjectUtil;
import com.github.houbb.heaven.util.lang.StringUtil;
@@ -12,7 +13,9 @@ import com.github.houbb.sensitive.word.api.IWordContext;
import com.github.houbb.sensitive.word.api.IWordMap;
import com.github.houbb.sensitive.word.constant.AppConst;
import com.github.houbb.sensitive.word.constant.enums.ValidModeEnum;
-import com.github.houbb.sensitive.word.support.check.SensitiveCheckChain;
+import com.github.houbb.sensitive.word.support.check.SensitiveCheckResult;
+import com.github.houbb.sensitive.word.support.check.impl.SensitiveCheckChain;
+import com.github.houbb.sensitive.word.support.check.impl.SensitiveCheckUrl;
import java.util.Collection;
import java.util.HashMap;
@@ -118,9 +121,9 @@ public class SensitiveWordMap implements IWordMap {
}
for (int i = 0; i < string.length(); i++) {
- int checkResult = checkSensitive(string, i, ValidModeEnum.FAIL_FAST, context);
+ SensitiveCheckResult checkResult = sensitiveCheck(string, i, ValidModeEnum.FAIL_FAST, context);
// 快速返回
- if (checkResult > 0) {
+ if (checkResult.index() > 0) {
return true;
}
}
@@ -178,9 +181,9 @@ public class SensitiveWordMap implements IWordMap {
List resultList = Guavas.newArrayList();
for (int i = 0; i < text.length(); i++) {
- int wordLength = checkSensitive(text, i, ValidModeEnum.FAIL_OVER, context);
-
+ SensitiveCheckResult checkResult = sensitiveCheck(text, i, ValidModeEnum.FAIL_OVER, context);
// 命ä¸
+ int wordLength = checkResult.index();
if (wordLength > 0) {
// ä¿å˜æ•感è¯
String sensitiveWord = text.substring(i, i + wordLength);
@@ -223,12 +226,22 @@ public class SensitiveWordMap implements IWordMap {
for (int i = 0; i < target.length(); i++) {
char currentChar = target.charAt(i);
// 内层直接从 i 开始往åŽé历,这个算法的,获å–第一个匹é…çš„å•è¯
- int wordLength = checkSensitive(target, i, ValidModeEnum.FAIL_OVER, context);
+ SensitiveCheckResult checkResult = sensitiveCheck(target, i, ValidModeEnum.FAIL_OVER, context);
// æ•æ„Ÿè¯
+ int wordLength = checkResult.index();
if(wordLength > 0) {
- String replaceStr = CharUtil.repeat(replaceChar, wordLength);
- resultBuilder.append(replaceStr);
+ // æ˜¯å¦æ‰§è¡Œæ›¿æ¢
+ Class checkClass = checkResult.checkClass();
+ String string = target.substring(i, i+wordLength);
+ if(SensitiveCheckUrl.class.equals(checkClass)
+ && FileUtil.isImage(string)) {
+ // 直接使用原始内容,é¿å… markdown 图片转æ¢å¤±è´¥
+ resultBuilder.append(string);
+ } else {
+ String replaceStr = CharUtil.repeat(replaceChar, wordLength);
+ resultBuilder.append(replaceStr);
+ }
// ç›´æŽ¥è·³è¿‡æ•æ„Ÿè¯çš„长度
i += wordLength-1;
@@ -242,13 +255,13 @@ public class SensitiveWordMap implements IWordMap {
}
@Override
- public int checkSensitive(String txt, int beginIndex, ValidModeEnum validModeEnum, IWordContext context) {
+ public SensitiveCheckResult sensitiveCheck(String txt, int beginIndex, ValidModeEnum validModeEnum, IWordContext context) {
// é»˜è®¤æ‰§è¡Œæ•æ„Ÿè¯æ“作
context.sensitiveWordMap(innerWordMap);
// 责任链模å¼è°ƒç”¨
return Instances.singleton(SensitiveCheckChain.class)
- .checkSensitive(txt, beginIndex, validModeEnum, context);
+ .sensitiveCheck(txt, beginIndex, validModeEnum, context);
}
}
diff --git a/src/test/java/com/github/houbb/sensitive/word/bs/SensitiveWordBsChineseTest.java b/src/test/java/com/github/houbb/sensitive/word/bs/SensitiveWordBsChineseTest.java
index 1fac846..5fa0485 100644
--- a/src/test/java/com/github/houbb/sensitive/word/bs/SensitiveWordBsChineseTest.java
+++ b/src/test/java/com/github/houbb/sensitive/word/bs/SensitiveWordBsChineseTest.java
@@ -23,7 +23,7 @@ public class SensitiveWordBsChineseTest {
final String text = "我爱我的祖国和五星紅旗。";
List wordList = SensitiveWordBs.newInstance().findAll(text);
- Assert.assertEquals("[五星紅旗]", wordList.toString());
+ Assert.assertEquals("[祖国, 五星紅旗]", wordList.toString());
}
}
diff --git a/src/test/java/com/github/houbb/sensitive/word/bs/SensitiveWordBsEmailTest.java b/src/test/java/com/github/houbb/sensitive/word/bs/SensitiveWordBsEmailTest.java
index 1b9d4d8..c930774 100644
--- a/src/test/java/com/github/houbb/sensitive/word/bs/SensitiveWordBsEmailTest.java
+++ b/src/test/java/com/github/houbb/sensitive/word/bs/SensitiveWordBsEmailTest.java
@@ -35,7 +35,7 @@ public class SensitiveWordBsEmailTest {
final String text = "楼主好人,邮箱 123456789@xx.com";
List wordList = SensitiveWordBs.newInstance().findAll(text);
- Assert.assertEquals("[邮箱, 123456789]", wordList.toString());
+ Assert.assertEquals("[邮箱, 123456789, xx.com]", wordList.toString());
}
}
diff --git a/src/test/java/com/github/houbb/sensitive/word/bs/SensitiveWordBsUrlTest.java b/src/test/java/com/github/houbb/sensitive/word/bs/SensitiveWordBsUrlTest.java
new file mode 100644
index 0000000..e13a1c7
--- /dev/null
+++ b/src/test/java/com/github/houbb/sensitive/word/bs/SensitiveWordBsUrlTest.java
@@ -0,0 +1,50 @@
+package com.github.houbb.sensitive.word.bs;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.List;
+
+/**
+ * project: sensitive-word-SensitiveWordBsTest
+ * create on 2020/1/7 23:43
+ *
+ * @author Administrator
+ * @since 0.0.12
+ */
+public class SensitiveWordBsUrlTest {
+
+ /**
+ * å¿½ç•¥ä¸æ–‡ç¹ç®€ä½“
+ * @since 0.0.12
+ */
+ @Test
+ public void commonUrlTest() {
+ final String text = "点击链接 www.baidu.comæŸ¥çœ‹ç”æ¡ˆ";
+
+ List wordList = SensitiveWordBs.newInstance().findAll(text);
+ Assert.assertEquals("[链接, www.baidu.com]", wordList.toString());
+
+ Assert.assertEquals("点击** *************æŸ¥çœ‹ç”æ¡ˆ", SensitiveWordBs
+ .newInstance().replace(text));
+ }
+
+ /**
+ * 图片测试
+ *
+ * (1)å¯ä»¥æ£€æµ‹
+ * (2ï¼‰é»˜è®¤ä¸æ›¿æ¢
+ *
+ * @since 0.0.12
+ */
+ @Test
+ public void imageUrlTest() {
+ final String text = "åŒå‡»æŸ¥çœ‹å¤§å›¾ www.big-image.png查看";
+
+ List wordList = SensitiveWordBs.newInstance().findAll(text);
+ Assert.assertEquals("[www.big-image.png]", wordList.toString());
+
+ Assert.assertEquals(text, SensitiveWordBs.newInstance().replace(text));
+ }
+
+}