在论坛、聊天、评论等用户生成内容(ugc)为核心的功能中,完全依赖用户自觉是不现实的。
为了防止个别用户发布违规、广告或恶意言论,从而污染社区环境、带来法律风险或伤害其他用户,我们需要自行搭建敏感词过滤系统来防御。
1.引入依赖
<!-- 敏感词工具包 -->
<dependency>
<groupid>com.github.houbb</groupid>
<artifactid>sensitive-word</artifactid>
<version>0.21.0</version>
</dependency>2.定义自定义敏感词类
package com.heyin.sass.portal.util.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 zenghuilin
*/
@slf4j
public class myworddeny implements iworddeny {
@override
public list<string> deny() {
list<string> list = new arraylist<>();
try {
resource mysensitivewords = new classpathresource("sensitive/sensitive_word_deny.txt");
path mysensitivewordspath = paths.get(mysensitivewords.getfile().getpath());
list = files.readalllines(mysensitivewordspath, standardcharsets.utf_8);
} catch (ioexception ioexception) {
log.error("读取敏感词文件错误!" + ioexception.getmessage());
}
return list;
}
}3.定义自定义非敏感类
package com.heyin.sass.portal.util.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 zenghuilin
*/
@slf4j
public class mywordallow implements iwordallow {
@override
public list<string> allow() {
list<string> list = new arraylist<>();
try {
resource mysensitivewords = new classpathresource("sensitive/sensitive_word_allow.txt");
path mysensitivewordspath = paths.get(mysensitivewords.getfile().getpath());
list = files.readalllines(mysensitivewordspath, standardcharsets.utf_8);
} catch (ioexception ioexception) {
log.error("读取敏感词文件错误!" + ioexception.getmessage());
}
return list;
}
}4.定义自定义替换词类
package com.heyin.sass.portal.util.sensitive;
import lombok.extern.slf4j.slf4j;
import org.springframework.core.io.classpathresource;
import org.springframework.core.io.resource;
import com.github.houbb.sensitive.word.api.iwordcontext;
import com.github.houbb.sensitive.word.api.iwordreplace;
import com.github.houbb.sensitive.word.api.iwordresult;
import com.github.houbb.sensitive.word.utils.innerwordcharutils;
import java.io.bufferedreader;
import java.io.inputstreamreader;
import java.nio.charset.standardcharsets;
import java.util.hashmap;
import java.util.map;
/**
* @author zenghuilin
*/
@slf4j
public class mywordreplace implements iwordreplace {
private static final map<string, string> sensitive_word_map = new hashmap<>();
static {
try {
// 使用 classpathresource 加载文件
resource resource = new classpathresource("sensitive/sensitive_word_replace.txt");
bufferedreader reader = new bufferedreader(new inputstreamreader(resource.getinputstream(), standardcharsets.utf_8));
// 逐行读取文件
string line;
while ((line = reader.readline()) != null) {
// 将每行按逗号分割成 key 和 value
string[] parts = line.split(",");
if (parts.length == 2) {
sensitive_word_map.put(parts[0], parts[1]); // 将 a,b 形式加入到map中
}
}
reader.close();
} catch (exception e) {
log.info("初始化sensitive_word_map失败:{}", e.getmessage());
}
}
@override
public void replace(stringbuilder stringbuilder, final char[] rawchars, iwordresult wordresult, iwordcontext wordcontext) {
string sensitiveword = innerwordcharutils.getstring(rawchars, wordresult);
// 自定义不同的敏感词替换策略,可以从数据库等地方读取
if (sensitive_word_map.containskey(sensitiveword)) {
stringbuilder.append(sensitive_word_map.get(sensitiveword));
} else {
// 其他默认使用 * 代替
int wordlength = wordresult.endindex() - wordresult.startindex();
for (int i = 0; i < wordlength; i++) {
stringbuilder.append('*');
}
}
}
}5.最后定义工具类
package com.heyin.sass.portal.util.sensitive;
import com.github.houbb.sensitive.word.api.iwordallow;
import com.github.houbb.sensitive.word.api.iworddeny;
import com.github.houbb.sensitive.word.api.iwordreplace;
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 java.util.list;
/**
* @author zenghuilin
*/
public class sensitivewordutil {
private static final sensitivewordbs sensitive_word_bs;
static {
// 配置默认敏感词 + 自定义敏感词
iworddeny worddeny = worddenys.chains(worddenys.defaults(), new myworddeny());
// 配置默认非敏感词 + 自定义非敏感词
iwordallow wordallow = wordallows.chains(wordallows.defaults(), new mywordallow());
// 配置自定义替换词
iwordreplace wordreplace = new mywordreplace();
sensitive_word_bs = sensitivewordbs.newinstance()
// 忽略大小写
.ignorecase(true)
// 忽略半角圆角
.ignorewidth(true)
// 忽略数字的写法
.ignorenumstyle(true)
// 忽略中文的书写格式:简繁体
.ignorechinesestyle(true)
// 忽略英文的书写格式
.ignoreenglishstyle(true)
// 忽略重复词
.ignorerepeat(false)
// 是否启用数字检测
.enablenumcheck(false)
// 是否启用邮箱检测
.enableemailcheck(false)
// 是否启用链接检测
.enableurlcheck(false)
// 数字检测,自定义指定长度
// .numchecklen(8)
// 配置自定义敏感词
.worddeny(worddeny)
// 配置非自定义敏感词
.wordallow(wordallow)
// 配置自定义替换词
.wordreplace(wordreplace)
.init();
}
/**
* 刷新敏感词库与非敏感词库缓存
*/
public static void refresh() {
sensitive_word_bs.init();
}
/**
* 判断是否含有敏感词
*
* @param text
* @return
*/
public static boolean contains(string text) {
return sensitive_word_bs.contains(text);
}
/**
* 替换敏感词
*
* @param text
* @return
*/
public static string replace(string text) {
return sensitive_word_bs.replace(text);
}
/**
* 返回所有敏感词
*
* @param text
* @return
*/
public static list<string> findall(string text) {
return sensitive_word_bs.findall(text);
}
public static void main(string[] args) {
string text = "五星红旗迎风飘扬";
system.out.println(findall(text));
string replace = replace(text);
system.out.println(replace);
}
}6.资源文件放在src/main/resouces目录下

敏感词文件和非敏感词文件,一个词一行

替换词文件,前面是敏感词,后面是想要替换的词

总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论