From f991c73f56f35665bcbe8ce2252c04ea82032b10 Mon Sep 17 00:00:00 2001 From: hjl <1657978663@qq.com> Date: 星期一, 27 五月 2024 18:06:31 +0800 Subject: [PATCH] feat: 修复登录bug;数据校验规则优化 --- ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java | 78 +++++++++++++++++++++------------------ 1 files changed, 42 insertions(+), 36 deletions(-) diff --git a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java index a9cb1b7..fa88c03 100644 --- a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java +++ b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java @@ -1,9 +1,16 @@ package com.ruoyi.common.security.handler; -import javax.naming.SizeLimitExceededException; -import javax.servlet.http.HttpServletRequest; - -import org.apache.commons.fileupload.FileUploadBase; +import com.ruoyi.common.core.constant.HttpStatus; +import com.ruoyi.common.core.domain.R; +import com.ruoyi.common.core.exception.DemoModeException; +import com.ruoyi.common.core.exception.GlobalException; +import com.ruoyi.common.core.exception.InnerAuthException; +import com.ruoyi.common.core.exception.ServiceException; +import com.ruoyi.common.core.exception.auth.NotPermissionException; +import com.ruoyi.common.core.exception.auth.NotRoleException; +import com.ruoyi.common.core.exception.user.StudyLoginException; +import com.ruoyi.common.core.utils.StringUtils; +import com.ruoyi.common.core.web.domain.AjaxResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; @@ -12,25 +19,17 @@ import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; -import com.ruoyi.common.core.constant.HttpStatus; -import com.ruoyi.common.core.exception.DemoModeException; -import com.ruoyi.common.core.exception.InnerAuthException; -import com.ruoyi.common.core.exception.ServiceException; -import com.ruoyi.common.core.exception.auth.NotPermissionException; -import com.ruoyi.common.core.exception.auth.NotRoleException; -import com.ruoyi.common.core.utils.StringUtils; -import com.ruoyi.common.core.web.domain.AjaxResult; import org.springframework.web.multipart.MaxUploadSizeExceededException; -import org.springframework.web.multipart.MultipartException; + +import javax.servlet.http.HttpServletRequest; /** * 全局异常处理器 - * + * * @author ruoyi */ @RestControllerAdvice -public class GlobalExceptionHandler -{ +public class GlobalExceptionHandler { private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); @Value("${spring.servlet.multipart.max-file-size:4MB}") @@ -43,8 +42,7 @@ * 权限码异常 */ @ExceptionHandler(NotPermissionException.class) - public AjaxResult handleNotPermissionException(NotPermissionException e, HttpServletRequest request) - { + public AjaxResult handleNotPermissionException(NotPermissionException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地址'{}',权限码校验失败'{}'", requestURI, e.getMessage()); return AjaxResult.error(HttpStatus.FORBIDDEN, "没有访问权限,请联系管理员授权"); @@ -54,8 +52,7 @@ * 角色权限异常 */ @ExceptionHandler(NotRoleException.class) - public AjaxResult handleNotRoleException(NotRoleException e, HttpServletRequest request) - { + public AjaxResult handleNotRoleException(NotRoleException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地址'{}',角色权限校验失败'{}'", requestURI, e.getMessage()); return AjaxResult.error(HttpStatus.FORBIDDEN, "没有访问权限,请联系管理员授权"); @@ -66,8 +63,7 @@ */ @ExceptionHandler(HttpRequestMethodNotSupportedException.class) public AjaxResult handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, - HttpServletRequest request) - { + HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod()); return AjaxResult.error(e.getMessage()); @@ -77,8 +73,7 @@ * 业务异常 */ @ExceptionHandler(ServiceException.class) - public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request) - { + public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request) { log.error(e.getMessage(), e); Integer code = e.getCode(); return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage()); @@ -88,8 +83,7 @@ * 拦截未知的运行时异常 */ @ExceptionHandler(RuntimeException.class) - public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request) - { + public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地址'{}',发生未知异常.", requestURI, e); return AjaxResult.error(e.getMessage()); @@ -99,8 +93,7 @@ * 系统异常 */ @ExceptionHandler(Exception.class) - public AjaxResult handleException(Exception e, HttpServletRequest request) - { + public AjaxResult handleException(Exception e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地址'{}',发生系统异常.", requestURI, e); return AjaxResult.error(e.getMessage()); @@ -110,8 +103,7 @@ * 自定义验证异常 */ @ExceptionHandler(BindException.class) - public AjaxResult handleBindException(BindException e) - { + public AjaxResult handleBindException(BindException e) { log.error(e.getMessage(), e); String message = e.getAllErrors().get(0).getDefaultMessage(); return AjaxResult.error(message); @@ -121,8 +113,7 @@ * 自定义验证异常 */ @ExceptionHandler(MethodArgumentNotValidException.class) - public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) - { + public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { log.error(e.getMessage(), e); String message = e.getBindingResult().getFieldError().getDefaultMessage(); return AjaxResult.error(message); @@ -132,8 +123,7 @@ * 内部认证异常 */ @ExceptionHandler(InnerAuthException.class) - public AjaxResult handleInnerAuthException(InnerAuthException e) - { + public AjaxResult handleInnerAuthException(InnerAuthException e) { return AjaxResult.error(e.getMessage()); } @@ -141,8 +131,7 @@ * 演示模式异常 */ @ExceptionHandler(DemoModeException.class) - public AjaxResult handleDemoModeException(DemoModeException e) - { + public AjaxResult handleDemoModeException(DemoModeException e) { return AjaxResult.error("演示模式,不允许操作"); } @@ -151,4 +140,21 @@ log.error("上传文件异常 => : {}", e.getMessage()); return AjaxResult.error("文件识别大小超出限制,允许的大小在" + maxFileSize); } + + /** + * 捕获全局自定义异常 + */ + @ExceptionHandler(GlobalException.class) + public R<String> globalExceptionHandler(GlobalException e) { + return R.fail(e.getMessage()); + } + + /** + * 学生端单点登录-异常信息 + */ + @ExceptionHandler(StudyLoginException.class) + public R<String> studyLoginExceptionHandler(StudyLoginException e) { + return R.fail(e.getCode(), e.getMessage()); + } + } -- Gitblit v1.7.1