+ * 每次数据库的信息发生变化之后,首先调用更新数据库敏感词库的方法。 + * 如果需要生效,则调用这个方法。 + *
+ * 说明:重新初始化不影响旧的方法使用。初始化完成后,会以新的为准。 + */ + public void refresh() { + // 每次数据库的信息发生变化之后,首先调用更新数据库敏感词库的方法,然后调用这个方法。 + sensitiveWordBs.init(); + } +} \ No newline at end of file diff --git a/luoo_comment/src/main/java/com/luoo/comment/util/MySensitiveWordReplaceUtils.java b/luoo_comment/src/main/java/com/luoo/comment/util/MySensitiveWordReplaceUtils.java new file mode 100644 index 0000000..efc0719 --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/util/MySensitiveWordReplaceUtils.java @@ -0,0 +1,29 @@ +package com.luoo.comment.util; + +import com.github.houbb.heaven.util.lang.CharUtil; +import com.github.houbb.sensitive.word.api.ISensitiveWordReplace; +import com.github.houbb.sensitive.word.api.ISensitiveWordReplaceContext; +import org.springframework.stereotype.Component; + +@Component +public class MySensitiveWordReplaceUtils 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); + } + +} + diff --git a/luoo_comment/src/main/resources/denyWords.txt b/luoo_comment/src/main/resources/denyWords.txt new file mode 100644 index 0000000..7479336 --- /dev/null +++ b/luoo_comment/src/main/resources/denyWords.txt @@ -0,0 +1,2 @@ +雀乐 +落网 \ No newline at end of file