From dc31792dc1b8ad1658e49ba8fce9a1be924e6fbb Mon Sep 17 00:00:00 2001 From: wu <731351411@qq.com> Date: 星期四, 30 十一月 2023 08:44:04 +0800 Subject: [PATCH] 理片笼测试修改 --- springboot-vue3/src/main/java/com/example/springboot/advice/GlobalExceptionHandler.java | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 42 insertions(+), 0 deletions(-) diff --git a/springboot-vue3/src/main/java/com/example/springboot/advice/GlobalExceptionHandler.java b/springboot-vue3/src/main/java/com/example/springboot/advice/GlobalExceptionHandler.java new file mode 100644 index 0000000..82ace50 --- /dev/null +++ b/springboot-vue3/src/main/java/com/example/springboot/advice/GlobalExceptionHandler.java @@ -0,0 +1,42 @@ +package com.example.springboot.advice; + +import com.example.springboot.entity.vo.Result; +import lombok.extern.slf4j.Slf4j; +import org.apache.shiro.authz.AuthorizationException; +import org.apache.shiro.authz.UnauthorizedException; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; + +import javax.servlet.http.HttpServletRequest; + +/** + * 鍏ㄥ眬寮傚父 + */ +@Slf4j +@ControllerAdvice +public class GlobalExceptionHandler { + @ExceptionHandler(value = Exception.class) + @ResponseBody + public Result exception(HttpServletRequest req, Exception e) { + log.error("---exception Handler---Host {} invokes url {} ERROR: ", req.getRemoteHost(), req.getRequestURL(), e); + return Result.fail("绯荤粺閿欒锛岃鑱旂郴缃戠珯绠$悊鍛橈紒"); + } + + @ExceptionHandler(value = RuntimeException.class) + @ResponseBody + public Result runtimeException(HttpServletRequest req, RuntimeException e) { + log.error("---runtimeException Handler---Host {} invokes url {} ERROR: ", req.getRemoteHost(), req.getRequestURL(), e); + return Result.fail(e.getMessage()); + } + + /** + * 澶勭悊Shiro鏉冮檺鎷︽埅寮傚父 + */ + @ExceptionHandler(value = {AuthorizationException.class, UnauthorizedException.class}) + @ResponseBody + public Result authorizationException(HttpServletRequest req, UnauthorizedException e) { + log.error("---authorizationException Handler---Host {} invokes url {} ERROR: ", req.getRemoteHost(), req.getRequestURL(), e); + return Result.fail("鏉冮檺涓嶈冻"); + } +} \ No newline at end of file -- Gitblit v1.8.0