fix(comment): align houbb sensitive-word integration with official usage
This commit is contained in:
@@ -107,8 +107,8 @@
|
|||||||
<!-- houbb 敏感词过滤 -->
|
<!-- houbb 敏感词过滤 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.houbb</groupId>
|
<groupId>com.github.houbb</groupId>
|
||||||
<artifactId>sensitive-word-core</artifactId>
|
<artifactId>sensitive-word</artifactId>
|
||||||
<version>0.34.0</version>
|
<version>0.29.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- 工具类 -->
|
<!-- 工具类 -->
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package com.test.musichouduan.service.impl;
|
package com.test.musichouduan.service.impl;
|
||||||
|
|
||||||
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
|
import com.github.houbb.sensitive.word.core.SensitiveWordHelper;
|
||||||
import com.test.musichouduan.exception.BusinessException;
|
import com.test.musichouduan.exception.BusinessException;
|
||||||
import com.test.musichouduan.service.SensitiveWordService;
|
import com.test.musichouduan.service.SensitiveWordService;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -13,23 +13,15 @@ import java.util.List;
|
|||||||
@Service
|
@Service
|
||||||
public class SensitiveWordServiceImpl implements SensitiveWordService {
|
public class SensitiveWordServiceImpl implements SensitiveWordService {
|
||||||
|
|
||||||
/**
|
|
||||||
* 使用 SensitiveWordBs 初始化敏感词引擎。
|
|
||||||
* 说明:当前依赖版本下推荐使用该入口,避免旧版 Helper 类路径变更导致编译失败。
|
|
||||||
*/
|
|
||||||
private static final SensitiveWordBs SENSITIVE_WORD_BS = SensitiveWordBs.newInstance().init();
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void validateCommentContent(String content) {
|
public void validateCommentContent(String content) {
|
||||||
if (content == null || content.trim().isEmpty()) {
|
if (content == null || content.trim().isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用 houbb/sensitive-word 默认内置词库进行检测。
|
// 按官方 README 常见用法,直接使用 SensitiveWordHelper 进行检测。
|
||||||
// 当前项目保留了 sensitive-words.txt 作为后续扩展词库参考文件,
|
if (SensitiveWordHelper.contains(content)) {
|
||||||
// 现阶段未主动加载,避免与默认词库机制产生混淆。
|
List<String> hitWords = SensitiveWordHelper.findAll(content);
|
||||||
if (SENSITIVE_WORD_BS.contains(content)) {
|
|
||||||
List<String> hitWords = SENSITIVE_WORD_BS.findAll(content);
|
|
||||||
if (!hitWords.isEmpty()) {
|
if (!hitWords.isEmpty()) {
|
||||||
throw new BusinessException("评论包含敏感词,请修改后再发布");
|
throw new BusinessException("评论包含敏感词,请修改后再发布");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user