优化登录拦截功能

This commit is contained in:
TinyAnts 2023-03-16 18:33:58 +08:00
parent 06e219d2a1
commit 294ddbf4bc
2 changed files with 43 additions and 3 deletions

View File

@ -25,9 +25,7 @@ public class StpInException {
@ExceptionHandler(NotLoginException.class) @ExceptionHandler(NotLoginException.class)
@ResponseBody @ResponseBody
public AjaxResult<Object> handleNotLoginException(NotLoginException e){ public AjaxResult<Object> handleNotLoginException(NotLoginException e){
String msg = e.getMessage().split("")[0]; String msg = e.getMessage().startsWith("Token无效") ? "尚未登录,请先登录!" : e.getMessage();
msg = msg.replaceFirst("Token", "");
msg = msg.trim().equals("") ? HttpEnum.TOKEN_INVALID.getMsg() : msg;
return AjaxResult.failed(HttpEnum.TOKEN_INVALID.getCode(), msg); return AjaxResult.failed(HttpEnum.TOKEN_INVALID.getCode(), msg);
} }

View File

@ -0,0 +1,42 @@
package com.mdd.front.config.stp;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.enums.HttpEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
* Sa-Token的异常拦截
*/
@Slf4j
@ControllerAdvice
public class StpInException {
/**
* 拦截登录异常
*/
@ResponseStatus(HttpStatus.OK)
@ExceptionHandler(NotLoginException.class)
@ResponseBody
public AjaxResult<Object> handleNotLoginException(NotLoginException e){
String msg = e.getMessage().startsWith("Token无效") ? "尚未登录,请先登录!" : e.getMessage();
return AjaxResult.failed(HttpEnum.TOKEN_INVALID.getCode(), msg);
}
/**
* 拦截权限异常
*/
@ResponseStatus(HttpStatus.OK)
@ExceptionHandler(NotPermissionException.class)
@ResponseBody
public AjaxResult<Object> handleNotPermissionException(){
return AjaxResult.failed(HttpEnum.NO_PERMISSION.getCode(), HttpEnum.NO_PERMISSION.getMsg());
}
}