refactor(core): 移动全局异常处理器到核心模块

This commit is contained in:
2024-01-18 22:34:07 +08:00
parent 6d9e8b43eb
commit ec0ebd00e4
10 changed files with 159 additions and 59 deletions

View File

@@ -27,7 +27,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import top.charles7c.continew.starter.auth.satoken.impl.SaTokenDaoRedisImpl;
import top.charles7c.continew.starter.auth.satoken.handler.SaTokenDaoRedisImpl;
import top.charles7c.continew.starter.cache.redisson.autoconfigure.RedissonAutoConfiguration;
/**

View File

@@ -0,0 +1,70 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.continew.starter.auth.satoken.exception;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import top.charles7c.continew.starter.core.model.R;
/**
* 全局 SaToken 异常处理器
*
* @author Charles7c
* @since 1.2.0
*/
@Slf4j
@RestControllerAdvice
public class GlobalSaTokenExceptionHandler {
/**
* 认证异常-登录认证
*/
@ExceptionHandler(NotLoginException.class)
public R handleNotLoginException(NotLoginException e, HttpServletRequest request) {
log.error("请求地址 [{}],认证失败,无法访问系统资源。", request.getRequestURI(), e);
String errorMsg = switch (e.getType()) {
case NotLoginException.KICK_OUT -> "您已被踢下线。";
case NotLoginException.BE_REPLACED_MESSAGE -> "您已被顶下线。";
default -> "您的登录状态已过期,请重新登录。";
};
return R.fail(HttpStatus.UNAUTHORIZED.value(), errorMsg);
}
/**
* 认证异常-权限认证
*/
@ExceptionHandler(NotPermissionException.class)
public R handleNotPermissionException(NotPermissionException e, HttpServletRequest request) {
log.error("请求地址 [{}],权限码校验失败。", request.getRequestURI(), e);
return R.fail(HttpStatus.FORBIDDEN.value(), "没有访问权限,请联系管理员授权");
}
/**
* 认证异常-角色认证
*/
@ExceptionHandler(NotRoleException.class)
public R handleNotRoleException(NotRoleException e, HttpServletRequest request) {
log.error("请求地址 [{}],角色权限校验失败。", request.getRequestURI(), e);
return R.fail(HttpStatus.FORBIDDEN.value(), "没有访问权限,请联系管理员授权");
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package top.charles7c.continew.starter.auth.satoken.impl;
package top.charles7c.continew.starter.auth.satoken.handler;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.util.SaFoxUtil;

View File

@@ -55,6 +55,13 @@
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<!-- Swagger 注解 -->
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations-jakarta</artifactId>
<optional>true</optional>
</dependency>
<!-- 第三方封装 Ip2region离线 IP 数据管理框架和定位库支持亿级别的数据段10 微秒级别的查询性能,提供了许多主流编程语言的 xdb 数据管理引擎的实现) -->
<dependency>
<groupId>net.dreamlu</groupId>

View File

@@ -14,10 +14,10 @@
* limitations under the License.
*/
package top.charles7c.continew.starter.extension.crud.annotation;
package top.charles7c.continew.starter.core.annotation;
import org.springframework.context.annotation.Import;
import top.charles7c.continew.starter.extension.crud.autoconfigure.GlobalExceptionHandlerAutoConfiguration;
import top.charles7c.continew.starter.core.autoconfigure.GlobalExceptionHandlerAutoConfiguration;
import java.lang.annotation.*;

View File

@@ -14,16 +14,17 @@
* limitations under the License.
*/
package top.charles7c.continew.starter.extension.crud.autoconfigure;
package top.charles7c.continew.starter.core.autoconfigure;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import top.charles7c.continew.starter.extension.crud.handler.GlobalErrorHandler;
import top.charles7c.continew.starter.extension.crud.handler.GlobalExceptionHandler;
import top.charles7c.continew.starter.core.exception.GlobalErrorHandler;
import top.charles7c.continew.starter.core.exception.GlobalExceptionHandler;
/**
* 全局异常处理器自动配置
@@ -35,6 +36,7 @@ import top.charles7c.continew.starter.extension.crud.handler.GlobalExceptionHand
@Configuration(proxyBeanMethods = false)
@Import({GlobalExceptionHandler.class, GlobalErrorHandler.class})
@ConditionalOnMissingBean(BasicErrorController.class)
@ComponentScan("top.charles7c.continew.starter.**.exception")
public class GlobalExceptionHandlerAutoConfiguration {
@PostConstruct

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package top.charles7c.continew.starter.extension.crud.handler;
package top.charles7c.continew.starter.core.exception;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
@@ -32,7 +32,7 @@ import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import top.charles7c.continew.starter.extension.crud.model.resp.R;
import top.charles7c.continew.starter.core.model.R;
import java.io.IOException;
import java.util.List;

View File

@@ -14,11 +14,8 @@
* limitations under the License.
*/
package top.charles7c.continew.starter.extension.crud.handler;
package top.charles7c.continew.starter.core.exception;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
@@ -36,10 +33,8 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import top.charles7c.continew.starter.core.constant.StringConstants;
import top.charles7c.continew.starter.core.exception.BadRequestException;
import top.charles7c.continew.starter.core.exception.BusinessException;
import top.charles7c.continew.starter.core.model.R;
import top.charles7c.continew.starter.core.util.ExceptionUtils;
import top.charles7c.continew.starter.extension.crud.model.resp.R;
import java.util.Objects;
@@ -117,38 +112,6 @@ public class GlobalExceptionHandler {
return R.fail(HttpStatus.BAD_REQUEST.value(), errorMsg);
}
/**
* 认证异常-登录认证
*/
@ExceptionHandler(NotLoginException.class)
public R handleNotLoginException(NotLoginException e, HttpServletRequest request) {
log.error("请求地址 [{}],认证失败,无法访问系统资源。", request.getRequestURI(), e);
String errorMsg = switch (e.getType()) {
case NotLoginException.KICK_OUT -> "您已被踢下线。";
case NotLoginException.BE_REPLACED_MESSAGE -> "您已被顶下线。";
default -> "您的登录状态已过期,请重新登录。";
};
return R.fail(HttpStatus.UNAUTHORIZED.value(), errorMsg);
}
/**
* 认证异常-权限认证
*/
@ExceptionHandler(NotPermissionException.class)
public R handleNotPermissionException(NotPermissionException e, HttpServletRequest request) {
log.error("请求地址 [{}],权限码校验失败。", request.getRequestURI(), e);
return R.fail(HttpStatus.FORBIDDEN.value(), "没有访问权限,请联系管理员授权");
}
/**
* 认证异常-角色认证
*/
@ExceptionHandler(NotRoleException.class)
public R handleNotRoleException(NotRoleException e, HttpServletRequest request) {
log.error("请求地址 [{}],角色权限校验失败。", request.getRequestURI(), e);
return R.fail(HttpStatus.FORBIDDEN.value(), "没有访问权限,请联系管理员授权");
}
/**
* 拦截校验异常-请求方式不支持异常
*/

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package top.charles7c.continew.starter.extension.crud.model.resp;
package top.charles7c.continew.starter.core.model;
import cn.hutool.core.date.DateUtil;
import io.swagger.v3.oas.annotations.media.Schema;
@@ -40,6 +40,9 @@ public class R<T> implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private static final int SUCCESS_CODE = HttpStatus.OK.value();
private static final int FAIL_CODE = HttpStatus.INTERNAL_SERVER_ERROR.value();
/**
* 是否成功
*/
@@ -70,15 +73,6 @@ public class R<T> implements Serializable {
@Schema(description = "时间戳", example = "1691453288")
private long timestamp = DateUtil.currentSeconds();
/**
* 成功状态码
*/
private static final int SUCCESS_CODE = HttpStatus.OK.value();
/**
* 失败状态码
*/
private static final int FAIL_CODE = HttpStatus.INTERNAL_SERVER_ERROR.value();
private R(boolean success, int code, String msg, T data) {
this.success = success;
this.code = code;
@@ -86,38 +80,102 @@ public class R<T> implements Serializable {
this.data = data;
}
/**
* 操作成功
*
* @param <T> 响应数据类型
* @return R /
*/
public static <T> R<T> ok() {
return new R<>(true, SUCCESS_CODE, "操作成功", null);
}
/**
* 操作成功
*
* @param data 响应数据
* @param <T> 响应数据类型
* @return R /
*/
public static <T> R<T> ok(T data) {
return new R<>(true, SUCCESS_CODE, "操作成功", data);
}
/**
* 操作成功
*
* @param msg 业务状态信息
* @param <T> 响应数据类型
* @return R /
*/
public static <T> R<T> ok(String msg) {
return new R<>(true, SUCCESS_CODE, msg, null);
}
/**
* 操作成功
*
* @param msg 业务状态信息
* @param data 响应数据
* @param <T> 响应数据类型
* @return R /
*/
public static <T> R<T> ok(String msg, T data) {
return new R<>(true, SUCCESS_CODE, msg, data);
}
/**
* 操作失败
*
* @param <T> 响应数据类型
* @return R /
*/
public static <T> R<T> fail() {
return new R<>(false, FAIL_CODE, "操作失败", null);
}
/**
* 操作失败
*
* @param msg 业务状态信息
* @param <T> 响应数据类型
* @return R /
*/
public static <T> R<T> fail(String msg) {
return new R<>(false, FAIL_CODE, msg, null);
}
/**
* 操作失败
*
* @param data 响应数据
* @param <T> 响应数据类型
* @return R /
*/
public static <T> R<T> fail(T data) {
return new R<>(false, FAIL_CODE, "操作失败", data);
}
/**
* 操作失败
*
* @param msg 业务状态信息
* @param data 响应数据
* @param <T> 响应数据类型
* @return R /
*/
public static <T> R<T> fail(String msg, T data) {
return new R<>(false, FAIL_CODE, msg, data);
}
/**
* 操作失败
*
* @param code 业务状态码
* @param msg 业务状态信息
* @param <T> 响应数据类型
* @return R /
*/
public static <T> R<T> fail(int code, String msg) {
return new R<>(false, code, msg, null);
}

View File

@@ -28,12 +28,12 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.charles7c.continew.starter.core.constant.StringConstants;
import top.charles7c.continew.starter.core.model.R;
import top.charles7c.continew.starter.extension.crud.annotation.CrudRequestMapping;
import top.charles7c.continew.starter.extension.crud.enums.Api;
import top.charles7c.continew.starter.extension.crud.model.query.PageQuery;
import top.charles7c.continew.starter.extension.crud.model.query.SortQuery;
import top.charles7c.continew.starter.extension.crud.model.resp.PageResp;
import top.charles7c.continew.starter.extension.crud.model.resp.R;
import java.util.List;