若依 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

相关文章

Spring Security+JWT如何实现前后端分离权限控制

《SpringSecurity+JWT如何实现前后端分离权限控制》本篇将手把手教你用SpringSecurity+JWT搭建一套完整的登录认证与权限控制体系,具有很好的参考价值,希望对大家... 目录Spring Security+JWT实现前后端分离权限控制实战一、为什么要用 JWT?二、JWT 基本结构

java解析jwt中的payload的用法

《java解析jwt中的payload的用法》:本文主要介绍java解析jwt中的payload的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解析jwt中的payload1. 使用 jjwt 库步骤 1:添加依赖步骤 2:解析 JWT2. 使用 N

springboot项目如何开启https服务

《springboot项目如何开启https服务》:本文主要介绍springboot项目如何开启https服务方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录springboot项目开启https服务1. 生成SSL证书密钥库使用keytool生成自签名证书将

Java实现优雅日期处理的方案详解

《Java实现优雅日期处理的方案详解》在我们的日常工作中,需要经常处理各种格式,各种类似的的日期或者时间,下面我们就来看看如何使用java处理这样的日期问题吧,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言一、日期的坑1.1 日期格式化陷阱1.2 时区转换二、优雅方案的进阶之路2.1 线程安全重构2

Java中的JSONObject详解

《Java中的JSONObject详解》:本文主要介绍Java中的JSONObject详解,需要的朋友可以参考下... Java中的jsONObject详解一、引言在Java开发中,处理JSON数据是一种常见的需求。JSONObject是处理JSON对象的一个非常有用的类,它提供了一系列的API来操作J

SpringBoot多数据源配置完整指南

《SpringBoot多数据源配置完整指南》在复杂的企业应用中,经常需要连接多个数据库,SpringBoot提供了灵活的多数据源配置方式,以下是详细的实现方案,需要的朋友可以参考下... 目录一、基础多数据源配置1. 添加依赖2. 配置多个数据源3. 配置数据源Bean二、JPA多数据源配置1. 配置主数据

将Java程序打包成EXE文件的实现方式

《将Java程序打包成EXE文件的实现方式》:本文主要介绍将Java程序打包成EXE文件的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录如何将Java程序编程打包成EXE文件1.准备Java程序2.生成JAR包3.选择并安装打包工具4.配置Launch4

SpringBoot内嵌Tomcat临时目录问题及解决

《SpringBoot内嵌Tomcat临时目录问题及解决》:本文主要介绍SpringBoot内嵌Tomcat临时目录问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录SprinjavascriptgBoot内嵌Tomcat临时目录问题1.背景2.方案3.代码中配置t

SpringBoot使用GZIP压缩反回数据问题

《SpringBoot使用GZIP压缩反回数据问题》:本文主要介绍SpringBoot使用GZIP压缩反回数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot使用GZIP压缩反回数据1、初识gzip2、gzip是什么,可以干什么?3、Spr

HTML5中的Microdata与历史记录管理详解

《HTML5中的Microdata与历史记录管理详解》Microdata作为HTML5新增的一个特性,它允许开发者在HTML文档中添加更多的语义信息,以便于搜索引擎和浏览器更好地理解页面内容,本文将探... 目录html5中的Mijscrodata与历史记录管理背景简介html5中的Microdata使用M