init
This commit is contained in:
56
ruoyi-sensitive-word/src/main/java/com/ruoyi/sensitive/cache/LastUpdateTimeCache.java
vendored
Normal file
56
ruoyi-sensitive-word/src/main/java/com/ruoyi/sensitive/cache/LastUpdateTimeCache.java
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
package com.ruoyi.sensitive.cache;
|
||||
|
||||
import com.ruoyi.common.constant.DateConstant;
|
||||
import org.redisson.api.RBucket;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
@Component
|
||||
public class LastUpdateTimeCache {
|
||||
@Autowired
|
||||
private RedissonClient redissonClient;
|
||||
private String getKey(){
|
||||
return "sensitive-lastUpdateTime";
|
||||
}
|
||||
|
||||
private String getRefreshKey(){
|
||||
return "sensitive-lastRefreshTime";
|
||||
}
|
||||
|
||||
public void setRefreshTime(){
|
||||
String key = getRefreshKey();
|
||||
RBucket<LocalDateTime> bucket = redissonClient.getBucket(key);
|
||||
bucket.set(LocalDateTime.now());
|
||||
}
|
||||
|
||||
public LocalDateTime getRefreshTime(){
|
||||
String key = getRefreshKey();
|
||||
RBucket<String> bucket = redissonClient.getBucket(key);
|
||||
String s = bucket.get();
|
||||
if(s == null){
|
||||
return null;
|
||||
}
|
||||
return LocalDateTime.parse(s, DateTimeFormatter.ofPattern(DateConstant.PATTERN_DATETIME));
|
||||
}
|
||||
|
||||
|
||||
public LocalDateTime getUpdateTime(){
|
||||
String key = getKey();
|
||||
RBucket<String> bucket = redissonClient.getBucket(key);
|
||||
String s = bucket.get();
|
||||
if(s == null){
|
||||
return null;
|
||||
}
|
||||
return LocalDateTime.parse(s, DateTimeFormatter.ofPattern(DateConstant.PATTERN_DATETIME));
|
||||
}
|
||||
|
||||
public void setUpdateTime(){
|
||||
String key = getKey();
|
||||
RBucket<LocalDateTime> bucket = redissonClient.getBucket(key);
|
||||
bucket.set(LocalDateTime.now());
|
||||
}
|
||||
}
|
||||
28
ruoyi-sensitive-word/src/main/java/com/ruoyi/sensitive/cache/SensitiveWordConfigCache.java
vendored
Normal file
28
ruoyi-sensitive-word/src/main/java/com/ruoyi/sensitive/cache/SensitiveWordConfigCache.java
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
package com.ruoyi.sensitive.cache;
|
||||
|
||||
import com.ruoyi.sensitive.dto.SensitiveConfig;
|
||||
import org.redisson.api.RBucket;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SensitiveWordConfigCache {
|
||||
@Autowired
|
||||
private RedissonClient redissonClient;
|
||||
private String getKey(){
|
||||
return "sensitive-config";
|
||||
}
|
||||
|
||||
public void setConfig(SensitiveConfig config){
|
||||
String key = getKey();
|
||||
RBucket<SensitiveConfig> bucket = redissonClient.getBucket(key);
|
||||
bucket.set(config);
|
||||
}
|
||||
|
||||
public SensitiveConfig getConfig(){
|
||||
String key = getKey();
|
||||
RBucket<SensitiveConfig> bucket = redissonClient.getBucket(key);
|
||||
return bucket.get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.ruoyi.sensitive.config;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.github.houbb.heaven.support.handler.IHandler;
|
||||
import com.github.houbb.heaven.util.util.CollectionUtil;
|
||||
import com.github.houbb.sensitive.word.api.IWordAllow;
|
||||
import com.ruoyi.sensitive.domain.Word;
|
||||
import com.ruoyi.sensitive.service.WordService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author binbin.hou
|
||||
* @since 1.1.0
|
||||
*/
|
||||
@Component
|
||||
public class MyDdWordAllow implements IWordAllow {
|
||||
|
||||
@Autowired
|
||||
private WordService wordService;
|
||||
|
||||
@Override
|
||||
public List<String> allow() {
|
||||
List<Word> wordList = wordService.list(Wrappers.lambdaQuery(Word.class)
|
||||
.select(Word::getWord)
|
||||
.eq(Word::getEnableStatus,1)
|
||||
.eq(Word::getType, 1));
|
||||
return CollectionUtil.toList(wordList, new IHandler<Word, String>() {
|
||||
@Override
|
||||
public String handle(Word word) {
|
||||
return word.getWord();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.ruoyi.sensitive.config;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.github.houbb.heaven.support.handler.IHandler;
|
||||
import com.github.houbb.heaven.util.util.CollectionUtil;
|
||||
import com.github.houbb.sensitive.word.api.IWordDeny;
|
||||
import com.ruoyi.sensitive.domain.Word;
|
||||
import com.ruoyi.sensitive.service.WordService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author binbin.hou
|
||||
* @since 1.1.0
|
||||
*/
|
||||
@Component
|
||||
public class MyDdWordDeny implements IWordDeny {
|
||||
|
||||
@Autowired
|
||||
private WordService wordService;
|
||||
|
||||
@Override
|
||||
public List<String> deny() {
|
||||
List<Word> wordList = wordService.list(Wrappers.lambdaQuery(Word.class)
|
||||
.select(Word::getWord)
|
||||
.eq(Word::getEnableStatus,1)
|
||||
.eq(Word::getType, 2));
|
||||
|
||||
return CollectionUtil.toList(wordList, new IHandler<Word, String>() {
|
||||
@Override
|
||||
public String handle(Word word) {
|
||||
return word.getWord();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.ruoyi.sensitive.config;
|
||||
|
||||
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
|
||||
import com.ruoyi.sensitive.cache.LastUpdateTimeCache;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class MySensitiveWordScheduleRefresh {
|
||||
|
||||
@Autowired
|
||||
private SensitiveWordBs sensitiveWordBs;
|
||||
@Autowired
|
||||
private LastUpdateTimeCache cache;
|
||||
|
||||
@Scheduled(fixedDelay = 20,timeUnit = TimeUnit.SECONDS)
|
||||
public void job(){
|
||||
try {
|
||||
LocalDateTime time = cache.getUpdateTime();
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
if(time == null || time.isBefore(now)){
|
||||
return;
|
||||
}
|
||||
refresh();
|
||||
} catch (Exception e) {
|
||||
log.error("MySensitiveWordScheduleRefresh meet ex", e);
|
||||
}
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
}
|
||||
|
||||
private void refresh() {
|
||||
sensitiveWordBs.init();
|
||||
cache.setRefreshTime();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.ruoyi.sensitive.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.ruoyi.sensitive.cache.SensitiveWordConfigCache;
|
||||
import com.ruoyi.sensitive.dto.SensitiveConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author binbin.hou
|
||||
* @since 1.1.0
|
||||
*/
|
||||
@Configuration
|
||||
public class SensitiveWordConfig {
|
||||
|
||||
@Autowired
|
||||
private MyDdWordAllow myDdWordAllow;
|
||||
|
||||
@Autowired
|
||||
private MyDdWordDeny myDdWordDeny;
|
||||
@Autowired
|
||||
private SensitiveWordConfigCache sensitiveWordConfigCache;
|
||||
|
||||
/**
|
||||
* 初始化引导类
|
||||
* @return 初始化引导类
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Bean
|
||||
public SensitiveWordBs sensitiveWordBs() {
|
||||
SensitiveConfig sensitiveConfig = sensitiveWordConfigCache.getConfig();
|
||||
if(sensitiveConfig == null){
|
||||
sensitiveConfig = new SensitiveConfig();
|
||||
}
|
||||
return SensitiveWordBs.newInstance()
|
||||
.wordAllow(WordAllows.chains(WordAllows.defaults(), myDdWordAllow))
|
||||
.wordDeny(WordDenys.chains(WordDenys.defaults(), myDdWordDeny))
|
||||
// 各种其他配置
|
||||
.ignoreCase(sensitiveConfig.isIgnoreCase())
|
||||
.ignoreWidth(sensitiveConfig.isIgnoreWidth())
|
||||
.ignoreNumStyle(sensitiveConfig.isIgnoreNumStyle())
|
||||
.ignoreChineseStyle(sensitiveConfig.isIgnoreChineseStyle())
|
||||
.ignoreEnglishStyle(sensitiveConfig.isIgnoreEnglishStyle())
|
||||
.ignoreRepeat(sensitiveConfig.isIgnoreRepeat())
|
||||
.enableNumCheck(sensitiveConfig.isEnableNumCheck())
|
||||
.enableEmailCheck(sensitiveConfig.isEnableEmailCheck())
|
||||
.enableUrlCheck(sensitiveConfig.isEnableUrlCheck())
|
||||
.enableWordCheck(sensitiveConfig.isEnableWordCheck())
|
||||
.numCheckLen(sensitiveConfig.getNumCheckLen())
|
||||
.init();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.ruoyi.sensitive.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 敏感词对象 cai_word
|
||||
*
|
||||
* @author 77
|
||||
* @date 2024-02-19
|
||||
*/
|
||||
@Data
|
||||
@TableName("xq_word")
|
||||
public class Word implements Serializable {
|
||||
|
||||
private static final long serialVersionUID=1L;
|
||||
|
||||
/**
|
||||
* 应用自增主键
|
||||
*/
|
||||
@TableId(value = "id")
|
||||
private Integer id;
|
||||
/**
|
||||
* 单词
|
||||
*/
|
||||
private String word;
|
||||
/**
|
||||
* 1-白名单 2-黑名单
|
||||
*/
|
||||
private Integer type;
|
||||
/**
|
||||
* 启用状态
|
||||
*/
|
||||
private Integer enableStatus;
|
||||
/**
|
||||
* 配置描述
|
||||
*/
|
||||
private String remark;
|
||||
/**
|
||||
* 操作员名称
|
||||
*/
|
||||
private String operatorId;
|
||||
|
||||
private LocalDateTime updateTime;
|
||||
private LocalDateTime createTime;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.ruoyi.sensitive.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class SensitiveConfig {
|
||||
/**
|
||||
* 是否忽略大小写
|
||||
*/
|
||||
private boolean ignoreCase = true;
|
||||
/**
|
||||
* 是否忽略全角、半角
|
||||
*/
|
||||
private boolean ignoreWidth = true;
|
||||
/**
|
||||
* 是否忽略数字样式
|
||||
*/
|
||||
private boolean ignoreNumStyle = true;
|
||||
/**
|
||||
* 是否忽略中文样式
|
||||
*/
|
||||
private boolean ignoreChineseStyle = true;
|
||||
/**
|
||||
* 是否忽略英文样式
|
||||
*/
|
||||
private boolean ignoreEnglishStyle = true;
|
||||
/**
|
||||
* 是否忽略重复
|
||||
*/
|
||||
private boolean ignoreRepeat = false;
|
||||
|
||||
// 开启校验
|
||||
/**
|
||||
* 启用数字检测
|
||||
*/
|
||||
private boolean enableNumCheck = true;
|
||||
/**
|
||||
* 启用邮箱检测
|
||||
*/
|
||||
private boolean enableEmailCheck = true;
|
||||
/**
|
||||
* 启用 URL 检测
|
||||
*/
|
||||
private boolean enableUrlCheck = true;
|
||||
|
||||
/**
|
||||
* 单词校验
|
||||
* @since 0.4.0
|
||||
*/
|
||||
private boolean enableWordCheck = true;
|
||||
|
||||
// 额外配置
|
||||
/**
|
||||
* 检测数字时的长度
|
||||
*/
|
||||
private int numCheckLen = 8;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.ruoyi.sensitive.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class WordTestResp {
|
||||
private String sourceText;
|
||||
private String targetText;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.ruoyi.sensitive.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.ruoyi.sensitive.domain.Word;
|
||||
|
||||
/**
|
||||
* 敏感词Mapper接口
|
||||
*
|
||||
* @author 77
|
||||
* @date 2024-02-19
|
||||
*/
|
||||
public interface WordMapper extends BaseMapper<Word> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.ruoyi.sensitive.service;
|
||||
|
||||
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
|
||||
import com.ruoyi.sensitive.cache.SensitiveWordConfigCache;
|
||||
import com.ruoyi.sensitive.dto.SensitiveConfig;
|
||||
import com.ruoyi.sensitive.dto.WordTestResp;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SensitiveWordManager {
|
||||
|
||||
@Autowired
|
||||
private SensitiveWordBs sensitiveWordBs;
|
||||
@Autowired
|
||||
private SensitiveWordConfigCache sensitiveWordConfigCache;
|
||||
|
||||
public WordTestResp test(String text) {
|
||||
String results = sensitiveWordBs.replace(text);
|
||||
WordTestResp wordTestResp = new WordTestResp();
|
||||
wordTestResp.setSourceText(text);
|
||||
wordTestResp.setTargetText(results);
|
||||
return wordTestResp;
|
||||
}
|
||||
|
||||
public void updateSensitiveConfig(SensitiveConfig sensitiveConfig) {
|
||||
sensitiveWordBs
|
||||
.ignoreCase(sensitiveConfig.isIgnoreCase())
|
||||
.ignoreWidth(sensitiveConfig.isIgnoreWidth())
|
||||
.ignoreNumStyle(sensitiveConfig.isIgnoreNumStyle())
|
||||
.ignoreChineseStyle(sensitiveConfig.isIgnoreChineseStyle())
|
||||
.ignoreEnglishStyle(sensitiveConfig.isIgnoreEnglishStyle())
|
||||
.ignoreRepeat(sensitiveConfig.isIgnoreRepeat())
|
||||
.enableNumCheck(sensitiveConfig.isEnableNumCheck())
|
||||
.enableEmailCheck(sensitiveConfig.isEnableEmailCheck())
|
||||
.enableUrlCheck(sensitiveConfig.isEnableUrlCheck())
|
||||
.enableWordCheck(sensitiveConfig.isEnableWordCheck())
|
||||
.numCheckLen(sensitiveConfig.getNumCheckLen())
|
||||
.init();
|
||||
sensitiveWordConfigCache.setConfig(sensitiveConfig);
|
||||
}
|
||||
|
||||
public SensitiveConfig getSensitiveConfig(){
|
||||
return sensitiveWordConfigCache.getConfig();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.ruoyi.sensitive.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.ruoyi.sensitive.domain.Word;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 敏感词Service接口
|
||||
*
|
||||
* @author 77
|
||||
* @date 2024-02-19
|
||||
*/
|
||||
public interface WordService extends IService<Word> {
|
||||
|
||||
boolean removeWordBatchByIds(List<Integer> asList);
|
||||
|
||||
boolean updateWordById(Word bo);
|
||||
|
||||
boolean saveWord(Word bo);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.ruoyi.sensitive.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.ruoyi.sensitive.cache.LastUpdateTimeCache;
|
||||
import com.ruoyi.sensitive.domain.Word;
|
||||
import com.ruoyi.sensitive.mapper.WordMapper;
|
||||
import com.ruoyi.sensitive.service.WordService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 敏感词Service业务层处理
|
||||
*
|
||||
* @author 77
|
||||
* @date 2024-02-19
|
||||
*/
|
||||
@Service
|
||||
public class WordServiceImpl extends ServiceImpl<WordMapper,Word> implements WordService {
|
||||
|
||||
@Autowired
|
||||
private LastUpdateTimeCache lastUpdateTimeCache;
|
||||
|
||||
@Override
|
||||
public boolean removeWordBatchByIds(List<Integer> asList) {
|
||||
boolean re = this.removeBatchByIds(asList);
|
||||
lastUpdateTimeCache.setUpdateTime();
|
||||
return re;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateWordById(Word bo) {
|
||||
boolean re = this.updateById(bo);
|
||||
lastUpdateTimeCache.setUpdateTime();
|
||||
return re;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean saveWord(Word bo) {
|
||||
boolean re = this.save(bo);
|
||||
lastUpdateTimeCache.setUpdateTime();
|
||||
return re;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.ruoyi.sensitive.mapper.WordMapper">
|
||||
|
||||
<resultMap type="com.ruoyi.sensitive.domain.Word" id="WordResult">
|
||||
<result property="id" column="id"/>
|
||||
<result property="word" column="word"/>
|
||||
<result property="type" column="type"/>
|
||||
<result property="enableStatus" column="enable_status"/>
|
||||
<result property="remark" column="remark"/>
|
||||
<result property="operatorId" column="operator_id"/>
|
||||
<result property="createTime" column="create_time"/>
|
||||
<result property="updateTime" column="update_time"/>
|
||||
</resultMap>
|
||||
|
||||
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user