token 无效

This commit is contained in:
2022-09-22 14:16:30 +08:00
parent b623970db8
commit 2f149911ba
14 changed files with 118 additions and 59 deletions

View File

@@ -49,17 +49,6 @@ public class GlobalExceptionHandler {
return buildResponseEntity(ApiError.error(e.getMessage()));
}
/**
* BadCredentialsException
*/
// @ExceptionHandler(BadCredentialsException.class)
// public ResponseEntity<ApiError> badCredentialsException(BadCredentialsException e){
// // 打印堆栈信息
// String message = "坏的凭证".equals(e.getMessage()) ? "用户名或密码不正确" : e.getMessage();
// log.error(message);
// return buildResponseEntity(ApiError.error(message));
// }
/**
* token 无效的异常拦截
* @param e
@@ -67,9 +56,8 @@ public class GlobalExceptionHandler {
*/
@ExceptionHandler(value = NotLoginException.class)
public ResponseEntity<ApiError> notLoginException(Exception e) {
// e.printStackTrace();
log.error(ThrowableUtil.getStackTrace(e));
return buildResponseEntity(ApiError.error("Token "));
return buildResponseEntity(ApiError.error(401,"token "));
}

View File

@@ -4,6 +4,8 @@ import cn.dev33.satoken.stp.StpUtil;
import lombok.extern.slf4j.Slf4j;
import org.nl.utils.dto.CurrentUser;
import java.util.List;
/**
* @author: lyd
* @description: 获取当前用户的信息 - 前提下在登录之后将数据存储到session
@@ -56,4 +58,17 @@ public class SecurityUtils {
}
return 0L;
}
/**
* 获取当前用户权限
* @return 权限列表
*/
public static List<String> getCurrentUserPermissions() {
CurrentUser userInfo = (CurrentUser) StpUtil.getTokenSession().get("userInfo");
List<String> permissions = userInfo.getPermissions();
if (permissions.size() > 0) {
return permissions;
}
return null;
}
}

View File

@@ -5,6 +5,8 @@ import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* @author: lyd
@@ -19,4 +21,6 @@ public class CurrentUser implements Serializable {
private String username;
private Object user;
private List<String> permissions = new ArrayList<>();
}