diff --git a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenDaoConfiguration.java b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenDaoConfiguration.java index 4a41672c..e71ac713 100644 --- a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenDaoConfiguration.java +++ b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/autoconfigure/SaTokenDaoConfiguration.java @@ -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; /** diff --git a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/exception/GlobalSaTokenExceptionHandler.java b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/exception/GlobalSaTokenExceptionHandler.java new file mode 100644 index 00000000..53a319e3 --- /dev/null +++ b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/exception/GlobalSaTokenExceptionHandler.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * 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 + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * 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(), "没有访问权限,请联系管理员授权"); + } +} \ No newline at end of file diff --git a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/impl/SaTokenDaoRedisImpl.java b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/handler/SaTokenDaoRedisImpl.java similarity index 98% rename from continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/impl/SaTokenDaoRedisImpl.java rename to continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/handler/SaTokenDaoRedisImpl.java index 9c49c3e1..3e608e7f 100644 --- a/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/impl/SaTokenDaoRedisImpl.java +++ b/continew-starter-auth/continew-starter-auth-satoken/src/main/java/top/charles7c/continew/starter/auth/satoken/handler/SaTokenDaoRedisImpl.java @@ -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; diff --git a/continew-starter-core/pom.xml b/continew-starter-core/pom.xml index 2ceac6ee..d120c864 100644 --- a/continew-starter-core/pom.xml +++ b/continew-starter-core/pom.xml @@ -55,6 +55,13 @@ spring-boot-configuration-processor + + + io.swagger.core.v3 + swagger-annotations-jakarta + true + + net.dreamlu diff --git a/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/annotation/EnableGlobalExceptionHandler.java b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/annotation/EnableGlobalExceptionHandler.java similarity index 85% rename from continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/annotation/EnableGlobalExceptionHandler.java rename to continew-starter-core/src/main/java/top/charles7c/continew/starter/core/annotation/EnableGlobalExceptionHandler.java index 2c7dcea0..09d981ff 100644 --- a/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/annotation/EnableGlobalExceptionHandler.java +++ b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/annotation/EnableGlobalExceptionHandler.java @@ -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.*; diff --git a/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/autoconfigure/GlobalExceptionHandlerAutoConfiguration.java b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/GlobalExceptionHandlerAutoConfiguration.java similarity index 81% rename from continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/autoconfigure/GlobalExceptionHandlerAutoConfiguration.java rename to continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/GlobalExceptionHandlerAutoConfiguration.java index 0e38cb2d..0077d138 100644 --- a/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/autoconfigure/GlobalExceptionHandlerAutoConfiguration.java +++ b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/GlobalExceptionHandlerAutoConfiguration.java @@ -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 diff --git a/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/handler/GlobalErrorHandler.java b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/exception/GlobalErrorHandler.java similarity index 96% rename from continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/handler/GlobalErrorHandler.java rename to continew-starter-core/src/main/java/top/charles7c/continew/starter/core/exception/GlobalErrorHandler.java index 25450317..1a9824b7 100644 --- a/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/handler/GlobalErrorHandler.java +++ b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/exception/GlobalErrorHandler.java @@ -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; diff --git a/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/handler/GlobalExceptionHandler.java b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/exception/GlobalExceptionHandler.java similarity index 77% rename from continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/handler/GlobalExceptionHandler.java rename to continew-starter-core/src/main/java/top/charles7c/continew/starter/core/exception/GlobalExceptionHandler.java index 3a727e57..8ba15470 100644 --- a/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/handler/GlobalExceptionHandler.java +++ b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/exception/GlobalExceptionHandler.java @@ -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(), "没有访问权限,请联系管理员授权"); - } - /** * 拦截校验异常-请求方式不支持异常 */ diff --git a/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/model/resp/R.java b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/model/R.java similarity index 71% rename from continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/model/resp/R.java rename to continew-starter-core/src/main/java/top/charles7c/continew/starter/core/model/R.java index 62b3020f..4aac70b2 100644 --- a/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/model/resp/R.java +++ b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/model/R.java @@ -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 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 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 implements Serializable { this.data = data; } + /** + * 操作成功 + * + * @param 响应数据类型 + * @return R / + */ public static R ok() { return new R<>(true, SUCCESS_CODE, "操作成功", null); } + /** + * 操作成功 + * + * @param data 响应数据 + * @param 响应数据类型 + * @return R / + */ public static R ok(T data) { return new R<>(true, SUCCESS_CODE, "操作成功", data); } + /** + * 操作成功 + * + * @param msg 业务状态信息 + * @param 响应数据类型 + * @return R / + */ public static R ok(String msg) { return new R<>(true, SUCCESS_CODE, msg, null); } + /** + * 操作成功 + * + * @param msg 业务状态信息 + * @param data 响应数据 + * @param 响应数据类型 + * @return R / + */ public static R ok(String msg, T data) { return new R<>(true, SUCCESS_CODE, msg, data); } + /** + * 操作失败 + * + * @param 响应数据类型 + * @return R / + */ public static R fail() { return new R<>(false, FAIL_CODE, "操作失败", null); } + /** + * 操作失败 + * + * @param msg 业务状态信息 + * @param 响应数据类型 + * @return R / + */ public static R fail(String msg) { return new R<>(false, FAIL_CODE, msg, null); } + /** + * 操作失败 + * + * @param data 响应数据 + * @param 响应数据类型 + * @return R / + */ public static R fail(T data) { return new R<>(false, FAIL_CODE, "操作失败", data); } + /** + * 操作失败 + * + * @param msg 业务状态信息 + * @param data 响应数据 + * @param 响应数据类型 + * @return R / + */ public static R fail(String msg, T data) { return new R<>(false, FAIL_CODE, msg, data); } + /** + * 操作失败 + * + * @param code 业务状态码 + * @param msg 业务状态信息 + * @param 响应数据类型 + * @return R / + */ public static R fail(int code, String msg) { return new R<>(false, code, msg, null); } diff --git a/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/base/BaseController.java b/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/base/BaseController.java index c1914360..87031416 100644 --- a/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/base/BaseController.java +++ b/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/base/BaseController.java @@ -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;