parent
ac1a0cd3de
commit
ac8da9fae3
@ -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;
|
||||
}
|
||||
}
|
@ -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,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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,2 @@
|
||||
雀乐
|
||||
落网
|
Loading…
Reference in new issue