SensitiveWordUtil.java 1.1 KB
Newer Older
1 2 3 4 5 6 7 8
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;

9 10 11 12 13 14 15 16

/**
 * 敏感词过滤工具类
 *
 * @author : qinyingjie
 * @version : 2.2.0
 * @date : 2023/8/17 09:41
 */
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
@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);
    }
}