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 {

    @Autowired
    private MyWordAllow myWordAllow;

    @Autowired
    private MyWordDeny myWordDeny;

    /**
     * 初始化引导类
     * @return 初始化引导类
     * @since 1.0.0
     */
    @Bean
    public 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 {

    @Override
    public 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 {

    @Override
    public 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 {

    @Autowired
    private 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 {

    @Autowired
    private SensitiveWordService sensitiveWordService;

    @Test
    public 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、文本目录

相关推荐
熊大如如8 分钟前
Java 反射
java·开发语言
猿来入此小猿26 分钟前
基于SSM实现的健身房系统功能实现十六
java·毕业设计·ssm·毕业源码·免费学习·猿来入此·健身平台
goTsHgo1 小时前
Spring Boot 自动装配原理详解
java·spring boot
卑微的Coder1 小时前
JMeter同步定时器 模拟多用户并发访问场景
java·jmeter·压力测试
pjx9871 小时前
微服务的“导航系统”:使用Spring Cloud Eureka实现服务注册与发现
java·spring cloud·微服务·eureka
炒空心菜菜2 小时前
SparkSQL 连接 MySQL 并添加新数据:实战指南
大数据·开发语言·数据库·后端·mysql·spark
多多*2 小时前
算法竞赛相关 Java 二分模版
java·开发语言·数据结构·数据库·sql·算法·oracle
爱喝酸奶的桃酥2 小时前
MYSQL数据库集群高可用和数据监控平台
java·数据库·mysql
唐僧洗头爱飘柔95273 小时前
【SSM-SSM整合】将Spring、SpringMVC、Mybatis三者进行整合;本文阐述了几个核心原理知识点,附带对应的源码以及描述解析
java·spring·mybatis·springmvc·动态代理·ioc容器·视图控制器
骑牛小道士3 小时前
Java基础 集合框架 Collection接口和抽象类AbstractCollection
java