From b874ca0782eb116bdedfc08023959a977f170a94 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Sun, 10 Sep 2023 11:17:47 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=B0=81=E8=A3=85=20Spring=20Boot?= =?UTF-8?q?=20=E9=BB=98=E8=AE=A4=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.排除路径配置放开 /error,以防止出现默认错误处理却显示为 401 的问题(例如:404 被识别为 401) 2.封装 Spring Boot 默认错误处理,统一响应结构 --- .../common/handler/GlobalErrorHandler.java | 94 +++++++++++++++++++ .../handler/GlobalExceptionHandler.java | 2 +- .../main/resources/config/application-dev.yml | 1 + .../resources/config/application-prod.yml | 1 + 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 continew-admin-common/src/main/java/top/charles7c/cnadmin/common/handler/GlobalErrorHandler.java diff --git a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/handler/GlobalErrorHandler.java b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/handler/GlobalErrorHandler.java new file mode 100644 index 00000000..ab4878d0 --- /dev/null +++ b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/handler/GlobalErrorHandler.java @@ -0,0 +1,94 @@ +/* + * 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.charles7c.cnadmin.common.handler; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import lombok.extern.slf4j.Slf4j; + +import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController; +import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver; +import org.springframework.boot.web.servlet.error.ErrorAttributes; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.ModelAndView; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.json.JSONUtil; + +import top.charles7c.cnadmin.common.model.vo.R; + +/** + * 全局错误处理器 + * + * @author Charles7c + * @since 2023/9/10 9:47 + */ +@Slf4j +@RestController +public class GlobalErrorHandler extends BasicErrorController { + + @Resource + private ObjectMapper objectMapper; + + public GlobalErrorHandler(ErrorAttributes errorAttributes, ServerProperties serverProperties, + List errorViewResolvers) { + super(errorAttributes, serverProperties.getError(), errorViewResolvers); + } + + @Override + public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) { + Map errorAttributeMap = + super.getErrorAttributes(request, super.getErrorAttributeOptions(request, MediaType.TEXT_HTML)); + String path = (String)errorAttributeMap.get("path"); + HttpStatus status = super.getStatus(request); + R result = R.fail(status.value(), (String)errorAttributeMap.get("error")); + result.setData(path); + try { + response.setStatus(status.value()); + response.setContentType(MediaType.APPLICATION_JSON_VALUE); + objectMapper.writeValue(response.getWriter(), result); + } catch (IOException e) { + log.error("请求地址 [{}],发生 IO 异常。", path, e); + } + log.error("请求地址 [{}],发生异常,错误信息:{}。", path, JSONUtil.toJsonStr(errorAttributeMap)); + return null; + } + + @Override + public ResponseEntity> error(HttpServletRequest request) { + Map errorAttributeMap = + super.getErrorAttributes(request, super.getErrorAttributeOptions(request, MediaType.ALL)); + String path = (String)errorAttributeMap.get("path"); + HttpStatus status = super.getStatus(request); + R result = R.fail(status.value(), (String)errorAttributeMap.get("error")); + result.setData(path); + log.error("请求地址 [{}],发生异常,错误信息:{}。", path, JSONUtil.toJsonStr(errorAttributeMap)); + return new ResponseEntity<>(BeanUtil.beanToMap(result), status); + } +} diff --git a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/handler/GlobalExceptionHandler.java b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/handler/GlobalExceptionHandler.java index 2b39425e..b5042b3a 100644 --- a/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/handler/GlobalExceptionHandler.java +++ b/continew-admin-common/src/main/java/top/charles7c/cnadmin/common/handler/GlobalExceptionHandler.java @@ -183,7 +183,7 @@ public class GlobalExceptionHandler { @ExceptionHandler(HttpRequestMethodNotSupportedException.class) public R handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request) { LogContextHolder.setErrorMsg(e.getMessage()); - log.error("请求地址 [{}],不支持 [{}] 请求", request.getRequestURI(), e.getMethod()); + log.error("请求地址 [{}],不支持 [{}] 请求。", request.getRequestURI(), e.getMethod()); return R.fail(HttpStatus.METHOD_NOT_ALLOWED.value(), e.getMessage()); } diff --git a/continew-admin-webapi/src/main/resources/config/application-dev.yml b/continew-admin-webapi/src/main/resources/config/application-dev.yml index 01769a34..5d07349f 100644 --- a/continew-admin-webapi/src/main/resources/config/application-dev.yml +++ b/continew-admin-webapi/src/main/resources/config/application-dev.yml @@ -116,6 +116,7 @@ captcha: --- ### 安全配置-排除路径配置 security.excludes: + - /error # 静态资源 - /*.html - /**/*.html diff --git a/continew-admin-webapi/src/main/resources/config/application-prod.yml b/continew-admin-webapi/src/main/resources/config/application-prod.yml index 8b475148..b5a07114 100644 --- a/continew-admin-webapi/src/main/resources/config/application-prod.yml +++ b/continew-admin-webapi/src/main/resources/config/application-prod.yml @@ -116,6 +116,7 @@ captcha: --- ### 安全配置-排除路径配置 security.excludes: + - /error # 静态资源 - /*.html - /**/*.html