GlobalExceptionHandler.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package com.genersoft.iot.vmp.conf;
  2. import cn.dev33.satoken.exception.*;
  3. import com.genersoft.iot.vmp.conf.exception.ControllerException;
  4. import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
  5. import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.http.HttpStatus;
  9. import org.springframework.http.ResponseEntity;
  10. import org.springframework.security.authentication.BadCredentialsException;
  11. import org.springframework.web.bind.annotation.ExceptionHandler;
  12. import org.springframework.web.bind.annotation.ResponseStatus;
  13. import org.springframework.web.bind.annotation.RestControllerAdvice;
  14. /**
  15. * 全局异常处理
  16. */
  17. @RestControllerAdvice
  18. public class GlobalExceptionHandler {
  19. private final static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
  20. /**
  21. * 默认异常处理
  22. * @param e 异常
  23. * @return 统一返回结果
  24. */
  25. @ExceptionHandler(Exception.class)
  26. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  27. public WVPResult<String> exceptionHandler(Exception e) {
  28. logger.error("[全局异常]: ", e);
  29. return WVPResult.fail(ErrorCode.ERROR500.getCode(), e.getMessage());
  30. }
  31. /**
  32. * 默认异常处理
  33. * @param e 异常
  34. * @return 统一返回结果
  35. */
  36. @ExceptionHandler(IllegalStateException.class)
  37. @ResponseStatus(HttpStatus.BAD_REQUEST)
  38. public WVPResult<String> exceptionHandler(IllegalStateException e) {
  39. return WVPResult.fail(ErrorCode.ERROR400);
  40. }
  41. /**
  42. * 自定义异常处理, 处理controller中返回的错误
  43. * @param e 异常
  44. * @return 统一返回结果
  45. */
  46. @ExceptionHandler(ControllerException.class)
  47. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  48. public ResponseEntity<WVPResult<String>> exceptionHandler(ControllerException e) {
  49. return new ResponseEntity<>(WVPResult.fail(e.getCode(), e.getMsg()), HttpStatus.OK);
  50. }
  51. /**
  52. * 登陆失败
  53. *
  54. * @param e 异常
  55. * @return 统一返回结果
  56. */
  57. @ExceptionHandler(BadCredentialsException.class)
  58. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  59. public ResponseEntity<WVPResult<String>> exceptionHandler(BadCredentialsException e) {
  60. return new ResponseEntity<>(WVPResult.fail(ErrorCode.ERROR100.getCode(), e.getMessage()), HttpStatus.OK);
  61. }
  62. /**
  63. * 全局异常处理
  64. */
  65. @RestControllerAdvice
  66. public static class AuthException {
  67. private Logger logger = LoggerFactory.getLogger(AuthException.class);
  68. // 拦截:未登录异常
  69. @ExceptionHandler(NotLoginException.class)
  70. public ResponseEntity<WVPResult> handlerException(NotLoginException e) {
  71. logger.warn("[未登录异常]: {}", e.getMessage());
  72. // 打印堆栈,以供调试
  73. // e.printStackTrace();
  74. // 构造包含401状态码和对应数据的WVPResult对象
  75. WVPResult result = WVPResult.fail(ErrorCode.ERROR401);
  76. return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(result);
  77. }
  78. // 拦截:缺少权限异常
  79. @ExceptionHandler(NotPermissionException.class)
  80. public ResponseEntity<WVPResult> handlerException(NotPermissionException e) {
  81. logger.warn("[缺少权限异常] {}", e.getMessage());
  82. // e.printStackTrace();
  83. WVPResult result = WVPResult.fail(ErrorCode.ERROR403);
  84. return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(result);
  85. }
  86. // 拦截:缺少角色异常
  87. @ExceptionHandler(NotRoleException.class)
  88. public ResponseEntity<WVPResult> handlerException(NotRoleException e) {
  89. logger.warn("[缺少角色异常] {}", e.getMessage());
  90. // e.printStackTrace();
  91. WVPResult result = WVPResult.fail(ErrorCode.ERROR403);
  92. return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(result);
  93. }
  94. // 拦截:二级认证校验失败异常
  95. @ExceptionHandler(NotSafeException.class)
  96. public ResponseEntity<WVPResult> handlerException(NotSafeException e) {
  97. logger.warn("二级认证校验失败异常");
  98. e.printStackTrace();
  99. WVPResult result = WVPResult.fail(ErrorCode.ERROR403);
  100. return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(result);
  101. }
  102. // 拦截:服务封禁异常
  103. @ExceptionHandler(DisableServiceException.class)
  104. public WVPResult handlerException(DisableServiceException e) {
  105. logger.error("当前服务已被封禁");
  106. e.printStackTrace();
  107. return WVPResult.fail(ErrorCode.ERROR403, "当前服务已被封禁");
  108. }
  109. // 拦截:Http Basic 校验失败异常
  110. @ExceptionHandler(NotBasicAuthException.class)
  111. public WVPResult handlerException(NotBasicAuthException e) {
  112. logger.error("Http Basic 校验失败异常");
  113. e.printStackTrace();
  114. return WVPResult.fail(ErrorCode.ERROR401);
  115. }
  116. // 拦截:其它所有异常
  117. @ExceptionHandler(Exception.class)
  118. public WVPResult handlerException(Exception e) {
  119. logger.error("其它所有异常");
  120. e.printStackTrace();
  121. return WVPResult.fail(ErrorCode.ERROR500);
  122. }
  123. }
  124. }