Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增敏感词功能、代码生成新增导入功能、RedisUtils工具新增ZSet #76

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ruoyi-admin/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ tenant:
- sys_oss_rule
- sys_dict_data
- sys_dict_type
- sys_sensitive_word

# MyBatisPlus配置
# https://baomidou.com/config/
Expand Down
5 changes: 5 additions & 0 deletions ruoyi-common/ruoyi-common-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@
<scope>compile</scope>
</dependency>

<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-dfa</artifactId>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public interface CacheConstants {
*/
String SYS_DICT_KEY = GlobalConstants.GLOBAL_REDIS_KEY + "sys_dict:";

/**
* 敏感词管理 cache key
*/
String SYS_SENSITIVE_WORD = GlobalConstants.GLOBAL_REDIS_KEY + "sys_sensitive_word:";

/**
* 字典类型管理 cache key
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ValidatorUtils {
public static <T> void validate(T object, Class<?>... groups) {
Set<ConstraintViolation<T>> validate = VALID.validate(object, groups);
if (!validate.isEmpty()) {
throw new ConstraintViolationException("参数校验异常", validate);
throw new ConstraintViolationException("参数校验异常: " + validate.iterator().next().getMessage(), validate);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package org.dromara.common.core.utils.sensitive;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.dfa.FoundWord;
import cn.hutool.dfa.SensitiveProcessor;
import cn.hutool.dfa.WordTree;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 敏感词替换器
*
* @author hexm
*/
public class SensitiveWordReplacer {
private final WordTree sensitiveTree = new WordTree();

public SensitiveWordReplacer() {
}

public SensitiveWordReplacer(Collection<String> sensitiveWords) {
sensitiveTree.addWords(sensitiveWords);
}

public void addWord(String word) {
sensitiveTree.addWord(word);
}

public void addWords(Collection<String> sensitiveWords) {
sensitiveTree.addWords(sensitiveWords);
}

public void clear() {
sensitiveTree.clear();
}

/**
* 判断是否包含敏感词
*
* @param text 文本
* @return
*/
public boolean containsSensitiveWord(String text) {
return sensitiveTree.isMatch(text);
}

/**
* 查找敏感词,返回找到的第一个敏感词
*
* @param text 文本
* @return 敏感词
*/
public FoundWord getFoundFirstSensitive(String text) {
return sensitiveTree.matchWord(text);
}

/**
* 查找敏感词,返回找到的所有敏感词
*
* @param text 文本
* @return 敏感词
*/
public List<FoundWord> getFoundAllSensitive(String text) {
return sensitiveTree.matchAllWords(text);
}

/**
* 查找敏感词,返回找到的所有敏感词<br>
* 密集匹配原则:假如关键词有 ab,b,文本是abab,将匹配 [ab,b,ab]<br>
* 贪婪匹配(最长匹配)原则:假如关键字a,ab,最长匹配将匹配[a, ab]
*
* @param text 文本
* @param isDensityMatch 是否使用密集匹配原则
* @param isGreedMatch 是否使用贪婪匹配(最长匹配)原则
* @return 敏感词
*/
public List<FoundWord> getFoundAllSensitive(String text, boolean isDensityMatch, boolean isGreedMatch) {
return sensitiveTree.matchAllWords(text, -1, isDensityMatch, isGreedMatch);
}

/**
* 处理过滤文本中的敏感词,默认替换成*
*
* @param text 文本
* @return 敏感词过滤处理后的文本
*/
public String sensitiveWordReplace(String text) {
return sensitiveWordReplace(text, true, null);
}

/**
* 处理过滤文本中的敏感词,默认替换成*
*
* @param text 文本
* @param isGreedMatch 贪婪匹配(最长匹配)原则:假如关键字a,ab,最长匹配将匹配[a, ab]
* @param sensitiveProcessor 敏感词处理器,默认按匹配内容的字符数替换成*
* @return 敏感词过滤处理后的文本
*/
public String sensitiveWordReplace(String text, boolean isGreedMatch, SensitiveProcessor sensitiveProcessor) {
if (StrUtil.isEmpty(text)) {
return text;
}

//敏感词过滤场景下,不需要密集匹配
final List<FoundWord> foundWordList = getFoundAllSensitive(text, true, isGreedMatch);
if (CollUtil.isEmpty(foundWordList)) {
return text;
}
sensitiveProcessor = sensitiveProcessor == null ? new SensitiveProcessor() {
} : sensitiveProcessor;

final Map<Integer, FoundWord> foundWordMap = new HashMap<>(foundWordList.size(), 1);
foundWordList.forEach(foundWord -> foundWordMap.put(foundWord.getStartIndex(), foundWord));
final int length = text.length();
final StringBuilder textStringBuilder = new StringBuilder();
for (int i = 0; i < length; i++) {
final FoundWord fw = foundWordMap.get(i);
if (fw != null) {
textStringBuilder.append(sensitiveProcessor.process(fw));
i = fw.getEndIndex();
} else {
textStringBuilder.append(text.charAt(i));
}
}
return textStringBuilder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import lombok.NoArgsConstructor;
import org.dromara.common.core.utils.spring.SpringUtils;
import org.redisson.api.*;
import org.redisson.api.listener.MessageListener;

import java.time.Duration;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -87,6 +89,29 @@ public static <T> void subscribe(String channelKey, Class<T> clazz, Consumer<T>
topic.addListener(clazz, (channel, msg) -> consumer.accept(msg));
}

/**
* 订阅通道接收消息
*
* @param channelKey 通道key
* @param clazz 消息类型
* @param listener 自定义处理
*/
public static <T> void subscribe(String channelKey, Class<T> clazz, MessageListener<? extends T> listener) {
RTopic topic = CLIENT.getTopic(channelKey);
topic.addListener(clazz, listener);
}

/**
* 取消订阅
*
* @param channelKey 通道key
* @param listener 自定义处理
*/
public static void unsubscribe(String channelKey, MessageListener<?> listener) {
RTopic topic = CLIENT.getTopic(channelKey);
topic.removeListener(listener);
}

/**
* 缓存基本的对象,Integer、String、实体类等
*
Expand Down Expand Up @@ -395,6 +420,78 @@ public static <T> Set<T> getSet(final String key) {
return rSet.readAll();
}

/**
* 缓存ZSet
*
* @param key 缓存键值
* @param dataSet 缓存的数据
* @return 缓存数据的对象
*/
public static <T> int addZSet(final String key, final Map<T, Double> dataSet) {
RScoredSortedSet<T> scoredSortedSet = CLIENT.getScoredSortedSet(key);
return scoredSortedSet.addAll(dataSet);
}

/**
* 缓存ZSet
*
* @param key 缓存键值
* @param dataSet 缓存的数据
* @return 缓存数据的对象
*/
public static <T> void setZSet(final String key, final Map<T, Double> dataSet) {
RBatch batch = CLIENT.createBatch();
RScoredSortedSetAsync<T> scoredSortedSet = batch.getScoredSortedSet(key);
scoredSortedSet.deleteAsync();
scoredSortedSet.addAllAsync(dataSet);
batch.execute();
}

/**
* 缓存Set
*
* @param key 缓存键值
* @param dataSet 缓存的数据
* @param expire 过期时间
* @return 缓存数据的对象
*/
public static <T> void setZSet(final String key, final Map<T, Double> dataSet, final Duration expire) {
RBatch batch = CLIENT.createBatch();
RScoredSortedSetAsync<T> scoredSortedSet = batch.getScoredSortedSet(key);
scoredSortedSet.deleteAsync();
scoredSortedSet.expireAsync(expire);
scoredSortedSet.addAllAsync(dataSet);
batch.execute();
}

/**
* 注册ZSet监听器
* <p>
* key 监听器需开启 `notify-keyspace-events` 等 redis 相关配置
*
* @param key 缓存的键值
* @param listener 监听器配置
*/
public static <T> void addZSetListener(final String key, final ObjectListener listener) {
RScoredSortedSet<T> scoredSortedSet = CLIENT.getScoredSortedSet(key);
scoredSortedSet.addListener(listener);
}

/**
* 获得缓存的set
*
* @param key 缓存的key
* @return set对象
*/
public static <T> Set<T> getZSet(final String key) {
RScoredSortedSet<T> scoredSortedSet = CLIENT.getScoredSortedSet(key);
Collection<T> all = scoredSortedSet.readAll();
if (all instanceof Set) {
return (Set<T>) all;
}
return new LinkedHashSet<>(all);
}

/**
* put缓存Map
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.dromara.common.websocket.constant;

import org.dromara.common.core.constant.GlobalConstants;

/**
* websocket的常量配置
*
Expand All @@ -14,7 +16,7 @@ public interface WebSocketConstants {
/**
* 订阅的频道
*/
String WEB_SOCKET_TOPIC = "global:websocket";
String WEB_SOCKET_TOPIC = GlobalConstants.GLOBAL_REDIS_KEY + "websocket";

/**
* 前端心跳检查的命令
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public class GenTableOptions implements Serializable {
* 是否使用删除方法
*/
private Boolean isUseRemoveMethod = true;
/**
* 是否使用导入方法
*/
private Boolean isUseImportMethod = false;
/**
* 是否使用导出方法
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public Map<String, String> tempPreviewCode(GenTableBo tableBo) {

private Map<String, String> previewCode(GenTableVo tableVo) {
List<Long> menuIds = new ArrayList<>();
for (int i = 0; i < 6; i++) {
for (int i = 0; i < 7; i++) {
menuIds.add(identifierGenerator.nextId(null).longValue());
}
tableVo.setMenuIds(menuIds);
Expand Down Expand Up @@ -430,7 +430,7 @@ private void generatorCode(Long tableId, ZipOutputStream zip) {
// 查询表信息
GenTableVo table = baseMapper.selectGenTableById(tableId);
List<Long> menuIds = new ArrayList<>();
for (int i = 0; i < 6; i++) {
for (int i = 0; i < 7; i++) {
menuIds.add(identifierGenerator.nextId(null).longValue());
}
table.setMenuIds(menuIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,15 @@ public static List<String> getTemplateList(GenTableVo table) {
if (options.getIsUseQuery()) {
templates.add("vm/java/query.java.vm");
}
if (options.getIsUseBO()) {
templates.add("vm/java/bo.java.vm");
}
if (options.getIsUseVO()) {
templates.add("vm/java/vo.java.vm");
}
if (options.getIsUseBO()) {
templates.add("vm/java/bo.java.vm");
if (options.getIsUseImportMethod()) {
templates.add("vm/java/import.java.vm");
templates.add("vm/java/listener.java.vm");
}
if (options.getIsUseController()) {
templates.add("vm/java/controller.java.vm");
Expand Down Expand Up @@ -177,6 +181,12 @@ public static String getFileName(String template, GenTableVo genTable) {
if (template.contains("bo.java.vm")) {
fileName = StringUtils.format("{}/domain/bo/{}Bo.java", javaPath, className);
}
if (template.contains("import.java.vm")) {
fileName = StringUtils.format("{}/domain/template/{}ImportTemplate.java", javaPath, className);
}
if (template.contains("listener.java.vm")) {
fileName = StringUtils.format("{}/listener/{}ImportListener.java", javaPath, className);
}
if (template.contains("mapper.java.vm")) {
fileName = StringUtils.format("{}/mapper/{}Mapper.java", javaPath, className);
} else if (template.contains("service.java.vm")) {
Expand Down
Loading