123333
This commit is contained in:
@@ -1,26 +1,54 @@
|
||||
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-lastRefreshTime";
|
||||
}
|
||||
public LocalDateTime getTime(){
|
||||
String key = getKey();
|
||||
RBucket<LocalDateTime> bucket = redissonClient.getBucket(key);
|
||||
return bucket.get();
|
||||
return "sensitive-lastUpdateTime";
|
||||
}
|
||||
|
||||
public void setTime(){
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -18,19 +18,17 @@ public class MySensitiveWordScheduleRefresh {
|
||||
@Autowired
|
||||
private SensitiveWordBs sensitiveWordBs;
|
||||
@Autowired
|
||||
private LastUpdateTimeCache lastUpdateTimeCache;
|
||||
private LastUpdateTimeCache cache;
|
||||
|
||||
@Scheduled(fixedDelay = 20,timeUnit = TimeUnit.SECONDS)
|
||||
public void job(){
|
||||
try {
|
||||
log.info("MySensitiveWordScheduleRefresh start");
|
||||
LocalDateTime time = lastUpdateTimeCache.getTime();
|
||||
LocalDateTime time = cache.getUpdateTime();
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
if(time == null || time.isBefore(now)){
|
||||
return;
|
||||
}
|
||||
refresh();
|
||||
log.info("MySensitiveWordScheduleRefresh end");
|
||||
} catch (Exception e) {
|
||||
log.error("MySensitiveWordScheduleRefresh meet ex", e);
|
||||
}
|
||||
@@ -42,6 +40,7 @@ public class MySensitiveWordScheduleRefresh {
|
||||
|
||||
private void refresh() {
|
||||
sensitiveWordBs.init();
|
||||
cache.setRefreshTime();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ 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;
|
||||
@@ -19,6 +21,8 @@ public class SensitiveWordConfig {
|
||||
|
||||
@Autowired
|
||||
private MyDdWordDeny myDdWordDeny;
|
||||
@Autowired
|
||||
private SensitiveWordConfigCache sensitiveWordConfigCache;
|
||||
|
||||
/**
|
||||
* 初始化引导类
|
||||
@@ -27,11 +31,25 @@ public class SensitiveWordConfig {
|
||||
*/
|
||||
@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))
|
||||
.ignoreRepeat(false)
|
||||
// 各种其他配置
|
||||
.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,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,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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.ruoyi.sensitive.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.github.houbb.sensitive.word.support.allow.WordAllows;
|
||||
import com.ruoyi.sensitive.cache.LastUpdateTimeCache;
|
||||
import com.ruoyi.sensitive.domain.Word;
|
||||
import com.ruoyi.sensitive.mapper.WordMapper;
|
||||
@@ -26,21 +25,21 @@ public class WordServiceImpl extends ServiceImpl<WordMapper,Word> implements Wor
|
||||
@Override
|
||||
public boolean removeWordBatchByIds(List<Integer> asList) {
|
||||
boolean re = this.removeBatchByIds(asList);
|
||||
lastUpdateTimeCache.setTime();
|
||||
lastUpdateTimeCache.setUpdateTime();
|
||||
return re;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateWordById(Word bo) {
|
||||
boolean re = this.updateById(bo);
|
||||
lastUpdateTimeCache.setTime();
|
||||
lastUpdateTimeCache.setUpdateTime();
|
||||
return re;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean saveWord(Word bo) {
|
||||
boolean re = this.save(bo);
|
||||
lastUpdateTimeCache.setTime();
|
||||
lastUpdateTimeCache.setUpdateTime();
|
||||
return re;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user