From 553c29ab8a46c9a07e3657fdd36a81a6bfde1afe Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期四, 23 九月 2021 19:13:58 +0800 Subject: [PATCH] update 整合 satoken 权限、鉴权一体化框架 --- ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/GlobalExceptionHandler.java | 35 +++++++++++++++++++++++++++++------ 1 files changed, 29 insertions(+), 6 deletions(-) diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/GlobalExceptionHandler.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/GlobalExceptionHandler.java index 4df3265..a96450c 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/GlobalExceptionHandler.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/GlobalExceptionHandler.java @@ -1,21 +1,22 @@ package com.ruoyi.framework.web.exception; +import cn.dev33.satoken.exception.NotLoginException; +import cn.dev33.satoken.exception.NotPermissionException; +import cn.dev33.satoken.exception.NotRoleException; import cn.hutool.http.HttpStatus; import com.ruoyi.common.core.domain.AjaxResult; -import com.ruoyi.common.exception.BaseException; -import com.ruoyi.common.exception.CustomException; import com.ruoyi.common.exception.DemoModeException; +import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.security.access.AccessDeniedException; import org.springframework.validation.BindException; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; -import org.springframework.web.servlet.NoHandlerFoundException; +import javax.servlet.http.HttpServletRequest; import javax.validation.ConstraintViolationException; /** @@ -31,8 +32,8 @@ /** * 鏉冮檺鏍¢獙寮傚父 */ - @ExceptionHandler(AccessDeniedException.class) - public AjaxResult handleAccessDeniedException(AccessDeniedException e, HttpServletRequest request) + @ExceptionHandler(NotPermissionException.class) + public AjaxResult handleAccessDeniedException(NotPermissionException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("璇锋眰鍦板潃'{}',鏉冮檺鏍¢獙澶辫触'{}'", requestURI, e.getMessage()); @@ -40,6 +41,28 @@ } /** + * 瑙掕壊鏍¢獙寮傚父 + */ + @ExceptionHandler(NotRoleException.class) + public AjaxResult handleAccessDeniedException(NotRoleException e, HttpServletRequest request) + { + String requestURI = request.getRequestURI(); + log.error("璇锋眰鍦板潃'{}',瑙掕壊鏍¢獙澶辫触'{}'", requestURI, e.getMessage()); + return AjaxResult.error(HttpStatus.HTTP_FORBIDDEN, "娌℃湁瑙掕壊锛岃鑱旂郴绠$悊鍛樻巿鏉�"); + } + + /** + * 璁よ瘉澶辫触 + */ + @ExceptionHandler(NotLoginException.class) + public AjaxResult handleAccessDeniedException(NotLoginException e, HttpServletRequest request) + { + String requestURI = request.getRequestURI(); + log.error("璇锋眰璁块棶锛歿}锛岃璇佸け璐ワ紝鏃犳硶璁块棶绯荤粺璧勬簮", requestURI, e.getMessage()); + return AjaxResult.error(HttpStatus.HTTP_UNAUTHORIZED, StringUtils.format("璇锋眰璁块棶锛歿}锛岃璇佸け璐ワ紝鏃犳硶璁块棶绯荤粺璧勬簮", requestURI)); + } + + /** * 璇锋眰鏂瑰紡涓嶆敮鎸� */ @ExceptionHandler(HttpRequestMethodNotSupportedException.class) -- Gitblit v1.9.3