diff --git a/luoo_comment/pom.xml b/luoo_comment/pom.xml index 6561788..7fdf314 100644 --- a/luoo_comment/pom.xml +++ b/luoo_comment/pom.xml @@ -95,6 +95,12 @@ + + com.github.houbb + sensitive-word + 0.2.0 + + app diff --git a/luoo_comment/src/main/java/com/luoo/comment/config/SpringSensitiveWordConfig.java b/luoo_comment/src/main/java/com/luoo/comment/config/SpringSensitiveWordConfig.java new file mode 100644 index 0000000..ef7cd29 --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/config/SpringSensitiveWordConfig.java @@ -0,0 +1,39 @@ +package com.luoo.comment.config; +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.luoo.comment.sensitiveWord.MyDdWordAllow; +import com.luoo.comment.sensitiveWord.MyDdWordDeny; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + + + + +@Configuration +public class SpringSensitiveWordConfig { + + + @Autowired + private MyDdWordAllow myDdWordAllow; + + @Autowired + private MyDdWordDeny myDdWordDeny; + /** + * 初始化引导类 + * @return 初始化引导类 + * @since 1.0.0 + */ + @Bean + public SensitiveWordBs sensitiveWordBs() { + SensitiveWordBs init = SensitiveWordBs.newInstance() + .wordAllow(WordAllows.chains(WordAllows.system(), myDdWordAllow)) + .wordDeny(WordDenys.chains( myDdWordDeny)) + .ignoreRepeat(false) + // 各种其他配置 + .init(); + + return init; + } +} \ No newline at end of file diff --git a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java index 3e37b88..a2aaba6 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java +++ b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java @@ -4,6 +4,8 @@ package com.luoo.comment.controller; import api.PageResult; import api.Result; import api.StatusCode; +import com.github.houbb.sensitive.word.bs.SensitiveWordBs; +import com.github.houbb.sensitive.word.core.SensitiveWordHelper; import com.luoo.comment.dao.PublicationLikeDao; import com.luoo.comment.pojo.*; import com.luoo.comment.service.CommentService; @@ -11,6 +13,7 @@ import com.luoo.comment.service.ComplaintService; import com.luoo.comment.service.LikeService; import com.luoo.comment.service.UserInfoService; import com.luoo.comment.util.IpUtil; +import com.luoo.comment.util.MySensitiveWordReplaceUtils; import constants.Constants; import controller.BaseController; import dto.UserLoginDto; @@ -79,6 +82,11 @@ public class CommentController extends BaseController { @Autowired private PublicationLikeDao publicationLikeDao; + @Autowired + private SensitiveWordBs sensitiveWordBs; + + @Autowired + private MySensitiveWordReplaceUtils mySensitiveWordReplaceUtils; @GetMapping public Result findAll(){ return Result.success(commentService.findAll()); @@ -419,5 +427,15 @@ public class CommentController extends BaseController { + @PostMapping("/testSensitiveMosaic/{note}") + public Result testSensitiveMosaic(@PathVariable String note) { + +// List all = sensitiveWordBs.findAll(note); +// String all = SensitiveWordHelper.replace(note,mySensitiveWordReplaceUtils); + + String all = sensitiveWordBs.replace(note,mySensitiveWordReplaceUtils); + return Result.success(all); + } + } 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 new file mode 100644 index 0000000..c690ef0 --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/sensitiveWord/MyDdWordAllow.java @@ -0,0 +1,21 @@ +package com.luoo.comment.sensitiveWord; + +import com.github.houbb.sensitive.word.api.IWordAllow; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; + +@Component +public class MyDdWordAllow implements IWordAllow { + + @Override + public List allow() { + List list = new ArrayList<>(); + 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 new file mode 100644 index 0000000..c470896 --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/sensitiveWord/MyDdWordDeny.java @@ -0,0 +1,34 @@ +package com.luoo.comment.sensitiveWord; + +import com.github.houbb.sensitive.word.api.IWordDeny; +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 { + + @Override + public List deny() { + List list = new ArrayList<>(); + list.add("落网"); + Resource mySensitiveWords = new ClassPathResource("denyWords.txt"); + try { + Path mySensitiveWordsPath = Paths.get(mySensitiveWords.getFile().getPath()); + list = Files.readAllLines(mySensitiveWordsPath, StandardCharsets.UTF_8); + + } 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 new file mode 100644 index 0000000..40bf027 --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/sensitiveWord/SensitiveWordService.java @@ -0,0 +1,23 @@ +package com.luoo.comment.sensitiveWord; + +import com.github.houbb.sensitive.word.bs.SensitiveWordBs; +import org.springframework.beans.factory.annotation.Autowired; + +public class SensitiveWordService { + + @Autowired + private SensitiveWordBs sensitiveWordBs; + + /** + * 更新词库 + *

+ * 每次数据库的信息发生变化之后,首先调用更新数据库敏感词库的方法。 + * 如果需要生效,则调用这个方法。 + *

+ * 说明:重新初始化不影响旧的方法使用。初始化完成后,会以新的为准。 + */ + 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