若依 ruoyi-vue SpringBoot聊天敏感词过滤sensitive-word

本文主要是介绍若依 ruoyi-vue SpringBoot聊天敏感词过滤sensitive-word,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

组件地址
https://github.com/houbb/sensitive-word

网上博客版本不是最新,查看官方文档,基于0.16.1整理总结,快速上手

pom文件引入

<dependency><groupId>com.github.houbb</groupId><artifactId>sensitive-word</artifactId><version>0.16.1</version>
</dependency>

配置类

package com.huida.tzly.sensitive;import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.github.houbb.sensitive.word.support.allow.WordAllows;
import com.github.houbb.sensitive.word.support.ignore.SensitiveWordCharIgnores;
import com.github.houbb.sensitive.word.support.resultcondition.WordResultConditions;
import com.github.houbb.sensitive.word.support.tag.WordTags;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class SpringSensitiveWordConfig {@Autowiredprivate MyDdWordAllow myDdWordAllow;@Autowiredprivate MyDdWordDeny myDdWordDeny;/*** 初始化引导类** @return 初始化引导类* @since 1.0.0*/@Beanpublic SensitiveWordBs sensitiveWordBs() {SensitiveWordBs sensitiveWordBs = SensitiveWordBs.newInstance()//白名单.wordAllow(WordAllows.chains(WordAllows.defaults(), myDdWordAllow))//黑名单.wordDeny(myDdWordDeny)//否启用敏感单词检测 fuck.enableWordCheck(true)//忽略大小写.ignoreCase(true)//忽略半角圆角 fuck the bad words..ignoreWidth(true)//忽略中文繁简体样式.ignoreChineseStyle(true)//忽略英文样式 Ⓕⓤc⒦ the bad words.ignoreEnglishStyle(true)// 是否忽略重复 ⒻⒻⒻfⓤuⓤ⒰cⓒ⒦ the bad words.ignoreRepeat(false)// 连续数字检测 过滤手机号、QQ等//.enableNumCheck(true)//指定连续数字检测长度//.numCheckLen(8)//邮箱检测//.enableEmailCheck(true)//网址检测 www.baidu.com//.enableUrlCheck(true)//忽略数字的写法,这个是我的微信:9⓿二肆⁹₈③⑸⒋➃㈤㊄//.ignoreNumStyle(true)//词对应的标签 属于政治或人身攻击.wordTag(WordTags.none())//忽略的字符.charIgnore(SensitiveWordCharIgnores.defaults())//针对匹配的敏感词额外加工,比如可以限制英文单词必须全匹配.wordResultCondition(WordResultConditions.alwaysTrue()).init();return sensitiveWordBs;}}

从数据库加载自定义敏感词白名单黑名单

package com.huida.tzly.sensitive;import com.github.houbb.sensitive.word.api.IWordAllow;
import com.huida.tzly.domain.TzLySensitiveWord;
import com.huida.tzly.service.TzLySensitiveWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;/*** @author binbin.hou* @since 1.0.0*/
@Component
public class MyDdWordAllow implements IWordAllow {@Autowiredprivate TzLySensitiveWordService tzLySensitiveWordService;@Overridepublic List<String> allow() {// 数据库查询List<TzLySensitiveWord> list = tzLySensitiveWordService.selectTzLySensitiveWord(TzLySensitiveWord.builder().type(1).build());List<String> allowList = list.stream().map(TzLySensitiveWord::getName).collect(Collectors.toList());return allowList;}}
package com.huida.tzly.sensitive;import com.github.houbb.sensitive.word.api.IWordDeny;
import com.huida.tzly.domain.TzLySensitiveWord;
import com.huida.tzly.service.TzLySensitiveWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;/*** @author binbin.hou* @since 1.0.0*/
@Component
public class MyDdWordDeny implements IWordDeny {@Autowiredprivate TzLySensitiveWordService tzLySensitiveWordService;@Overridepublic List<String> deny() {// 数据库查询List<TzLySensitiveWord> list = tzLySensitiveWordService.selectTzLySensitiveWord(TzLySensitiveWord.builder().type(0).build());List<String> denyList = list.stream().map(TzLySensitiveWord::getName).collect(Collectors.toList());return denyList;}}
    @Overridepublic String checkMessage(Long messageId, String message, List<TzLyUser> tzLyUserList) {//找出全部敏感词List<String> sensitiveWordList = SensitiveWordHelper.findAll(message);// 判断发送的消息是否包含扬言敏感词if (CollectionUtils.isEmpty(sensitiveWordList)) {return null;}String finalMessage = message;//将敏感词替换成**for (String word : sensitiveWordList) {StringBuilder sb = new StringBuilder();for (int i = 0; i < word.length(); i++) {sb.append("*");}finalMessage = finalMessage.replace(word, sb.toString());}// 批量插入敏感词记录mergeBatch(messageId, tzLyUserList, StrUtil.join(",", sensitiveWordList),message);return finalMessage;}

这篇关于若依 ruoyi-vue SpringBoot聊天敏感词过滤sensitive-word的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1007594

相关文章

Java中的Cursor使用详解

《Java中的Cursor使用详解》本文介绍了Java中的Cursor接口及其在大数据集处理中的优势,包括逐行读取、分页处理、流控制、动态改变查询、并发控制和减少网络流量等,感兴趣的朋友一起看看吧... 最近看代码,有一段代码涉及到Cursor,感觉写法挺有意思的。注意是Cursor,而不是Consumer

解决java.lang.NullPointerException问题(空指针异常)

《解决java.lang.NullPointerException问题(空指针异常)》本文详细介绍了Java中的NullPointerException异常及其常见原因,包括对象引用为null、数组元... 目录Java.lang.NullPointerException(空指针异常)NullPointer

javaScript在表单提交时获取表单数据的示例代码

《javaScript在表单提交时获取表单数据的示例代码》本文介绍了五种在JavaScript中获取表单数据的方法:使用FormData对象、手动提取表单数据、使用querySelector获取单个字... 方法 1:使用 FormData 对象FormData 是一个方便的内置对象,用于获取表单中的键值

Vue ElementUI中Upload组件批量上传的实现代码

《VueElementUI中Upload组件批量上传的实现代码》ElementUI中Upload组件批量上传通过获取upload组件的DOM、文件、上传地址和数据,封装uploadFiles方法,使... ElementUI中Upload组件如何批量上传首先就是upload组件 <el-upl

前端知识点之Javascript选择输入框confirm用法

《前端知识点之Javascript选择输入框confirm用法》:本文主要介绍JavaScript中的confirm方法的基本用法、功能特点、注意事项及常见用途,文中通过代码介绍的非常详细,对大家... 目录1. 基本用法2. 功能特点①阻塞行为:confirm 对话框会阻塞脚本的执行,直到用户作出选择。②

SpringBoot项目注入 traceId 追踪整个请求的日志链路(过程详解)

《SpringBoot项目注入traceId追踪整个请求的日志链路(过程详解)》本文介绍了如何在单体SpringBoot项目中通过手动实现过滤器或拦截器来注入traceId,以追踪整个请求的日志链... SpringBoot项目注入 traceId 来追踪整个请求的日志链路,有了 traceId, 我们在排

Java实战之利用POI生成Excel图表

《Java实战之利用POI生成Excel图表》ApachePOI是Java生态中处理Office文档的核心工具,这篇文章主要为大家详细介绍了如何在Excel中创建折线图,柱状图,饼图等常见图表,需要的... 目录一、环境配置与依赖管理二、数据源准备与工作表构建三、图表生成核心步骤1. 折线图(Line Ch

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

CSS3 最强二维布局系统之Grid 网格布局

《CSS3最强二维布局系统之Grid网格布局》CS3的Grid网格布局是目前最强的二维布局系统,可以同时对列和行进行处理,将网页划分成一个个网格,可以任意组合不同的网格,做出各种各样的布局,本文介... 深入学习 css3 目前最强大的布局系统 Grid 网格布局Grid 网格布局的基本认识Grid 网

HTML5中下拉框<select>标签的属性和样式详解

《HTML5中下拉框<select>标签的属性和样式详解》在HTML5中,下拉框(select标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中选择值的方式,本文将深入探讨select标签的... 在html5中,下拉框(<select>标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中