mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-08 16:57:09 +08:00
refactor: 优化部分代码
修复 Sonar 扫描问题
This commit is contained in:
@@ -37,6 +37,9 @@ import top.charles7c.continew.starter.cache.redisson.autoconfigure.RedissonAutoC
|
||||
@Slf4j
|
||||
abstract class SaTokenDaoConfiguration {
|
||||
|
||||
private SaTokenDaoConfiguration() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义持久层实现类-Redis
|
||||
*/
|
||||
|
@@ -40,7 +40,7 @@ public class GlobalSaTokenExceptionHandler {
|
||||
* 认证异常-登录认证
|
||||
*/
|
||||
@ExceptionHandler(NotLoginException.class)
|
||||
public R handleNotLoginException(NotLoginException e, HttpServletRequest request) {
|
||||
public R<Void> handleNotLoginException(NotLoginException e, HttpServletRequest request) {
|
||||
log.error("请求地址 [{}],认证失败,无法访问系统资源。", request.getRequestURI(), e);
|
||||
String errorMsg = switch (e.getType()) {
|
||||
case NotLoginException.KICK_OUT -> "您已被踢下线。";
|
||||
@@ -54,7 +54,7 @@ public class GlobalSaTokenExceptionHandler {
|
||||
* 认证异常-权限认证
|
||||
*/
|
||||
@ExceptionHandler(NotPermissionException.class)
|
||||
public R handleNotPermissionException(NotPermissionException e, HttpServletRequest request) {
|
||||
public R<Void> handleNotPermissionException(NotPermissionException e, HttpServletRequest request) {
|
||||
log.error("请求地址 [{}],权限码校验失败。", request.getRequestURI(), e);
|
||||
return R.fail(HttpStatus.FORBIDDEN.value(), "没有访问权限,请联系管理员授权");
|
||||
}
|
||||
@@ -63,7 +63,7 @@ public class GlobalSaTokenExceptionHandler {
|
||||
* 认证异常-角色认证
|
||||
*/
|
||||
@ExceptionHandler(NotRoleException.class)
|
||||
public R handleNotRoleException(NotRoleException e, HttpServletRequest request) {
|
||||
public R<Void> handleNotRoleException(NotRoleException e, HttpServletRequest request) {
|
||||
log.error("请求地址 [{}],角色权限校验失败。", request.getRequestURI(), e);
|
||||
return R.fail(HttpStatus.FORBIDDEN.value(), "没有访问权限,请联系管理员授权");
|
||||
}
|
||||
|
@@ -24,7 +24,6 @@ import top.charles7c.continew.starter.core.constant.StringConstants;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Collection;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
@@ -138,7 +137,7 @@ public class RedisUtils {
|
||||
*/
|
||||
public static Collection<String> keys(final String keyPattern) {
|
||||
Stream<String> stream = CLIENT.getKeys().getKeysStreamByPattern(getNameMapper().map(keyPattern));
|
||||
return stream.map(key -> getNameMapper().unmap(key)).collect(Collectors.toList());
|
||||
return stream.map(key -> getNameMapper().unmap(key)).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -42,6 +42,9 @@ import top.charles7c.continew.starter.core.constant.PropertiesConstants;
|
||||
@Slf4j
|
||||
abstract class BehaviorCaptchaCacheConfiguration {
|
||||
|
||||
private BehaviorCaptchaCacheConfiguration() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义缓存实现类-Redis
|
||||
*/
|
||||
@@ -49,7 +52,8 @@ abstract class BehaviorCaptchaCacheConfiguration {
|
||||
@AutoConfigureBefore(RedissonAutoConfiguration.class)
|
||||
@ConditionalOnProperty(name = PropertiesConstants.CAPTCHA_BEHAVIOR + ".cache-type", havingValue = "redis")
|
||||
static class Redis {
|
||||
static {
|
||||
@PostConstruct
|
||||
public void postConstruct() {
|
||||
CaptchaServiceFactory.cacheService.put(StorageType.REDIS.name()
|
||||
.toLowerCase(), new BehaviorCaptchaCacheServiceImpl());
|
||||
log.debug("[ContiNew Starter] - Auto Configuration 'Behavior-CaptchaCache-Redis' completed initialization.");
|
||||
@@ -61,7 +65,6 @@ abstract class BehaviorCaptchaCacheConfiguration {
|
||||
*/
|
||||
@ConditionalOnProperty(name = PropertiesConstants.CAPTCHA_BEHAVIOR + ".cache-type", havingValue = "custom")
|
||||
static class Custom {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public CaptchaCacheService captchaCacheService(BehaviorCaptchaProperties properties) {
|
||||
|
@@ -29,6 +29,9 @@ import java.lang.reflect.Type;
|
||||
*/
|
||||
public class ClassUtils {
|
||||
|
||||
private ClassUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得给定类的所有泛型参数
|
||||
*
|
||||
|
@@ -22,7 +22,6 @@ import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 反射工具类
|
||||
@@ -45,7 +44,7 @@ public class ReflectUtils {
|
||||
*/
|
||||
public static List<String> getNonStaticFieldsName(Class<?> beanClass) throws SecurityException {
|
||||
List<Field> nonStaticFields = getNonStaticFields(beanClass);
|
||||
return nonStaticFields.stream().map(Field::getName).collect(Collectors.toList());
|
||||
return nonStaticFields.stream().map(Field::getName).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,6 +57,6 @@ public class ReflectUtils {
|
||||
*/
|
||||
public static List<Field> getNonStaticFields(Class<?> beanClass) throws SecurityException {
|
||||
Field[] fields = ReflectUtil.getFields(beanClass);
|
||||
return Arrays.stream(fields).filter(f -> !Modifier.isStatic(f.getModifiers())).collect(Collectors.toList());
|
||||
return Arrays.stream(fields).filter(f -> !Modifier.isStatic(f.getModifiers())).toList();
|
||||
}
|
||||
}
|
||||
|
@@ -98,9 +98,8 @@ public class DataPermissionHandlerImpl implements DataPermissionHandler {
|
||||
return where;
|
||||
}
|
||||
if (DataScope.DEPT_AND_CHILD.equals(dataScope)) {
|
||||
// select t1.* from table as t1 where t1.`dept_id` in (select `id` from `sys_dept` where `id` = xxx or
|
||||
// find_in_set(xxx, `ancestors`));
|
||||
// 构建子查询
|
||||
// 语句示例:select t1.* from table as t1 where t1.`dept_id` in (select `id` from `sys_dept` where `id` = xxx or find_in_set(xxx, `ancestors`));
|
||||
SubSelect subSelect = new SubSelect();
|
||||
PlainSelect select = new PlainSelect();
|
||||
select.setSelectItems(Collections.singletonList(new SelectExpressionItem(new Column(id))));
|
||||
@@ -120,21 +119,21 @@ public class DataPermissionHandlerImpl implements DataPermissionHandler {
|
||||
inExpression.setRightExpression(subSelect);
|
||||
expression = null != expression ? new OrExpression(expression, inExpression) : inExpression;
|
||||
} else if (DataScope.DEPT.equals(dataScope)) {
|
||||
// select t1.* from table as t1 where t1.`dept_id` = xxx;
|
||||
// 语句示例:select t1.* from table as t1 where t1.`dept_id` = xxx;
|
||||
EqualsTo equalsTo = new EqualsTo();
|
||||
equalsTo.setLeftExpression(this.buildColumn(tableAlias, deptId));
|
||||
equalsTo.setRightExpression(new LongValue(currentUser.getDeptId()));
|
||||
expression = null != expression ? new OrExpression(expression, equalsTo) : equalsTo;
|
||||
} else if (DataScope.SELF.equals(dataScope)) {
|
||||
// select t1.* from table as t1 where t1.`create_user` = xxx;
|
||||
// 语句示例:select t1.* from table as t1 where t1.`create_user` = xxx;
|
||||
EqualsTo equalsTo = new EqualsTo();
|
||||
equalsTo.setLeftExpression(this.buildColumn(tableAlias, dataPermission.userId()));
|
||||
equalsTo.setRightExpression(new LongValue(currentUser.getUserId()));
|
||||
expression = null != expression ? new OrExpression(expression, equalsTo) : equalsTo;
|
||||
} else if (DataScope.CUSTOM.equals(dataScope)) {
|
||||
// select t1.* from table as t1 where t1.`dept_id` in (select `dept_id` from `sys_role_dept` where
|
||||
// `role_id` = xxx);
|
||||
// 构建子查询
|
||||
// 语句示例:select t1.* from table as t1 where t1.`dept_id` in (select `dept_id` from `sys_role_dept` where
|
||||
// `role_id` = xxx);
|
||||
SubSelect subSelect = new SubSelect();
|
||||
PlainSelect select = new PlainSelect();
|
||||
select.setSelectItems(Collections.singletonList(new SelectExpressionItem(new Column(deptId))));
|
||||
|
@@ -153,7 +153,7 @@ public abstract class BaseController<S extends BaseService<L, D, Q, C>, L, D, Q,
|
||||
@Parameter(name = "ids", description = "ID 列表", example = "1,2", in = ParameterIn.PATH)
|
||||
@ResponseBody
|
||||
@DeleteMapping("/{ids}")
|
||||
public R delete(@PathVariable List<Long> ids) {
|
||||
public R<Void> delete(@PathVariable List<Long> ids) {
|
||||
this.checkPermission(Api.DELETE);
|
||||
baseService.delete(ids);
|
||||
return R.ok("删除成功");
|
||||
|
@@ -24,6 +24,9 @@ package top.charles7c.continew.starter.extension.crud.constant;
|
||||
*/
|
||||
public class ContainerPool {
|
||||
|
||||
private ContainerPool() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户昵称
|
||||
*/
|
||||
|
@@ -63,9 +63,9 @@ public class CrudRequestMappingHandlerMapping extends RequestMappingHandlerMappi
|
||||
private RequestMappingInfo getMappingForMethodWrapper(@NonNull Method method,
|
||||
@NonNull Class<?> handlerType,
|
||||
CrudRequestMapping crudRequestMapping) {
|
||||
RequestMappingInfo info = this.createRequestMappingInfo(method);
|
||||
RequestMappingInfo info = this.buildRequestMappingInfo(method);
|
||||
if (null != info) {
|
||||
RequestMappingInfo typeInfo = this.createRequestMappingInfo(handlerType);
|
||||
RequestMappingInfo typeInfo = this.buildRequestMappingInfo(handlerType);
|
||||
if (null != typeInfo) {
|
||||
info = typeInfo.combine(info);
|
||||
}
|
||||
@@ -79,11 +79,11 @@ public class CrudRequestMappingHandlerMapping extends RequestMappingHandlerMappi
|
||||
return info;
|
||||
}
|
||||
|
||||
private RequestMappingInfo createRequestMappingInfo(AnnotatedElement element) {
|
||||
private RequestMappingInfo buildRequestMappingInfo(AnnotatedElement element) {
|
||||
RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(element, RequestMapping.class);
|
||||
RequestCondition<?> condition = (element instanceof Class<?> clazz
|
||||
? getCustomTypeCondition(clazz)
|
||||
: getCustomMethodCondition((Method)element));
|
||||
return (requestMapping != null ? createRequestMappingInfo(requestMapping, condition) : null);
|
||||
return (requestMapping != null ? super.createRequestMappingInfo(requestMapping, condition) : null);
|
||||
}
|
||||
}
|
||||
|
@@ -68,9 +68,6 @@ public class PageQuery extends SortQuery {
|
||||
@Range(min = 1, max = 1000, message = "每页条数(取值范围 {min}-{max})")
|
||||
private Integer size = DEFAULT_SIZE;
|
||||
|
||||
public PageQuery() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 基于分页查询条件转换为 MyBatis Plus 分页条件
|
||||
*
|
||||
|
@@ -57,12 +57,12 @@ public final class RecordableServletHttpRequest implements RecordableHttpRequest
|
||||
return URI.create(request.getRequestURL().toString());
|
||||
}
|
||||
try {
|
||||
StringBuffer urlBuffer = this.appendQueryString(queryString);
|
||||
return new URI(urlBuffer.toString());
|
||||
StringBuilder urlBuilder = this.appendQueryString(queryString);
|
||||
return new URI(urlBuilder.toString());
|
||||
} catch (URISyntaxException e) {
|
||||
String encoded = UriUtils.encodeQuery(queryString, StandardCharsets.UTF_8);
|
||||
StringBuffer urlBuffer = this.appendQueryString(encoded);
|
||||
return URI.create(urlBuffer.toString());
|
||||
StringBuilder urlBuilder = this.appendQueryString(encoded);
|
||||
return URI.create(urlBuilder.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,7 +94,9 @@ public final class RecordableServletHttpRequest implements RecordableHttpRequest
|
||||
: Collections.unmodifiableMap(request.getParameterMap());
|
||||
}
|
||||
|
||||
private StringBuffer appendQueryString(String queryString) {
|
||||
return request.getRequestURL().append(StringConstants.QUESTION_MARK).append(queryString);
|
||||
private StringBuilder appendQueryString(String queryString) {
|
||||
return new StringBuilder().append(request.getRequestURL())
|
||||
.append(StringConstants.QUESTION_MARK)
|
||||
.append(queryString);
|
||||
}
|
||||
}
|
@@ -47,11 +47,13 @@ import top.charles7c.continew.starter.web.model.R;
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
private static final String PARAM_FAILED = "请求地址 [{}],参数验证失败。";
|
||||
|
||||
/**
|
||||
* 拦截自定义验证异常-错误请求
|
||||
*/
|
||||
@ExceptionHandler(BadRequestException.class)
|
||||
public R handleBadRequestException(BadRequestException e, HttpServletRequest request) {
|
||||
public R<Void> handleBadRequestException(BadRequestException e, HttpServletRequest request) {
|
||||
log.warn("请求地址 [{}],自定义验证失败。", request.getRequestURI(), e);
|
||||
return R.fail(HttpStatus.BAD_REQUEST.value(), e.getMessage());
|
||||
}
|
||||
@@ -60,8 +62,8 @@ public class GlobalExceptionHandler {
|
||||
* 拦截校验异常-违反约束异常
|
||||
*/
|
||||
@ExceptionHandler(ConstraintViolationException.class)
|
||||
public R constraintViolationException(ConstraintViolationException e, HttpServletRequest request) {
|
||||
log.warn("请求地址 [{}],参数验证失败。", request.getRequestURI(), e);
|
||||
public R<Void> constraintViolationException(ConstraintViolationException e, HttpServletRequest request) {
|
||||
log.warn(PARAM_FAILED, request.getRequestURI(), e);
|
||||
String errorMsg = CollUtil.join(e
|
||||
.getConstraintViolations(), StringConstants.CHINESE_COMMA, ConstraintViolation::getMessage);
|
||||
return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg);
|
||||
@@ -71,8 +73,8 @@ public class GlobalExceptionHandler {
|
||||
* 拦截校验异常-绑定异常
|
||||
*/
|
||||
@ExceptionHandler(BindException.class)
|
||||
public R handleBindException(BindException e, HttpServletRequest request) {
|
||||
log.warn("请求地址 [{}],参数验证失败。", request.getRequestURI(), e);
|
||||
public R<Void> handleBindException(BindException e, HttpServletRequest request) {
|
||||
log.warn(PARAM_FAILED, request.getRequestURI(), e);
|
||||
String errorMsg = CollUtil.join(e
|
||||
.getAllErrors(), StringConstants.CHINESE_COMMA, DefaultMessageSourceResolvable::getDefaultMessage);
|
||||
return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg);
|
||||
@@ -82,8 +84,9 @@ public class GlobalExceptionHandler {
|
||||
* 拦截校验异常-方法参数无效异常
|
||||
*/
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public R handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request) {
|
||||
log.warn("请求地址 [{}],参数验证失败。", request.getRequestURI(), e);
|
||||
public R<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e,
|
||||
HttpServletRequest request) {
|
||||
log.warn(PARAM_FAILED, request.getRequestURI(), e);
|
||||
String errorMsg = CollUtil.join(e
|
||||
.getAllErrors(), StringConstants.CHINESE_COMMA, DefaultMessageSourceResolvable::getDefaultMessage);
|
||||
return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg);
|
||||
@@ -93,8 +96,8 @@ public class GlobalExceptionHandler {
|
||||
* 拦截校验异常-方法参数类型不匹配异常
|
||||
*/
|
||||
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
|
||||
public R handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e,
|
||||
HttpServletRequest request) {
|
||||
public R<Void> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e,
|
||||
HttpServletRequest request) {
|
||||
String errorMsg = StrUtil.format("参数名:[{}],期望参数类型:[{}]", e.getName(), e.getParameter().getParameterType());
|
||||
log.warn("请求地址 [{}],参数转换失败,{}。", request.getRequestURI(), errorMsg, e);
|
||||
return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg);
|
||||
@@ -104,7 +107,7 @@ public class GlobalExceptionHandler {
|
||||
* 拦截文件上传异常-超过上传大小限制
|
||||
*/
|
||||
@ExceptionHandler(MaxUploadSizeExceededException.class)
|
||||
public R handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e, HttpServletRequest request) {
|
||||
public R<Void> handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e, HttpServletRequest request) {
|
||||
log.warn("请求地址 [{}],上传文件失败,文件大小超过限制。", request.getRequestURI(), e);
|
||||
String sizeLimit = StrUtil.subBetween(e.getMessage(), "The maximum size ", " for");
|
||||
String errorMsg = String.format("请上传小于 %sMB 的文件", NumberUtil.parseLong(sizeLimit) / 1024 / 1024);
|
||||
@@ -115,7 +118,8 @@ public class GlobalExceptionHandler {
|
||||
* 拦截校验异常-请求方式不支持异常
|
||||
*/
|
||||
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
||||
public R handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request) {
|
||||
public R<Void> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e,
|
||||
HttpServletRequest request) {
|
||||
log.error("请求地址 [{}],不支持 [{}] 请求。", request.getRequestURI(), e.getMethod());
|
||||
return R.fail(HttpStatus.METHOD_NOT_ALLOWED.value(), e.getMessage());
|
||||
}
|
||||
@@ -124,7 +128,7 @@ public class GlobalExceptionHandler {
|
||||
* 拦截业务异常
|
||||
*/
|
||||
@ExceptionHandler(BusinessException.class)
|
||||
public R handleServiceException(BusinessException e, HttpServletRequest request) {
|
||||
public R<Void> handleServiceException(BusinessException e, HttpServletRequest request) {
|
||||
log.error("请求地址 [{}],发生业务异常。", request.getRequestURI(), e);
|
||||
return R.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
|
||||
}
|
||||
@@ -133,7 +137,7 @@ public class GlobalExceptionHandler {
|
||||
* 拦截未知的运行时异常
|
||||
*/
|
||||
@ExceptionHandler(RuntimeException.class)
|
||||
public R handleRuntimeException(RuntimeException e, HttpServletRequest request) {
|
||||
public R<Void> handleRuntimeException(RuntimeException e, HttpServletRequest request) {
|
||||
log.error("请求地址 [{}],发生系统异常。", request.getRequestURI(), e);
|
||||
return R.fail(e.getMessage());
|
||||
}
|
||||
@@ -142,7 +146,7 @@ public class GlobalExceptionHandler {
|
||||
* 拦截未知的系统异常
|
||||
*/
|
||||
@ExceptionHandler(Throwable.class)
|
||||
public R handleException(Throwable e, HttpServletRequest request) {
|
||||
public R<Void> handleException(Throwable e, HttpServletRequest request) {
|
||||
log.error("请求地址 [{}],发生未知异常。", request.getRequestURI(), e);
|
||||
return R.fail(e.getMessage());
|
||||
}
|
||||
|
Reference in New Issue
Block a user