fix:敏感词过滤

上级 3fb1e69f
...@@ -161,6 +161,21 @@ ...@@ -161,6 +161,21 @@
<artifactId>fastjson2</artifactId> <artifactId>fastjson2</artifactId>
<version>2.0.23</version> <version>2.0.23</version>
</dependency> </dependency>
<!-- 敏感词工具包 -->
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>sensitive-word</artifactId>
<version>0.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
......
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();
}
}
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);
}
}
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<String> allow() {
List<String> 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
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<String> deny() {
List<String> 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;
}
}
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<String> findAll(String text) {
return sensitiveWordBs.findAll(text);
}
}
小码哥
\ No newline at end of file
国家
马化腾
\ No newline at end of file
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);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册