diff --git a/pom.xml b/pom.xml index 5c8dd738eefadd6c3d926fd32f0531299703682b..49ef70f4de77bb521c2a78eeb94ad808f010a851 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 0000000000000000000000000000000000000000..fb9de3d380e8ac26948a437c7875e240516b8554 --- /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 0000000000000000000000000000000000000000..8aacc894d7923cf335b67e00d61be32d0b1a75de --- /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 0000000000000000000000000000000000000000..cbe500badc1640a96d9be2c9b59b72298f828551 --- /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 0000000000000000000000000000000000000000..ea65dd0fe8474226089676ba090466eedd65b013 --- /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 0000000000000000000000000000000000000000..7bea1de03df27b02e22a53171671a81116cac04b --- /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 0000000000000000000000000000000000000000..110129a5bd6e29e34bf42433b297b1833a901334 --- /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 0000000000000000000000000000000000000000..8bb6cf6d3c95fac5b27b4ef985b2ff4fb17b2747 --- /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 0000000000000000000000000000000000000000..3b7b72374326737ff871521c55e4295ab891d4ca --- /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); + } +} +