java敏感词过滤的实现方式

 更新时间:2025年12月01日 14:35:29   作者:一点晖光  
文章描述了如何搭建敏感词过滤系统来防御用户生成内容中的违规、广告或恶意言论,包括引入依赖、定义敏感词类、非敏感词类、替换词类和工具类等步骤,并指出资源文件应放在src/main/resources目录下

在论坛、聊天、评论等用户生成内容(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目录下

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

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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java 轻松掌握字符缓冲流的使用

    Java 轻松掌握字符缓冲流的使用

    这篇文章主要介绍了Java的字符缓冲流用法,字符缓冲流的用途很多,主要是几个构造方法的使用,在项目开发中经常会用到,需要的朋友参考下吧
    2022-04-04
  • SpringBoot读取自定义yml/yaml文件键值对的代码实现方式

    SpringBoot读取自定义yml/yaml文件键值对的代码实现方式

    YAML键值对常见于业务处理,读取方式主要有两种——通过配置类直接解析,或使用键值对类读取后存入List,选择合适方法取决于具体业务需求
    2025-08-08
  • Java NoClassDefFoundError运行时错误分析解决

    Java NoClassDefFoundError运行时错误分析解决

    在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能找到该类的定义,这个问题经常与类路径配置不正确或者缺失的库文件有关,需要的朋友可以参考下
    2025-05-05
  • Java中由substring方法引发的内存泄漏详解

    Java中由substring方法引发的内存泄漏详解

    这篇文章主要介绍了Java中由substring方法引发的内存泄漏详解,涉及substring方法引发的内存泄漏简介,substring的作用和实现原理等相关内容,具有一定借鉴价值,需要的朋友可以参考下
    2017-12-12
  • java字符串反转示例分享

    java字符串反转示例分享

    这篇文章主要介绍了将一个字符串进行反转或者字符串中指定部分进行反转的方法,大家参考使用吧
    2014-01-01
  • 利用Java8 Optional类优雅如何地解决空指针问题

    利用Java8 Optional类优雅如何地解决空指针问题

    这篇文章主要给大家介绍了关于如何利用Java8 Optional类优雅解决空指针问题的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • Hadoop2.8.1完全分布式环境搭建过程

    Hadoop2.8.1完全分布式环境搭建过程

    本文搭建了一个由三节点(master、slave1、slave2)构成的Hadoop完全分布式集群(区别单节点伪分布式集群),并通过Hadoop分布式计算的一个示例测试集群的正确性。对hadoop分布式环境搭建过程感兴趣的朋友跟随小编一起看看吧
    2019-06-06
  • Maven+oracle+SSM搭建简单项目的方法

    Maven+oracle+SSM搭建简单项目的方法

    本篇文章主要介绍了Maven+oracle+SSM搭建简单项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • 如何使用java给局域网的电脑发送开机数据包

    如何使用java给局域网的电脑发送开机数据包

    这篇文章主要为大家详细介绍了如何使用java给局域网的电脑发送开机数据包,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2025-09-09
  • 基于SpringBoot实现定时发送邮件过程解析

    基于SpringBoot实现定时发送邮件过程解析

    这篇文章主要介绍了基于SpringBoot实现定时发送邮件过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06

最新评论