From df9cc881f10af931bb62443ba8022d6ce61d4478 Mon Sep 17 00:00:00 2001 From: 疯狂的狮子Li <15040126243@163.com> Date: 星期四, 05 九月 2024 18:50:00 +0800 Subject: [PATCH] update 优化 连接SSE token过期导致的 Servlet异常 --- ruoyi-common/ruoyi-common-web/src/main/java/org/dromara/common/web/handler/GlobalExceptionHandler.java | 35 +++++++++++++++++++++++++++++++++++ 1 files changed, 35 insertions(+), 0 deletions(-) diff --git a/ruoyi-common/ruoyi-common-web/src/main/java/org/dromara/common/web/handler/GlobalExceptionHandler.java b/ruoyi-common/ruoyi-common-web/src/main/java/org/dromara/common/web/handler/GlobalExceptionHandler.java index bd47c18..bd801ba 100644 --- a/ruoyi-common/ruoyi-common-web/src/main/java/org/dromara/common/web/handler/GlobalExceptionHandler.java +++ b/ruoyi-common/ruoyi-common-web/src/main/java/org/dromara/common/web/handler/GlobalExceptionHandler.java @@ -2,6 +2,7 @@ import cn.hutool.core.util.ObjectUtil; import cn.hutool.http.HttpStatus; +import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.ConstraintViolation; import jakarta.validation.ConstraintViolationException; @@ -10,15 +11,19 @@ import org.dromara.common.core.exception.ServiceException; import org.dromara.common.core.exception.base.BaseException; import org.dromara.common.core.utils.StreamUtils; +import org.dromara.common.core.utils.StringUtils; import org.springframework.context.support.DefaultMessageSourceResolvable; import org.springframework.validation.BindException; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MissingPathVariableException; import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; import org.springframework.web.servlet.NoHandlerFoundException; + +import java.io.IOException; /** * 鍏ㄥ眬寮傚父澶勭悊鍣� @@ -48,6 +53,22 @@ log.error(e.getMessage()); Integer code = e.getCode(); return ObjectUtil.isNotNull(code) ? R.fail(code, e.getMessage()) : R.fail(e.getMessage()); + } + + /** + * servlet寮傚父 + */ + @ExceptionHandler(ServletException.class) + public R<Void> handleServletException(ServletException e, HttpServletRequest request) { + if (StringUtils.contains(e.getMessage(), "NotLoginException")) { + String requestURI = request.getRequestURI(); + log.error("璇锋眰鍦板潃'{}',璁よ瘉澶辫触'{}',鏃犳硶璁块棶绯荤粺璧勬簮", requestURI, e.getMessage()); + return R.fail(HttpStatus.HTTP_UNAUTHORIZED, "璁よ瘉澶辫触锛屾棤娉曡闂郴缁熻祫婧�"); + } else { + String requestURI = request.getRequestURI(); + log.error("璇锋眰鍦板潃'{}',鍙戠敓鏈煡寮傚父.", requestURI, e); + return R.fail(e.getMessage()); + } } /** @@ -92,6 +113,20 @@ /** * 鎷︽埅鏈煡鐨勮繍琛屾椂寮傚父 */ + @ResponseStatus(org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR) + @ExceptionHandler(IOException.class) + public void handleRuntimeException(IOException e, HttpServletRequest request) { + String requestURI = request.getRequestURI(); + if (requestURI.contains("sse")) { + // sse 缁忓父鎬ц繛鎺ヤ腑鏂� 渚嬪鍏抽棴娴忚鍣� 鐩存帴灞忚斀 + return; + } + log.error("璇锋眰鍦板潃'{}',杩炴帴涓柇", requestURI, e); + } + + /** + * 鎷︽埅鏈煡鐨勮繍琛屾椂寮傚父 + */ @ExceptionHandler(RuntimeException.class) public R<Void> handleRuntimeException(RuntimeException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); -- Gitblit v1.9.3