123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- package com.genersoft.iot.vmp.conf;
- import cn.dev33.satoken.exception.*;
- import com.genersoft.iot.vmp.conf.exception.ControllerException;
- import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
- import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.ResponseEntity;
- import org.springframework.security.authentication.BadCredentialsException;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.ResponseStatus;
- import org.springframework.web.bind.annotation.RestControllerAdvice;
- /**
- * 全局异常处理
- */
- @RestControllerAdvice
- public class GlobalExceptionHandler {
- private final static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
- /**
- * 默认异常处理
- * @param e 异常
- * @return 统一返回结果
- */
- @ExceptionHandler(Exception.class)
- @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
- public WVPResult<String> exceptionHandler(Exception e) {
- logger.error("[全局异常]: ", e);
- return WVPResult.fail(ErrorCode.ERROR500.getCode(), e.getMessage());
- }
- /**
- * 默认异常处理
- * @param e 异常
- * @return 统一返回结果
- */
- @ExceptionHandler(IllegalStateException.class)
- @ResponseStatus(HttpStatus.BAD_REQUEST)
- public WVPResult<String> exceptionHandler(IllegalStateException e) {
- return WVPResult.fail(ErrorCode.ERROR400);
- }
- /**
- * 自定义异常处理, 处理controller中返回的错误
- * @param e 异常
- * @return 统一返回结果
- */
- @ExceptionHandler(ControllerException.class)
- @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
- public ResponseEntity<WVPResult<String>> exceptionHandler(ControllerException e) {
- return new ResponseEntity<>(WVPResult.fail(e.getCode(), e.getMsg()), HttpStatus.OK);
- }
- /**
- * 登陆失败
- *
- * @param e 异常
- * @return 统一返回结果
- */
- @ExceptionHandler(BadCredentialsException.class)
- @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
- public ResponseEntity<WVPResult<String>> exceptionHandler(BadCredentialsException e) {
- return new ResponseEntity<>(WVPResult.fail(ErrorCode.ERROR100.getCode(), e.getMessage()), HttpStatus.OK);
- }
- /**
- * 全局异常处理
- */
- @RestControllerAdvice
- public static class AuthException {
- private Logger logger = LoggerFactory.getLogger(AuthException.class);
- // 拦截:未登录异常
- @ExceptionHandler(NotLoginException.class)
- public ResponseEntity<WVPResult> handlerException(NotLoginException e) {
- logger.warn("[未登录异常]: {}", e.getMessage());
- // 打印堆栈,以供调试
- // e.printStackTrace();
- // 构造包含401状态码和对应数据的WVPResult对象
- WVPResult result = WVPResult.fail(ErrorCode.ERROR401);
- return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(result);
- }
- // 拦截:缺少权限异常
- @ExceptionHandler(NotPermissionException.class)
- public ResponseEntity<WVPResult> handlerException(NotPermissionException e) {
- logger.warn("[缺少权限异常] {}", e.getMessage());
- // e.printStackTrace();
- WVPResult result = WVPResult.fail(ErrorCode.ERROR403);
- return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(result);
- }
- // 拦截:缺少角色异常
- @ExceptionHandler(NotRoleException.class)
- public ResponseEntity<WVPResult> handlerException(NotRoleException e) {
- logger.warn("[缺少角色异常] {}", e.getMessage());
- // e.printStackTrace();
- WVPResult result = WVPResult.fail(ErrorCode.ERROR403);
- return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(result);
- }
- // 拦截:二级认证校验失败异常
- @ExceptionHandler(NotSafeException.class)
- public ResponseEntity<WVPResult> handlerException(NotSafeException e) {
- logger.warn("二级认证校验失败异常");
- e.printStackTrace();
- WVPResult result = WVPResult.fail(ErrorCode.ERROR403);
- return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(result);
- }
- // 拦截:服务封禁异常
- @ExceptionHandler(DisableServiceException.class)
- public WVPResult handlerException(DisableServiceException e) {
- logger.error("当前服务已被封禁");
- e.printStackTrace();
- return WVPResult.fail(ErrorCode.ERROR403, "当前服务已被封禁");
- }
- // 拦截:Http Basic 校验失败异常
- @ExceptionHandler(NotBasicAuthException.class)
- public WVPResult handlerException(NotBasicAuthException e) {
- logger.error("Http Basic 校验失败异常");
- e.printStackTrace();
- return WVPResult.fail(ErrorCode.ERROR401);
- }
- // 拦截:其它所有异常
- @ExceptionHandler(Exception.class)
- public WVPResult handlerException(Exception e) {
- logger.error("其它所有异常");
- e.printStackTrace();
- return WVPResult.fail(ErrorCode.ERROR500);
- }
- }
- }
|