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敏感词转换成符号内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论