feat: sensitiveword demo

main
wangqing 9 months ago
parent ac1a0cd3de
commit ac8da9fae3

@ -95,6 +95,12 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>sensitive-word</artifactId>
<version>0.2.0</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<finalName>app</finalName> <finalName>app</finalName>

@ -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;
}
}

@ -4,6 +4,8 @@ package com.luoo.comment.controller;
import api.PageResult; import api.PageResult;
import api.Result; import api.Result;
import api.StatusCode; 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.dao.PublicationLikeDao;
import com.luoo.comment.pojo.*; import com.luoo.comment.pojo.*;
import com.luoo.comment.service.CommentService; 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.LikeService;
import com.luoo.comment.service.UserInfoService; import com.luoo.comment.service.UserInfoService;
import com.luoo.comment.util.IpUtil; import com.luoo.comment.util.IpUtil;
import com.luoo.comment.util.MySensitiveWordReplaceUtils;
import constants.Constants; import constants.Constants;
import controller.BaseController; import controller.BaseController;
import dto.UserLoginDto; import dto.UserLoginDto;
@ -79,6 +82,11 @@ public class CommentController extends BaseController {
@Autowired @Autowired
private PublicationLikeDao publicationLikeDao; private PublicationLikeDao publicationLikeDao;
@Autowired
private SensitiveWordBs sensitiveWordBs;
@Autowired
private MySensitiveWordReplaceUtils mySensitiveWordReplaceUtils;
@GetMapping @GetMapping
public Result findAll(){ public Result findAll(){
return Result.success(commentService.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<String> all = sensitiveWordBs.findAll(note);
// String all = SensitiveWordHelper.replace(note,mySensitiveWordReplaceUtils);
String all = sensitiveWordBs.replace(note,mySensitiveWordReplaceUtils);
return Result.success(all);
}
} }

@ -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<String> allow() {
List<String> list = new ArrayList<>();
list.add("五星红旗");
list.add("天安门");
return list;
}
}

@ -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<String> deny() {
List<String> 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;
}
}

@ -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;
/**
*
* <p>
*
*
* <p>
* 使
*/
public void refresh() {
// 每次数据库的信息发生变化之后,首先调用更新数据库敏感词库的方法,然后调用这个方法。
sensitiveWordBs.init();
}
}

@ -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);
}
}
Loading…
Cancel
Save