当前位置: 代码网 > it编程>编程语言>Java > Java数据敏感词转换成符号的方法详解

Java数据敏感词转换成符号的方法详解

2024年05月19日 Java 我要评论
java数据敏感词转换成符号在某个论坛下用户可以随意留言,为了防止不法分子在网上任意冲浪,需要对一些敏感词汇进行一些校验。在这里使用一个高性能敏感词工具sensitive-word。maven<

java数据敏感词转换成符号

在某个论坛下用户可以随意留言,为了防止不法分子在网上任意冲浪,需要对一些敏感词汇进行一些校验。在这里使用一个高性能敏感词工具sensitive-word。

maven

<!--     数据脱敏   -->
        <dependency>
            <groupid>com.github.houbb</groupid>
            <artifactid>sensitive-word</artifactid>
            <version>0.13.1</version>
        </dependency>

直接使用

	    string str1 = "我他妈的没有说过脏话";
        string replace1 = sensitivewordhelper.replace(str1);
        system.out.println("原始数据:" + str1);
        system.out.println("脱敏数据:" + replace1);
// 原始数据:我他妈的没有说过脏话
// 脱敏数据:我***没有说过脏话

结合spring使用

拓展已脱敏词汇

@component
public class myworddeny implements iworddeny {

    @override
    public list<string> deny() {
        return arrays.aslist("卢本伟没有开挂","我的发");
    }
}

拓展排除脱敏词汇

@component
public class mywordallow implements iwordallow {
    @override
    public list<string> allow() {
        return arrays.aslist("废物","辣鸡");
    }
}

配置类

@configuration
public class springsensitivewordconfig {

    /**
     * 排除敏感词
     */
    @resource
    private mywordallow mywordallow;

    /**
     * 拓展敏感词
     */
    @resource
    private myworddeny myworddeny;


    /**
     * 初始化引导类
     * wordallows.chains 在原作者词汇数据库进行拓展
     * worddenys.chains 同上
     */
    @bean
    public sensitivewordbs sensitivewordbs() {
        return sensitivewordbs.newinstance()
                .wordallow(wordallows.chains(wordallows.defaults(), mywordallow))
                .worddeny(worddenys.chains(worddenys.defaults(), myworddeny))
                .init();
    }
}

测试调用

@springboottest(classes = qpikeradminapplication.class)
@runwith(springrunner.class)
public class sensitivetest {

    /**
     * 数据脱敏
     */
    @resource
    private springsensitivewordconfig springsensitivewordconfig;

    @test
    public void wordsensitive(){
        string str = "谁他妈的是废物?,我卢本伟没有开挂真的,1824176666,我的发你是小辣鸡";
        system.out.println("原始数据:" + str);
        string replace = springsensitivewordconfig.sensitivewordbs().replace(str);
        system.out.println("脱敏数据:" + replace);
    }
}

输出结果

原始数据:谁他妈的是废物?,我卢本伟没有开挂真的,18241766666,他妈的,我的发你是小辣鸡
脱敏数据:谁***是废物?,我*******真的,***********,***,***你是小辣鸡

'废物' 被加到排除脱敏数据中,结果没被替换
'卢本伟没有开挂' 被加到已脱敏数据中,结果被替换

真没有意说脏话,希望各大网友们构建一个文明网络世界。

到此这篇关于java数据敏感词转换成符号的示例代码的文章就介绍到这了,更多相关java敏感词转换成符号内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com