From f76b2293e8e9d669c6f29cfcbf714e7a2eb8bbab Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com>
Date: Fri, 24 Mar 2023 15:23:29 +0800
Subject: [PATCH] =?UTF-8?q?fix:=E6=95=8F=E6=84=9F=E8=AF=8D=E8=BF=87?=
=?UTF-8?q?=E6=BB=A4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 15 ++++++
.../config/SensitiveConfig.java | 52 +++++++++++++++++++
.../sensitive/MySensitiveWordReplace.java | 31 +++++++++++
.../service/sensitive/MyWordAllow.java | 43 +++++++++++++++
.../service/sensitive/MyWordDeny.java | 43 +++++++++++++++
.../utils/SensitiveWordUtil.java | 40 ++++++++++++++
src/main/resources/myNotSensitiveWords.txt | 1 +
src/main/resources/mySensitiveWords.txt | 2 +
.../kwan/springbootkwan/SensitiveTest.java | 25 +++++++++
9 files changed, 252 insertions(+)
create mode 100644 src/main/java/com/kwan/springbootkwan/config/SensitiveConfig.java
create mode 100644 src/main/java/com/kwan/springbootkwan/service/sensitive/MySensitiveWordReplace.java
create mode 100644 src/main/java/com/kwan/springbootkwan/service/sensitive/MyWordAllow.java
create mode 100644 src/main/java/com/kwan/springbootkwan/service/sensitive/MyWordDeny.java
create mode 100644 src/main/java/com/kwan/springbootkwan/utils/SensitiveWordUtil.java
create mode 100644 src/main/resources/myNotSensitiveWords.txt
create mode 100644 src/main/resources/mySensitiveWords.txt
create mode 100644 src/test/java/com/kwan/springbootkwan/SensitiveTest.java
diff --git a/pom.xml b/pom.xml
index 5c8dd73..49ef70f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -161,6 +161,21 @@
fastjson2
2.0.23
+
+
+ com.github.houbb
+ sensitive-word
+ 0.2.1
+
+
+
+ junit
+ junit
+ 4.12
+ test
+
+
+
diff --git a/src/main/java/com/kwan/springbootkwan/config/SensitiveConfig.java b/src/main/java/com/kwan/springbootkwan/config/SensitiveConfig.java
new file mode 100644
index 0000000..fb9de3d
--- /dev/null
+++ b/src/main/java/com/kwan/springbootkwan/config/SensitiveConfig.java
@@ -0,0 +1,52 @@
+package com.kwan.springbootkwan.config;
+
+import com.github.houbb.sensitive.word.api.IWordAllow;
+import com.github.houbb.sensitive.word.api.IWordDeny;
+import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
+import com.github.houbb.sensitive.word.support.allow.WordAllows;
+import com.github.houbb.sensitive.word.support.deny.WordDenys;
+import com.kwan.springbootkwan.service.sensitive.MyWordAllow;
+import com.kwan.springbootkwan.service.sensitive.MyWordDeny;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+
+@Configuration
+public class SensitiveConfig {
+
+ // 配置默认敏感词 + 自定义敏感词
+ IWordDeny wordDeny = WordDenys.chains(WordDenys.system(), new MyWordDeny());
+ // 配置默认非敏感词 + 自定义非敏感词
+ IWordAllow wordAllow = WordAllows.chains(WordAllows.system(), new MyWordAllow());
+
+ @Bean
+ public SensitiveWordBs sensitiveWordBs() {
+ return SensitiveWordBs.newInstance()
+ // 忽略大小写
+ .ignoreCase(true)
+ // 忽略半角圆角
+ .ignoreWidth(true)
+ // 忽略数字的写法
+ .ignoreNumStyle(true)
+ // 忽略中文的书写格式:简繁体
+ .ignoreChineseStyle(true)
+ // 忽略英文的书写格式
+ .ignoreEnglishStyle(true)
+ // 忽略重复词
+ .ignoreRepeat(false)
+ // 是否启用数字检测
+ .enableNumCheck(true)
+ // 是否启用邮箱检测
+ .enableEmailCheck(true)
+ // 是否启用链接检测
+ .enableUrlCheck(true)
+ // 数字检测,自定义指定长度
+ .numCheckLen(8)
+ // 配置自定义敏感词
+ .wordDeny(wordDeny)
+ // 配置非自定义敏感词
+ .wordAllow(wordAllow)
+ .init();
+ }
+
+}
diff --git a/src/main/java/com/kwan/springbootkwan/service/sensitive/MySensitiveWordReplace.java b/src/main/java/com/kwan/springbootkwan/service/sensitive/MySensitiveWordReplace.java
new file mode 100644
index 0000000..8aacc89
--- /dev/null
+++ b/src/main/java/com/kwan/springbootkwan/service/sensitive/MySensitiveWordReplace.java
@@ -0,0 +1,31 @@
+package com.kwan.springbootkwan.service.sensitive;
+
+import com.github.houbb.heaven.util.lang.CharUtil;
+import com.github.houbb.sensitive.word.api.ISensitiveWordReplace;
+import com.github.houbb.sensitive.word.api.ISensitiveWordReplaceContext;
+
+
+/**
+ * 自定义敏感词
+ *
+ * @author : qinyingjie
+ * @version : 2.2.0
+ * @date : 2023/3/24 15:09
+ */
+public class MySensitiveWordReplace implements ISensitiveWordReplace {
+ @Override
+ public String replace(ISensitiveWordReplaceContext context) {
+ String sensitiveWord = context.sensitiveWord();
+ // 自定义不同的敏感词替换策略,可以从数据库等地方读取
+ if ("五星红旗".equals(sensitiveWord)) {
+ return "国家旗帜";
+ }
+ if ("毛主席".equals(sensitiveWord)) {
+ return "教员";
+ }
+ // 其他默认使用 * 代替
+ int wordLength = context.wordLength();
+ return CharUtil.repeat('*', wordLength);
+ }
+}
+
diff --git a/src/main/java/com/kwan/springbootkwan/service/sensitive/MyWordAllow.java b/src/main/java/com/kwan/springbootkwan/service/sensitive/MyWordAllow.java
new file mode 100644
index 0000000..cbe500b
--- /dev/null
+++ b/src/main/java/com/kwan/springbootkwan/service/sensitive/MyWordAllow.java
@@ -0,0 +1,43 @@
+package com.kwan.springbootkwan.service.sensitive;
+
+
+import com.github.houbb.sensitive.word.api.IWordAllow;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.Resource;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+
+
+/**
+ * 自定义非敏感词
+ * 注意每一行为一个非敏感词,单行不能只包括空格,否则,也会把空格识别为非敏感词
+ *
+ * @author : qinyingjie
+ * @version : 2.2.0
+ * @date : 2023/3/24 15:06
+ */
+@Slf4j
+public class MyWordAllow implements IWordAllow {
+
+ @Override
+ public List allow() {
+ List list = new ArrayList<>();
+ ;
+ try {
+ Resource myAllowWords = new ClassPathResource("myNotSensitiveWords.txt");
+ Path myAllowWordsPath = Paths.get(myAllowWords.getFile().getPath());
+ list = Files.readAllLines(myAllowWordsPath, StandardCharsets.UTF_8);
+ } catch (IOException ioException) {
+ log.error("读取非敏感词文件错误!" + ioException.getMessage());
+ }
+ return list;
+ }
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/kwan/springbootkwan/service/sensitive/MyWordDeny.java b/src/main/java/com/kwan/springbootkwan/service/sensitive/MyWordDeny.java
new file mode 100644
index 0000000..ea65dd0
--- /dev/null
+++ b/src/main/java/com/kwan/springbootkwan/service/sensitive/MyWordDeny.java
@@ -0,0 +1,43 @@
+package com.kwan.springbootkwan.service.sensitive;
+
+import com.github.houbb.sensitive.word.api.IWordDeny;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.Resource;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 自定义敏感词
+ * 注意每一行为一个敏感词,单行不能只包括空格,否则,也会把空格识别为敏感词
+ *
+ * @author : qinyingjie
+ * @version : 2.2.0
+ * @date : 2023/3/24 15:07
+ */
+@Slf4j
+public class MyWordDeny implements IWordDeny {
+
+ @Override
+ public List deny() {
+ List list = new ArrayList<>();
+ try {
+ Resource mySensitiveWords = new ClassPathResource("mySensitiveWords.txt");
+ Path mySensitiveWordsPath = Paths.get(mySensitiveWords.getFile().getPath());
+ list = Files.readAllLines(mySensitiveWordsPath, StandardCharsets.UTF_8);
+ } catch (IOException ioException) {
+ log.error("读取敏感词文件错误!" + ioException.getMessage());
+ }
+ return list;
+ }
+
+}
+
+
+
diff --git a/src/main/java/com/kwan/springbootkwan/utils/SensitiveWordUtil.java b/src/main/java/com/kwan/springbootkwan/utils/SensitiveWordUtil.java
new file mode 100644
index 0000000..7bea1de
--- /dev/null
+++ b/src/main/java/com/kwan/springbootkwan/utils/SensitiveWordUtil.java
@@ -0,0 +1,40 @@
+package com.kwan.springbootkwan.utils;
+
+import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+
+@Component
+public class SensitiveWordUtil {
+
+ @Autowired
+ private SensitiveWordBs sensitiveWordBs;
+
+ // 刷新敏感词库与非敏感词库缓存
+ public void refresh() {
+ sensitiveWordBs.init();
+ }
+
+ // 判断是否含有敏感词
+ public boolean contains(String text) {
+ return sensitiveWordBs.contains(text);
+ }
+
+ // 指定替换符进行替换敏感词
+ public String replace(String text, char replaceChar) {
+ return sensitiveWordBs.replace(text, replaceChar);
+ }
+
+ // 使用默认替换符 * 进行替换敏感词
+ public String replace(String text) {
+ return sensitiveWordBs.replace(text);
+ }
+
+ // 返回所有敏感词
+ public List findAll(String text) {
+ return sensitiveWordBs.findAll(text);
+ }
+}
+
diff --git a/src/main/resources/myNotSensitiveWords.txt b/src/main/resources/myNotSensitiveWords.txt
new file mode 100644
index 0000000..110129a
--- /dev/null
+++ b/src/main/resources/myNotSensitiveWords.txt
@@ -0,0 +1 @@
+小码哥
\ No newline at end of file
diff --git a/src/main/resources/mySensitiveWords.txt b/src/main/resources/mySensitiveWords.txt
new file mode 100644
index 0000000..8bb6cf6
--- /dev/null
+++ b/src/main/resources/mySensitiveWords.txt
@@ -0,0 +1,2 @@
+国家
+马化腾
\ No newline at end of file
diff --git a/src/test/java/com/kwan/springbootkwan/SensitiveTest.java b/src/test/java/com/kwan/springbootkwan/SensitiveTest.java
new file mode 100644
index 0000000..3b7b723
--- /dev/null
+++ b/src/test/java/com/kwan/springbootkwan/SensitiveTest.java
@@ -0,0 +1,25 @@
+package com.kwan.springbootkwan;
+
+import com.kwan.springbootkwan.utils.SensitiveWordUtil;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringRunner;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest
+@ContextConfiguration(classes = SpringBootKwanApplication.class)
+public class SensitiveTest {
+
+ @Autowired
+ private SensitiveWordUtil sensitiveWordUtil;
+
+ @Test
+ public void test() {
+ String result = sensitiveWordUtil.replace("国家 哇 nnd 复活 马化腾");
+ System.out.println(result);
+ }
+}
+
--
GitLab