欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > Springboot 敏感词过滤

Springboot 敏感词过滤

2024/10/24 5:24:23 来源:https://blog.csdn.net/wangqiaowq/article/details/140180016  浏览:    关键词:Springboot 敏感词过滤

参考:网站是怎么屏蔽脏话的呢:简单学会SpringBoot项目敏感词、违规词过滤方案_springboot 项目关键词过滤-CSDN博客

【敏感词过滤】_wx60d2a462203aa的技术博客_51CTO博客

1、添加依赖

<dependency><groupId>com.github.houbb</groupId><artifactId>sensitive-word</artifactId><version>0.17.0</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.1</version><scope>test</scope></dependency>

2、SensitiveConfig
 

@Configuration
public class SensitiveConfig {@Autowiredprivate MyWordAllow myWordAllow;@Autowiredprivate MyWordDeny myWordDeny;/*** 初始化引导类* @return 初始化引导类* @since 1.0.0*/@Beanpublic SensitiveWordBs sensitiveWordBs() {// 配置默认敏感词 + 自定义敏感词IWordDeny wordDeny = WordDenys.chains(WordDenys.defaults(), myWordDeny);// 配置默认非敏感词 + 自定义非敏感词IWordAllow wordAllow = WordAllows.chains(WordAllows.defaults(), myWordAllow);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();}

3、自定义 敏感词

@Slf4j
@Component
public class MyWordDeny implements IWordDeny {@Overridepublic List<String> deny() {List<String> list = new ArrayList<String>();;try {Resource mySensitiveWords = new ClassPathResource("sensitive/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;}}

自定义非敏感词

@Slf4j
@Component
public class MyWordAllow implements IWordAllow {@Overridepublic List<String> allow() {List<String> list = new ArrayList<String>();;try {Resource myAllowWords = new ClassPathResource("sensitive/myAllowWords.txt");Path myAllowWordsPath = Paths.get(myAllowWords.getFile().getPath());list =  Files.readAllLines(myAllowWordsPath, StandardCharsets.UTF_8);} catch (IOException ioException) {log.error("读取非敏感词文件错误!"+ ioException.getMessage());}return list;}}

3、 Service类
 

@Component
public class SensitiveWordService {@Autowiredprivate SensitiveWordBs sensitiveWordBs;// 刷新敏感词库与非敏感词库缓存public void refresh(){sensitiveWordBs.init();}// 判断是否含有敏感词public boolean contains(String text){return sensitiveWordBs.contains(text);}// 使用默认替换符 * 进行替换敏感词public String replace(String text){return sensitiveWordBs.replace(text);}// 返回所有敏感词public List<String> findAll(String text){return sensitiveWordBs.findAll(text);}
}

 4、测试类

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MyGptApplication.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = MyGptApplication.class)
public class SensitiveTest {@Autowiredprivate SensitiveWordService sensitiveWordService;@Testpublic void test() {String s = "赌博 test";String s1 = sensitiveWordService.replace(s);Boolean flag = sensitiveWordService.contains(s);if (flag) {System.out.println("请调整问题,避免受限内容,我们将更好地协助您。");}System.out.println(s1);}}

5、文本目录

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com