Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
SpringBoot-kwan
提交
f76b2293
S
SpringBoot-kwan
项目概览
Kwan的解忧杂货铺@新空间代码工作室
/
SpringBoot-kwan
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SpringBoot-kwan
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
f76b2293
编写于
3月 24, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:敏感词过滤
上级
3fb1e69f
变更
9
显示空白变更内容
内联
并排
Showing
9 changed file
with
252 addition
and
0 deletion
+252
-0
pom.xml
pom.xml
+15
-0
src/main/java/com/kwan/springbootkwan/config/SensitiveConfig.java
.../java/com/kwan/springbootkwan/config/SensitiveConfig.java
+52
-0
src/main/java/com/kwan/springbootkwan/service/sensitive/MySensitiveWordReplace.java
...ingbootkwan/service/sensitive/MySensitiveWordReplace.java
+31
-0
src/main/java/com/kwan/springbootkwan/service/sensitive/MyWordAllow.java
...om/kwan/springbootkwan/service/sensitive/MyWordAllow.java
+43
-0
src/main/java/com/kwan/springbootkwan/service/sensitive/MyWordDeny.java
...com/kwan/springbootkwan/service/sensitive/MyWordDeny.java
+43
-0
src/main/java/com/kwan/springbootkwan/utils/SensitiveWordUtil.java
...java/com/kwan/springbootkwan/utils/SensitiveWordUtil.java
+40
-0
src/main/resources/myNotSensitiveWords.txt
src/main/resources/myNotSensitiveWords.txt
+1
-0
src/main/resources/mySensitiveWords.txt
src/main/resources/mySensitiveWords.txt
+2
-0
src/test/java/com/kwan/springbootkwan/SensitiveTest.java
src/test/java/com/kwan/springbootkwan/SensitiveTest.java
+25
-0
未找到文件。
pom.xml
浏览文件 @
f76b2293
...
@@ -161,6 +161,21 @@
...
@@ -161,6 +161,21 @@
<artifactId>
fastjson2
</artifactId>
<artifactId>
fastjson2
</artifactId>
<version>
2.0.23
</version>
<version>
2.0.23
</version>
</dependency>
</dependency>
<!-- 敏感词工具包 -->
<dependency>
<groupId>
com.github.houbb
</groupId>
<artifactId>
sensitive-word
</artifactId>
<version>
0.2.1
</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
4.12
</version>
<scope>
test
</scope>
</dependency>
</dependencies>
</dependencies>
<build>
<build>
<plugins>
<plugins>
...
...
src/main/java/com/kwan/springbootkwan/config/SensitiveConfig.java
0 → 100644
浏览文件 @
f76b2293
package
com.kwan.springbootkwan.config
;
import
com.github.houbb.sensitive.word.api.IWordAllow
;
import
com.github.houbb.sensitive.word.api.IWordDeny
;
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
com.kwan.springbootkwan.service.sensitive.MyWordAllow
;
import
com.kwan.springbootkwan.service.sensitive.MyWordDeny
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
@Configuration
public
class
SensitiveConfig
{
// 配置默认敏感词 + 自定义敏感词
IWordDeny
wordDeny
=
WordDenys
.
chains
(
WordDenys
.
system
(),
new
MyWordDeny
());
// 配置默认非敏感词 + 自定义非敏感词
IWordAllow
wordAllow
=
WordAllows
.
chains
(
WordAllows
.
system
(),
new
MyWordAllow
());
@Bean
public
SensitiveWordBs
sensitiveWordBs
()
{
return
SensitiveWordBs
.
newInstance
()
// 忽略大小写
.
ignoreCase
(
true
)
// 忽略半角圆角
.
ignoreWidth
(
true
)
// 忽略数字的写法
.
ignoreNumStyle
(
true
)
// 忽略中文的书写格式:简繁体
.
ignoreChineseStyle
(
true
)
// 忽略英文的书写格式
.
ignoreEnglishStyle
(
true
)
// 忽略重复词
.
ignoreRepeat
(
false
)
// 是否启用数字检测
.
enableNumCheck
(
true
)
// 是否启用邮箱检测
.
enableEmailCheck
(
true
)
// 是否启用链接检测
.
enableUrlCheck
(
true
)
// 数字检测,自定义指定长度
.
numCheckLen
(
8
)
// 配置自定义敏感词
.
wordDeny
(
wordDeny
)
// 配置非自定义敏感词
.
wordAllow
(
wordAllow
)
.
init
();
}
}
src/main/java/com/kwan/springbootkwan/service/sensitive/MySensitiveWordReplace.java
0 → 100644
浏览文件 @
f76b2293
package
com.kwan.springbootkwan.service.sensitive
;
import
com.github.houbb.heaven.util.lang.CharUtil
;
import
com.github.houbb.sensitive.word.api.ISensitiveWordReplace
;
import
com.github.houbb.sensitive.word.api.ISensitiveWordReplaceContext
;
/**
* 自定义敏感词
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/3/24 15:09
*/
public
class
MySensitiveWordReplace
implements
ISensitiveWordReplace
{
@Override
public
String
replace
(
ISensitiveWordReplaceContext
context
)
{
String
sensitiveWord
=
context
.
sensitiveWord
();
// 自定义不同的敏感词替换策略,可以从数据库等地方读取
if
(
"五星红旗"
.
equals
(
sensitiveWord
))
{
return
"国家旗帜"
;
}
if
(
"毛主席"
.
equals
(
sensitiveWord
))
{
return
"教员"
;
}
// 其他默认使用 * 代替
int
wordLength
=
context
.
wordLength
();
return
CharUtil
.
repeat
(
'*'
,
wordLength
);
}
}
src/main/java/com/kwan/springbootkwan/service/sensitive/MyWordAllow.java
0 → 100644
浏览文件 @
f76b2293
package
com.kwan.springbootkwan.service.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 : qinyingjie
* @version : 2.2.0
* @date : 2023/3/24 15:06
*/
@Slf4j
public
class
MyWordAllow
implements
IWordAllow
{
@Override
public
List
<
String
>
allow
()
{
List
<
String
>
list
=
new
ArrayList
<>();
;
try
{
Resource
myAllowWords
=
new
ClassPathResource
(
"myNotSensitiveWords.txt"
);
Path
myAllowWordsPath
=
Paths
.
get
(
myAllowWords
.
getFile
().
getPath
());
list
=
Files
.
readAllLines
(
myAllowWordsPath
,
StandardCharsets
.
UTF_8
);
}
catch
(
IOException
ioException
)
{
log
.
error
(
"读取非敏感词文件错误!"
+
ioException
.
getMessage
());
}
return
list
;
}
}
\ No newline at end of file
src/main/java/com/kwan/springbootkwan/service/sensitive/MyWordDeny.java
0 → 100644
浏览文件 @
f76b2293
package
com.kwan.springbootkwan.service.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 : qinyingjie
* @version : 2.2.0
* @date : 2023/3/24 15:07
*/
@Slf4j
public
class
MyWordDeny
implements
IWordDeny
{
@Override
public
List
<
String
>
deny
()
{
List
<
String
>
list
=
new
ArrayList
<>();
try
{
Resource
mySensitiveWords
=
new
ClassPathResource
(
"mySensitiveWords.txt"
);
Path
mySensitiveWordsPath
=
Paths
.
get
(
mySensitiveWords
.
getFile
().
getPath
());
list
=
Files
.
readAllLines
(
mySensitiveWordsPath
,
StandardCharsets
.
UTF_8
);
}
catch
(
IOException
ioException
)
{
log
.
error
(
"读取敏感词文件错误!"
+
ioException
.
getMessage
());
}
return
list
;
}
}
src/main/java/com/kwan/springbootkwan/utils/SensitiveWordUtil.java
0 → 100644
浏览文件 @
f76b2293
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
;
@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
);
}
}
src/main/resources/myNotSensitiveWords.txt
0 → 100644
浏览文件 @
f76b2293
小码哥
\ No newline at end of file
src/main/resources/mySensitiveWords.txt
0 → 100644
浏览文件 @
f76b2293
国家
马化腾
\ No newline at end of file
src/test/java/com/kwan/springbootkwan/SensitiveTest.java
0 → 100644
浏览文件 @
f76b2293
package
com.kwan.springbootkwan
;
import
com.kwan.springbootkwan.utils.SensitiveWordUtil
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.junit4.SpringRunner
;
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
@ContextConfiguration
(
classes
=
SpringBootKwanApplication
.
class
)
public
class
SensitiveTest
{
@Autowired
private
SensitiveWordUtil
sensitiveWordUtil
;
@Test
public
void
test
()
{
String
result
=
sensitiveWordUtil
.
replace
(
"国家 哇 nnd 复活 马化腾"
);
System
.
out
.
println
(
result
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录