mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-08 16:57:09 +08:00
refactor: 解决 Sonar 问题,替换部分过期 API
This commit is contained in:
@@ -36,7 +36,6 @@ import org.springdoc.core.service.SecurityService;
|
||||
import org.springdoc.core.utils.PropertyResolverUtils;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
|
||||
import java.io.StringReader;
|
||||
@@ -152,7 +151,7 @@ public class OpenApiHandler extends OpenAPIService {
|
||||
if (this.openAPI.getPaths() == null) {
|
||||
this.openAPI.setPaths(new Paths());
|
||||
}
|
||||
if (!CollectionUtils.isEmpty(this.openAPI.getServers())) {
|
||||
if (CollUtil.isNotEmpty(this.openAPI.getServers())) {
|
||||
this.isServersPresent = true;
|
||||
}
|
||||
}
|
||||
@@ -176,7 +175,7 @@ public class OpenApiHandler extends OpenAPIService {
|
||||
buildTagsFromMethod(handlerMethod.getMethod(), tags, tagsStr, locale);
|
||||
buildTagsFromClass(handlerMethod.getBeanType(), tags, tagsStr, locale);
|
||||
|
||||
if (!CollectionUtils.isEmpty(tagsStr)) {
|
||||
if (CollUtil.isNotEmpty(tagsStr)) {
|
||||
tagsStr = tagsStr.stream()
|
||||
.map(str -> propertyResolverUtils.resolve(str, locale))
|
||||
.collect(Collectors.toSet());
|
||||
@@ -190,8 +189,8 @@ public class OpenApiHandler extends OpenAPIService {
|
||||
}
|
||||
}
|
||||
|
||||
if (!CollectionUtils.isEmpty(tagsStr)) {
|
||||
if (CollectionUtils.isEmpty(operation.getTags())) {
|
||||
if (CollUtil.isNotEmpty(tagsStr)) {
|
||||
if (CollUtil.isEmpty(operation.getTags())) {
|
||||
operation.setTags(new ArrayList<>(tagsStr));
|
||||
} else {
|
||||
Set<String> operationTagsSet = new HashSet<>(operation.getTags());
|
||||
@@ -225,10 +224,10 @@ public class OpenApiHandler extends OpenAPIService {
|
||||
}
|
||||
}
|
||||
|
||||
if (!CollectionUtils.isEmpty(tags)) {
|
||||
if (CollUtil.isNotEmpty(tags)) {
|
||||
// Existing tags
|
||||
List<Tag> openApiTags = openAPI.getTags();
|
||||
if (!CollectionUtils.isEmpty(openApiTags)) {
|
||||
if (CollUtil.isNotEmpty(openApiTags)) {
|
||||
tags.addAll(openApiTags);
|
||||
}
|
||||
openAPI.setTags(new ArrayList<>(tags));
|
||||
@@ -256,7 +255,7 @@ public class OpenApiHandler extends OpenAPIService {
|
||||
.collect(Collectors.toSet());
|
||||
methodTags.addAll(AnnotatedElementUtils
|
||||
.findAllMergedAnnotations(method, io.swagger.v3.oas.annotations.tags.Tag.class));
|
||||
if (!CollectionUtils.isEmpty(methodTags)) {
|
||||
if (CollUtil.isNotEmpty(methodTags)) {
|
||||
tagsStr.addAll(toSet(methodTags, tag -> propertyResolverUtils.resolve(tag.name(), locale)));
|
||||
List<io.swagger.v3.oas.annotations.tags.Tag> allTags = new ArrayList<>(methodTags);
|
||||
addTags(allTags, tags, locale);
|
||||
|
@@ -19,6 +19,7 @@ package top.continew.starter.cache.redisson.util;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import org.redisson.api.*;
|
||||
import org.redisson.api.options.KeysScanOptions;
|
||||
import top.continew.starter.core.constant.StringConstants;
|
||||
|
||||
import java.time.Duration;
|
||||
@@ -191,7 +192,9 @@ public class RedisUtils {
|
||||
* @return 缓存列表
|
||||
*/
|
||||
public static Collection<String> keys(String pattern) {
|
||||
return CLIENT.getKeys().getKeysStreamByPattern(pattern).toList();
|
||||
KeysScanOptions options = KeysScanOptions.defaults();
|
||||
options.pattern(pattern);
|
||||
return CLIENT.getKeys().getKeysStream(options).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -365,8 +368,21 @@ public class RedisUtils {
|
||||
* @return true:成功;false:失败
|
||||
*/
|
||||
public static boolean rateLimit(String key, RateType rateType, int rate, int rateInterval) {
|
||||
return rateLimit(key, rateType, rate, Duration.ofSeconds(rateInterval));
|
||||
}
|
||||
|
||||
/**
|
||||
* 限流
|
||||
*
|
||||
* @param key 键
|
||||
* @param rateType 限流类型(OVERALL:全局限流;PER_CLIENT:单机限流)
|
||||
* @param rate 速率(指定时间间隔产生的令牌数)
|
||||
* @param rateInterval 速率间隔(时间间隔)
|
||||
* @return true:成功;false:失败
|
||||
*/
|
||||
public static boolean rateLimit(String key, RateType rateType, int rate, Duration rateInterval) {
|
||||
RRateLimiter rateLimiter = CLIENT.getRateLimiter(key);
|
||||
rateLimiter.trySetRate(rateType, rate, rateInterval, RateIntervalUnit.SECONDS);
|
||||
rateLimiter.trySetRate(rateType, rate, rateInterval);
|
||||
return rateLimiter.tryAcquire(1);
|
||||
}
|
||||
|
||||
|
@@ -67,7 +67,6 @@ public class AsyncAutoConfiguration implements AsyncConfigurer {
|
||||
@Override
|
||||
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
|
||||
return (throwable, method, objects) -> {
|
||||
throwable.printStackTrace();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Exception message: ")
|
||||
.append(throwable.getMessage())
|
||||
|
@@ -183,12 +183,11 @@ public class QueryWrapperHelper {
|
||||
* @param queryType 查询类型
|
||||
* @param columnName 列名
|
||||
* @param fieldValue 字段值
|
||||
* @param <R> 查询数据类型
|
||||
*/
|
||||
private static <R> void parse(QueryType queryType,
|
||||
String columnName,
|
||||
Object fieldValue,
|
||||
List<Consumer<QueryWrapper>> consumers) {
|
||||
private static void parse(QueryType queryType,
|
||||
String columnName,
|
||||
Object fieldValue,
|
||||
List<Consumer<QueryWrapper>> consumers) {
|
||||
switch (queryType) {
|
||||
case EQ -> consumers.add(q -> q.eq(columnName, fieldValue));
|
||||
case NE -> consumers.add(q -> q.ne(columnName, fieldValue));
|
||||
|
@@ -37,9 +37,9 @@ public enum Algorithm {
|
||||
DES(DesEncryptor.class),
|
||||
|
||||
/**
|
||||
* PBEWithMD5AndDES
|
||||
* PBE With MD5 And DES
|
||||
*/
|
||||
PBEWithMD5AndDES(PbeWithMd5AndDesEncryptor.class),
|
||||
PBE_WITH_MD5_AND_DES(PbeWithMd5AndDesEncryptor.class),
|
||||
|
||||
/**
|
||||
* RSA
|
||||
|
@@ -16,10 +16,10 @@
|
||||
|
||||
package top.continew.starter.security.limiter.annotation;
|
||||
|
||||
import org.redisson.api.RateIntervalUnit;
|
||||
import top.continew.starter.security.limiter.enums.LimitType;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 限流注解
|
||||
@@ -60,7 +60,7 @@ public @interface RateLimiter {
|
||||
/**
|
||||
* 速率间隔时间单位(默认:毫秒)
|
||||
*/
|
||||
RateIntervalUnit unit() default RateIntervalUnit.MILLISECONDS;
|
||||
TimeUnit unit() default TimeUnit.MILLISECONDS;
|
||||
|
||||
/**
|
||||
* 提示信息
|
||||
|
@@ -39,6 +39,7 @@ import top.continew.starter.security.limiter.exception.RateLimiterException;
|
||||
import top.continew.starter.web.util.SpringWebUtils;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.time.Duration;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@@ -129,12 +130,11 @@ public class RateLimiterAspect {
|
||||
// 限流器配置
|
||||
RateType rateType = rateLimiter.type() == LimitType.CLUSTER ? RateType.PER_CLIENT : RateType.OVERALL;
|
||||
int rate = rateLimiter.rate();
|
||||
int rateInterval = rateLimiter.interval();
|
||||
RateIntervalUnit rateIntervalUnit = rateLimiter.unit();
|
||||
Duration rateInterval = Duration.ofMillis(rateLimiter.unit().toMillis(rateLimiter.interval()));
|
||||
// 判断是否需要更新限流器
|
||||
if (this.isConfigurationUpdateNeeded(rRateLimiter, rateType, rate, rateInterval, rateIntervalUnit)) {
|
||||
if (this.isConfigurationUpdateNeeded(rRateLimiter, rateType, rate, rateInterval)) {
|
||||
// 更新限流器
|
||||
rRateLimiter.setRate(rateType, rate, rateInterval, rateIntervalUnit);
|
||||
rRateLimiter.setRate(rateType, rate, rateInterval);
|
||||
}
|
||||
// 尝试获取令牌
|
||||
return !rRateLimiter.tryAcquire();
|
||||
@@ -181,20 +181,18 @@ public class RateLimiterAspect {
|
||||
/**
|
||||
* 判断是否需要更新限流器配置
|
||||
*
|
||||
* @param rRateLimiter 限流器
|
||||
* @param rateType 限流类型(OVERALL:全局限流;PER_CLIENT:单机限流)
|
||||
* @param rate 速率(指定时间间隔产生的令牌数)
|
||||
* @param rateInterval 速率间隔
|
||||
* @param rateIntervalUnit 时间单位
|
||||
* @param rRateLimiter 限流器
|
||||
* @param rateType 限流类型(OVERALL:全局限流;PER_CLIENT:单机限流)
|
||||
* @param rate 速率(指定时间间隔产生的令牌数)
|
||||
* @param rateInterval 速率间隔
|
||||
* @return 是否需要更新配置
|
||||
*/
|
||||
private boolean isConfigurationUpdateNeeded(RRateLimiter rRateLimiter,
|
||||
RateType rateType,
|
||||
long rate,
|
||||
long rateInterval,
|
||||
RateIntervalUnit rateIntervalUnit) {
|
||||
Duration rateInterval) {
|
||||
RateLimiterConfig config = rRateLimiter.getConfig();
|
||||
return !Objects.equals(config.getRateType(), rateType) || !Objects.equals(config.getRate(), rate) || !Objects
|
||||
.equals(config.getRateInterval(), rateIntervalUnit.toMillis(rateInterval));
|
||||
.equals(config.getRateInterval(), rateInterval.toMillis());
|
||||
}
|
||||
}
|
||||
|
@@ -34,7 +34,7 @@ public enum MaskType implements IMaskStrategy {
|
||||
CUSTOM {
|
||||
@Override
|
||||
public String mask(String str, char character, int left, int right) {
|
||||
return CharSequenceUtil.replace(str, left, str.length() - right, character);
|
||||
return CharSequenceUtil.replaceByCodePoint(str, left, str.length() - right, character);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -45,7 +45,7 @@ public enum MaskType implements IMaskStrategy {
|
||||
MOBILE_PHONE {
|
||||
@Override
|
||||
public String mask(String str, char character, int left, int right) {
|
||||
return CharSequenceUtil.replace(str, 3, str.length() - 4, character);
|
||||
return CharSequenceUtil.replaceByCodePoint(str, 3, str.length() - 4, character);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -58,7 +58,7 @@ public enum MaskType implements IMaskStrategy {
|
||||
FIXED_PHONE {
|
||||
@Override
|
||||
public String mask(String str, char character, int left, int right) {
|
||||
return CharSequenceUtil.replace(str, 4, str.length() - 2, character);
|
||||
return CharSequenceUtil.replaceByCodePoint(str, 4, str.length() - 2, character);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -76,7 +76,7 @@ public enum MaskType implements IMaskStrategy {
|
||||
if (index <= 1) {
|
||||
return str;
|
||||
}
|
||||
return CharSequenceUtil.replace(str, 1, index, character);
|
||||
return CharSequenceUtil.replaceByCodePoint(str, 1, index, character);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -89,7 +89,7 @@ public enum MaskType implements IMaskStrategy {
|
||||
ID_CARD {
|
||||
@Override
|
||||
public String mask(String str, char character, int left, int right) {
|
||||
return CharSequenceUtil.replace(str, 1, str.length() - 2, character);
|
||||
return CharSequenceUtil.replaceByCodePoint(str, 1, str.length() - 2, character);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -140,11 +140,11 @@ public enum MaskType implements IMaskStrategy {
|
||||
// 普通车牌
|
||||
int length = str.length();
|
||||
if (length == 7) {
|
||||
return CharSequenceUtil.replace(str, 3, 6, character);
|
||||
return CharSequenceUtil.replaceByCodePoint(str, 3, 6, character);
|
||||
}
|
||||
// 新能源车牌
|
||||
if (length == 8) {
|
||||
return CharSequenceUtil.replace(str, 3, 7, character);
|
||||
return CharSequenceUtil.replaceByCodePoint(str, 3, 7, character);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
@@ -159,7 +159,7 @@ public enum MaskType implements IMaskStrategy {
|
||||
CHINESE_NAME {
|
||||
@Override
|
||||
public String mask(String str, char character, int left, int right) {
|
||||
return CharSequenceUtil.replace(str, 1, str.length(), character);
|
||||
return CharSequenceUtil.replaceByCodePoint(str, 1, str.length(), character);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -186,7 +186,7 @@ public enum MaskType implements IMaskStrategy {
|
||||
@Override
|
||||
public String mask(String str, char character, int left, int right) {
|
||||
int length = str.length();
|
||||
return CharSequenceUtil.replace(str, length - 8, length, character);
|
||||
return CharSequenceUtil.replaceByCodePoint(str, length - 8, length, character);
|
||||
}
|
||||
},
|
||||
|
||||
|
@@ -16,7 +16,7 @@
|
||||
|
||||
package top.continew.starter.web.autoconfigure.xss;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import jakarta.servlet.*;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.slf4j.Logger;
|
||||
@@ -55,14 +55,14 @@ public class XssFilter implements Filter {
|
||||
if (servletRequest instanceof HttpServletRequest request && xssProperties.isEnabled()) {
|
||||
// 放行路由:忽略 XSS 过滤
|
||||
List<String> excludePatterns = xssProperties.getExcludePatterns();
|
||||
if (CollectionUtil.isNotEmpty(excludePatterns) && SpringWebUtils.isMatch(request
|
||||
if (CollUtil.isNotEmpty(excludePatterns) && SpringWebUtils.isMatch(request
|
||||
.getServletPath(), excludePatterns)) {
|
||||
filterChain.doFilter(request, servletResponse);
|
||||
return;
|
||||
}
|
||||
// 拦截路由:执行 XSS 过滤
|
||||
List<String> includePatterns = xssProperties.getIncludePatterns();
|
||||
if (CollectionUtil.isNotEmpty(includePatterns)) {
|
||||
if (CollUtil.isNotEmpty(includePatterns)) {
|
||||
if (SpringWebUtils.isMatch(request.getServletPath(), includePatterns)) {
|
||||
filterChain.doFilter(new XssServletRequestWrapper(request, xssProperties), servletResponse);
|
||||
} else {
|
||||
|
@@ -16,7 +16,7 @@
|
||||
|
||||
package top.continew.starter.web.autoconfigure.xss;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
@@ -110,7 +110,7 @@ public class XssServletRequestWrapper extends HttpServletRequestWrapper {
|
||||
// 转义
|
||||
if (XssMode.ESCAPE.equals(mode)) {
|
||||
List<String> reStr = ReUtil.findAllGroup0(HtmlUtil.RE_HTML_MARK, content);
|
||||
if (CollectionUtil.isEmpty(reStr)) {
|
||||
if (CollUtil.isEmpty(reStr)) {
|
||||
return content;
|
||||
}
|
||||
for (String s : reStr) {
|
||||
|
Reference in New Issue
Block a user