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