-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: continew-starter 2.4.0 => 2.5.0
1.continew-starter-log-httptrace-pro => continew-starter-log-interceptor 2.移除 WebMvcConfiguration 配置(已迁移到 Starter 项目) 3.Starter 全局响应(新)适配,自定义异常拦截调整到 Admin 项目 4.部分 API 调整
- Loading branch information
Showing
30 changed files
with
331 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 0 additions & 62 deletions
62
...inew-admin-common/src/main/java/top/continew/admin/common/config/WebMvcConfiguration.java
This file was deleted.
Oops, something went wrong.
87 changes: 87 additions & 0 deletions
87
...mmon/src/main/java/top/continew/admin/common/config/exception/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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.continew.admin.common.config.exception; | ||
|
||
import cn.hutool.core.text.CharSequenceUtil; | ||
import cn.hutool.core.util.NumberUtil; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.multipart.MultipartException; | ||
import top.continew.starter.core.exception.BadRequestException; | ||
import top.continew.starter.core.exception.BusinessException; | ||
import top.continew.starter.web.model.R; | ||
|
||
/** | ||
* 全局异常处理器 | ||
* | ||
* @author Charles7c | ||
* @since 2024/8/7 20:21 | ||
*/ | ||
@Slf4j | ||
@Order(99) | ||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
/** | ||
* 拦截业务异常 | ||
*/ | ||
@ExceptionHandler(BusinessException.class) | ||
public R handleBusinessException(BusinessException e, HttpServletRequest request) { | ||
log.error("请求地址 [{}],发生业务异常。", request.getRequestURI(), e); | ||
return R.fail(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()), e.getMessage()); | ||
} | ||
|
||
/** | ||
* 拦截自定义验证异常-错误请求 | ||
*/ | ||
@ExceptionHandler(BadRequestException.class) | ||
public R handleBadRequestException(BadRequestException e, HttpServletRequest request) { | ||
log.warn("请求地址 [{}],自定义验证失败。", request.getRequestURI(), e); | ||
return R.fail(String.valueOf(HttpStatus.BAD_REQUEST.value()), e.getMessage()); | ||
} | ||
|
||
/** | ||
* 拦截文件上传异常-超过上传大小限制 | ||
*/ | ||
@ExceptionHandler(MultipartException.class) | ||
public R handleRequestTooBigException(MultipartException e, HttpServletRequest request) { | ||
String msg = e.getMessage(); | ||
R defaultFail = R.fail(String.valueOf(HttpStatus.BAD_REQUEST.value()), msg); | ||
if (CharSequenceUtil.isBlank(msg)) { | ||
return defaultFail; | ||
} | ||
String sizeLimit; | ||
Throwable cause = e.getCause(); | ||
if (null != cause) { | ||
msg = msg.concat(cause.getMessage().toLowerCase()); | ||
} | ||
if (msg.contains("size") && msg.contains("exceed")) { | ||
sizeLimit = CharSequenceUtil.subBetween(msg, "the maximum size ", " for"); | ||
} else if (msg.contains("larger than")) { | ||
sizeLimit = CharSequenceUtil.subAfter(msg, "larger than ", true); | ||
} else { | ||
return defaultFail; | ||
} | ||
String errorMsg = "请上传小于 %sKB 的文件".formatted(NumberUtil.parseLong(sizeLimit) / 1024); | ||
log.warn("请求地址 [{}],上传文件失败,文件大小超过限制。", request.getRequestURI(), e); | ||
return R.fail(String.valueOf(HttpStatus.BAD_REQUEST.value()), errorMsg); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...c/main/java/top/continew/admin/common/config/exception/GlobalSaTokenExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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.continew.admin.common.config.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.core.annotation.Order; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import top.continew.starter.web.model.R; | ||
|
||
/** | ||
* 全局 SaToken 异常处理器 | ||
* | ||
* @author Charles7c | ||
* @since 2024/8/7 20:21 | ||
*/ | ||
@Slf4j | ||
@Order(99) | ||
@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(String.valueOf(HttpStatus.UNAUTHORIZED.value()), errorMsg); | ||
} | ||
|
||
/** | ||
* 认证异常-权限认证 | ||
*/ | ||
@ExceptionHandler(NotPermissionException.class) | ||
public R handleNotPermissionException(NotPermissionException e, HttpServletRequest request) { | ||
log.error("请求地址 [{}],权限码校验失败。", request.getRequestURI(), e); | ||
return R.fail(String.valueOf(HttpStatus.FORBIDDEN.value()), "没有访问权限,请联系管理员授权"); | ||
} | ||
|
||
/** | ||
* 认证异常-角色认证 | ||
*/ | ||
@ExceptionHandler(NotRoleException.class) | ||
public R handleNotRoleException(NotRoleException e, HttpServletRequest request) { | ||
log.error("请求地址 [{}],角色权限校验失败。", request.getRequestURI(), e); | ||
return R.fail(String.valueOf(HttpStatus.FORBIDDEN.value()), "没有访问权限,请联系管理员授权"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.