StpInException.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package com.sckj.front.config.stp;
  2. import cn.dev33.satoken.exception.NotLoginException;
  3. import cn.dev33.satoken.exception.NotPermissionException;
  4. import com.sckj.common.core.AjaxResult;
  5. import com.sckj.common.enums.ErrorEnum;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.springframework.http.HttpStatus;
  8. import org.springframework.web.bind.annotation.ControllerAdvice;
  9. import org.springframework.web.bind.annotation.ExceptionHandler;
  10. import org.springframework.web.bind.annotation.ResponseBody;
  11. import org.springframework.web.bind.annotation.ResponseStatus;
  12. /**
  13. * Sa-Token的异常拦截
  14. */
  15. @Slf4j
  16. @ControllerAdvice
  17. public class StpInException {
  18. /**
  19. * 拦截登录异常
  20. */
  21. @ResponseStatus(HttpStatus.OK)
  22. @ExceptionHandler(NotLoginException.class)
  23. @ResponseBody
  24. public AjaxResult<Object> handleNotLoginException(NotLoginException e){
  25. String msg = e.getMessage().startsWith("Token无效") ? "尚未登录,请先登录!" : e.getMessage();
  26. return AjaxResult.failed(ErrorEnum.TOKEN_INVALID.getCode(), msg);
  27. }
  28. /**
  29. * 拦截权限异常
  30. */
  31. @ResponseStatus(HttpStatus.OK)
  32. @ExceptionHandler(NotPermissionException.class)
  33. @ResponseBody
  34. public AjaxResult<Object> handleNotPermissionException(){
  35. return AjaxResult.failed(ErrorEnum.NO_PERMISSION.getCode(), ErrorEnum.NO_PERMISSION.getMsg());
  36. }
  37. }