v1.0.3版本更新

This commit is contained in:
2020-05-02 15:19:55 +08:00
parent 23d0e7106c
commit 16b20f38cd
26 changed files with 219 additions and 83 deletions

View File

@@ -7,7 +7,7 @@ public class SaTokenUtil {
// sa-token 版本号
public static final String version = "v1.0.0";
public static final String version = "v1.0.3";
// sa-token 开源地址
public static final String github_url = "https://github.com/click33/sa-token";
@@ -23,8 +23,8 @@ public class SaTokenUtil {
System.out.println(str);
}
// 如果token为本次请求新创建的则以此字符串为key存储在当前request中
public static final String just_created_save_key= "just_created_save_key_";
// 如果token为本次请求新创建的则以此字符串为key存储在当前request中 JUST_CREATED_SAVE_KEY
public static final String JUST_CREATED_SAVE_KEY= "JUST_CREATED_SAVE_KEY_";
}

View File

@@ -6,6 +6,7 @@ import javax.servlet.http.HttpServletResponse;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.stp.StpUtil;
/**
@@ -13,6 +14,28 @@ import cn.dev33.satoken.stp.StpUtil;
*/
public class SaCheckInterceptor implements HandlerInterceptor {
// 底层的 StpLogic 对象
public StpLogic stpLogic = null;
/**
* 创建,并指定一个默认的 StpLogic
*/
public SaCheckInterceptor() {
this(StpUtil.stpLogic);
}
/**
* 创建,并指定一个的 StpLogic
* @param stpLogic 指定的StpLogic
*/
public SaCheckInterceptor(StpLogic stpLogic) {
this.stpLogic = stpLogic;
}
// 每次请求之前触发
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
@@ -26,7 +49,7 @@ public class SaCheckInterceptor implements HandlerInterceptor {
// 验证登录
if(method.hasMethodAnnotation(SaCheckLogin.class) || method.getBeanType().isAnnotationPresent(SaCheckLogin.class)) {
StpUtil.getLoginId();
stpLogic.checkLogin();
}
// 获取权限注解
@@ -41,14 +64,17 @@ public class SaCheckInterceptor implements HandlerInterceptor {
// 开始验证权限
Object[] codeArray = concatABC(scp.value(), scp.valueInt(), scp.valueLong());
if(scp.isAnd()) {
StpUtil.checkPermissionAnd(codeArray); // 必须全部都有
stpLogic.checkPermissionAnd(codeArray); // 必须全部都有
} else {
StpUtil.checkPermissionOr(codeArray); // 有一个就行了
stpLogic.checkPermissionOr(codeArray); // 有一个就行了
}
return true;
}
// 合并三个数组
private Object[] concatABC(String[] a, int[] b, long[] c) {

View File

@@ -6,7 +6,7 @@ package cn.dev33.satoken.config;
public class SaTokenConfig {
private String tokenName = "satoken"; // token名称同时也是cookie名称
private long timeout = 30 * 24 * 60 * 60; // token有效期单位s 默认30天-1为永不过期
private long timeout = 30 * 24 * 60 * 60; // token有效期单位s 默认30天
private Boolean isShare = true; // 在多人登录同一账号时是否共享会话为true时共用一个为false时新登录挤掉旧登录
private Boolean isReadHead = true; // 是否在cookie读取不到token时继续从请求header里继续尝试读取
private Boolean isReadBody = true; // 是否在header读取不到token时继续从请求题参数里继续尝试读取

View File

@@ -1,5 +1,7 @@
package cn.dev33.satoken.exception;
import cn.dev33.satoken.stp.StpUtil;
/**
* 没有登陆抛出的异常
*/
@@ -9,13 +11,35 @@ public class NotLoginException extends RuntimeException {
*
*/
private static final long serialVersionUID = 6806129545290130142L;
/**
* login_key
*/
private String login_key;
/**
* 获得login_key
* @return login_key
*/
public String getLoginKey() {
return login_key;
}
/**
* 创建一个
*/
public NotLoginException() {
super("当前账号未登录");
this(StpUtil.stpLogic.login_key);
}
/**
* 创建一个
* @param login_key login_key
*/
public NotLoginException(String login_key) {
super("当前会话未登录"); // 这里到底要不要拼接上login_key呢纠结
this.login_key = login_key;
}
}

View File

@@ -1,5 +1,7 @@
package cn.dev33.satoken.exception;
import cn.dev33.satoken.stp.StpUtil;
/**
* 没有指定权限码,抛出的异常
*/
@@ -10,24 +12,38 @@ public class NotPermissionException extends RuntimeException {
*/
private static final long serialVersionUID = 6806129545290130142L;
/**
* 权限码
*/
private Object code;
/**
* @return 获得权限码
*/
public Object getCode() {
return code;
}
/**
* login_key
*/
private String login_key;
/**
* 获得login_key
* @return login_key
*/
public String getLoginKey() {
return login_key;
}
public NotPermissionException(Object code) {
super("无此权限:" + code);
this(code, StpUtil.stpLogic.login_key);
}
public NotPermissionException(Object code, String login_key) {
super("无此权限:" + code); // 这里到底要不要拼接上login_key呢纠结
this.code = code;
this.login_key = login_key;
}
// public NotPermissionException(Object code, String s) {
// super(s);
// this.code = code;
// }
}

View File

@@ -27,7 +27,7 @@ import cn.dev33.satoken.util.SpringMVCUtil;
public class StpLogic {
private String login_key = ""; // 持久化的key前缀多账号体系时以此值区分比如login、user、admin
public String login_key = ""; // 持久化的key前缀多账号体系时以此值区分比如login、user、admin
public StpLogic(String login_key) {
this.login_key = login_key;
@@ -55,8 +55,8 @@ public class StpLogic {
String key_tokenName = getKey_tokenName();
// 1、尝试从request里读取
if(request.getAttribute(SaTokenUtil.just_created_save_key) != null) {
return String.valueOf(request.getAttribute(SaTokenUtil.just_created_save_key));
if(request.getAttribute(SaTokenUtil.JUST_CREATED_SAVE_KEY) != null) {
return String.valueOf(request.getAttribute(SaTokenUtil.JUST_CREATED_SAVE_KEY));
}
// 2、尝试从cookie里读取
@@ -137,7 +137,7 @@ public class StpLogic {
// 3、持久化
dao.setValue(getKey_TokenValue(tokenValue), String.valueOf(login_id), config.getTimeout()); // token -> uid
dao.setValue(getKey_LoginId(login_id), tokenValue, config.getTimeout()); // uid -> token
request.setAttribute(SaTokenUtil.just_created_save_key, tokenValue); // 保存到本次request里
request.setAttribute(SaTokenUtil.JUST_CREATED_SAVE_KEY, tokenValue); // 保存到本次request里
SaCookieUtil.addCookie(SpringMVCUtil.getResponse(), getKey_tokenName(), tokenValue, "/", (int)config.getTimeout()); // cookie注入
}
@@ -180,7 +180,14 @@ public class StpLogic {
public boolean isLogin() {
return getLoginId_defaultNull() != null;
}
/**
* 检验当前会话是否已经登录,如未登录,则抛出异常
*/
public void checkLogin() {
getLoginId();
}
/**
* 获取当前会话登录id, 如果未登录,则抛出异常
* @return
@@ -188,7 +195,7 @@ public class StpLogic {
public Object getLoginId() {
Object login_id = getLoginId_defaultNull();
if(login_id == null) {
throw new NotLoginException();
throw new NotLoginException(this.login_key);
}
return login_id;
}
@@ -263,6 +270,19 @@ public class StpLogic {
return Long.valueOf(String.valueOf(getLoginId()));
}
/**
* 获取指定token对应的登录id如果未登录则返回 null
* @return
*/
public Object getLoginIdByToken(String tokenValue) {
if(tokenValue != null) {
Object login_id = SaTokenManager.getDao().getValue(getKey_TokenValue(tokenValue));
if(login_id != null) {
return login_id;
}
}
return null;
}
// =================== session相关 ===================
@@ -329,7 +349,7 @@ public class StpLogic {
*/
public void checkPermission(Object pcode) {
if(hasPermission(pcode) == false) {
throw new NotPermissionException(pcode);
throw new NotPermissionException(pcode, this.login_key);
}
}
@@ -342,7 +362,7 @@ public class StpLogic {
List<Object> pcodeList = SaTokenManager.getStp().getPermissionCodeList(login_id, login_key);
for (Object pcode : pcodeArray) {
if(pcodeList.contains(pcode) == false) {
throw new NotPermissionException(pcode); // 没有权限抛出异常
throw new NotPermissionException(pcode, this.login_key); // 没有权限抛出异常
}
}
}
@@ -360,7 +380,7 @@ public class StpLogic {
}
}
if(pcodeArray.length > 0) {
throw new NotPermissionException(pcodeArray[0]); // 没有权限抛出异常
throw new NotPermissionException(pcodeArray[0], this.login_key); // 没有权限抛出异常
}
}

View File

@@ -79,6 +79,13 @@ public class StpUtil {
return stpLogic.isLogin();
}
/**
* 检验当前会话是否已经登录,如未登录,则抛出异常
*/
public static void checkLogin() {
getLoginId();
}
/**
* 获取当前会话登录id, 如果未登录,则抛出异常
* @return
@@ -128,6 +135,14 @@ public class StpUtil {
return stpLogic.getLoginId_asLong();
}
/**
* 获取指定token对应的登录id如果未登录则返回 null
* @return
*/
public static Object getLoginIdByToken(String tokenValue) {
return stpLogic.getLoginIdByToken(tokenValue);
}
// =================== session相关 ===================
/**

View File

@@ -7,7 +7,7 @@ spring:
sa-token:
# token名称同时也是cookie名称
token-name: satoken
# token有效期单位s 默认30天-1为永不过期
# token有效期单位s 默认30天
timeout: 2592000
# 在多人登录同一账号时是否共享会话为true时共用一个为false时新登录挤掉旧登录
is-share: true