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