springboot整合sensitive

前言

本人采用sensitive-word实现敏感词的过滤。

步骤

  1. 引入pom
<dependency>
	<groupId>com.github.houbb</groupId>
	<artifactId>sensitive-word</artifactId>
	<version>0.2.0</version>
</dependency>
  1. 配置
/**
 * 敏感词配置
 *
 * @author : cnl
 */
@RequiredArgsConstructor
@Configuration
public class SensitiveWordConfig {

    private final DdWordAllowConfig DdWordAllowConfig;

    private final MyDdWordDenyConfig DdWordDenyConfig;

    @Bean
    public SensitiveWordBs sensitiveWordBs() {
        return SensitiveWordBs.newInstance()
                //可以指定多个过滤WordDenys.chains(WordDeny... wordDeny)
                .wordDeny(WordDenys.chains(WordDenys.system(), DdWordAllowConfig))
                //可以指定多个放行WordAllows.chains(WordAllow... wordAllow)
                .wordAllow(WordAllows.chains(WordAllows.system(), DdWordDenyConfig))
                .init();
    }
}
/**
 * 黑名单
 * @author cnl
 */
@RequiredArgsConstructor
@Component
public class DdWordDenyConfig implements IWordDeny {

    private final SensitiveProperties sensitiveProperties;

    /*你如果想使用查询sql的话,可以另外起一个MyDdWordAllow或者直接在该方法中加入*/
    @Override
    public List<String> deny() {
        List<String> list = new ArrayList<>();
        // 黑名单的敏感词文件
        String denyPath = sensitiveProperties.getDenyPath();
        if (StrUtil.isEmpty(denyPath) || !FileUtil.exist(denyPath)) {
            return list;
        }

        List<String> denys = FileUtil.readUtf8Lines(denyPath);
        if (CollectionUtil.isEmpty(denys)) {
            return list;
        }

        list.addAll(denys);

        return list;
    }

}
/**
 * 白名单
 * @author cnl
 */
@RequiredArgsConstructor
@Component
public class DdWordAllowConfig implements IWordAllow {

    private final SensitiveProperties sensitiveProperties;

    /*你如果想使用查询sql的话,可以另外起一个MyDdWordAllow或者直接在该方法中加入*/
    @Override
    public List<String> allow() {
        List<String> list = new ArrayList<>();
        // 白名单的敏感词文件
        String allowPath = sensitiveProperties.getAllowPath();
        if (StrUtil.isEmpty(allowPath) || !FileUtil.exist(allowPath)) {
            return list;
        }

        List<String> allows = FileUtil.readUtf8Lines(allowPath);
        if (CollectionUtil.isEmpty(allows)) {
            return list;
        }

        list.addAll(allows);

        return list;
    }

}
/**
 * 敏感词配置
 *
 * @Author : cnl
 */
@Component
@Data
@ConfigurationProperties(prefix = "drm.cnl.sensitive")
public class SensitiveProperties {

    /**
     * 白名单文件
     */
    private String allowPath;

    /**
     * 黑名单的文件
     */
    private String denyPath;
}
@Service
@RequiredArgsConstructor
public class Test {

    private final SensitiveWordBs sensitiveWordBs;
    
    @Override
    public void testSensitive(String content) {
        //获取content中所有的敏感词
        List<String> sensitiveWords = sensitiveWordBs.findAll(content));
        //默认会把敏感词转换成 *
        String aa = sensitiveWordBs.replace(content);
        //也可以自定义替换字符
        String aa = sensitiveWordBs.replace(content, "#");
        //.... 还有一些其他的不做阐述了
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
springboot整合敏感词可以通过配置和自定义过滤器来实现。首先,需要在配置文件中设置敏感词处理方式。例如,在`application.properties`文件中,可以使用`sensitiveWord.dealMethod`配置项来指定处理方式,默认不处理敏感词可以设置为null。 接下来,在配置类中使用`@Bean`注解来创建一个过滤器的实例。可以使用`FilterRegistrationBean`来注册过滤器,并在构造函数中传入敏感词处理方式。例如,可以创建一个`SensitiveWordFilter`过滤器实例,并将敏感词处理方式作为参数传入。然后,将过滤器实例添加到`FilterRegistrationBean`中,并返回该实例。 这样,当应用启动时,过滤器就会自动注册,并根据配置的敏感词处理方式进行相应的处理。例如,如果配置为替换敏感字符的方式(1),则过滤器会将敏感词替换为指定的字符;如果配置为包含敏感字符禁止提交的方式(2),则过滤器会禁止提交包含敏感词的内容。如果未配置处理方式,则过滤器不会对敏感词进行处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot2.2.6 整合Jwt实现前后端分离](https://download.csdn.net/download/lianghecai52171314/12352790)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [spring boot 统一处理敏感词](https://blog.csdn.net/fengxing_2/article/details/109182394)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值