From 980a52a062be380dd178a22dc961bdfafc0bc780 Mon Sep 17 00:00:00 2001 From: wangqing <408564126@qq.com> Date: Fri, 8 Mar 2024 20:46:25 +0800 Subject: [PATCH] feat: SensitiveWords --- .../comment/sensitiveword/MyDdWordAllow.java | 4 +-- .../comment/sensitiveword/MyDdWordDeny.java | 25 ++++++++++++------- .../sensitiveword/SensitiveWordService.java | 2 -- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/luoo_comment/src/main/java/com/luoo/comment/sensitiveword/MyDdWordAllow.java b/luoo_comment/src/main/java/com/luoo/comment/sensitiveword/MyDdWordAllow.java index 1d61d61..c690ef0 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/sensitiveword/MyDdWordAllow.java +++ b/luoo_comment/src/main/java/com/luoo/comment/sensitiveword/MyDdWordAllow.java @@ -1,4 +1,4 @@ -package com.luoo.comment.sensitiveword; +package com.luoo.comment.sensitiveWord; import com.github.houbb.sensitive.word.api.IWordAllow; import org.springframework.stereotype.Component; @@ -15,8 +15,6 @@ public class MyDdWordAllow implements IWordAllow { list.add("五星红旗"); list.add("天安门"); - - return list; } diff --git a/luoo_comment/src/main/java/com/luoo/comment/sensitiveword/MyDdWordDeny.java b/luoo_comment/src/main/java/com/luoo/comment/sensitiveword/MyDdWordDeny.java index 1285f96..c470896 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/sensitiveword/MyDdWordDeny.java +++ b/luoo_comment/src/main/java/com/luoo/comment/sensitiveword/MyDdWordDeny.java @@ -1,27 +1,34 @@ -package com.luoo.comment.sensitiveword; +package com.luoo.comment.sensitiveWord; import com.github.houbb.sensitive.word.api.IWordDeny; -import constants.SensitiveWords; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; import org.springframework.stereotype.Component; +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; @Component public class MyDdWordDeny implements IWordDeny { - String DENY_WORDS= SensitiveWords.DENY_WORDS; + @Override public List deny() { List list = new ArrayList<>(); -// list.add("落网"); + list.add("落网"); + Resource mySensitiveWords = new ClassPathResource("denyWords.txt"); + try { + Path mySensitiveWordsPath = Paths.get(mySensitiveWords.getFile().getPath()); + list = Files.readAllLines(mySensitiveWordsPath, StandardCharsets.UTF_8); - String[] s = DENY_WORDS.split("\n"); - for(String denyWord:s) { - list.add(denyWord); + } catch (IOException e) { + throw new RuntimeException(e); } - - return list; } } \ No newline at end of file diff --git a/luoo_comment/src/main/java/com/luoo/comment/sensitiveword/SensitiveWordService.java b/luoo_comment/src/main/java/com/luoo/comment/sensitiveword/SensitiveWordService.java index e9f52d0..c75aca4 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/sensitiveword/SensitiveWordService.java +++ b/luoo_comment/src/main/java/com/luoo/comment/sensitiveword/SensitiveWordService.java @@ -16,8 +16,6 @@ public class SensitiveWordService { *

* 说明:重新初始化不影响旧的方法使用。初始化完成后,会以新的为准。 */ - - public void refresh() { // 每次数据库的信息发生变化之后,首先调用更新数据库敏感词库的方法,然后调用这个方法。 sensitiveWordBs.init();