新增token临时验证模块

This commit is contained in:
click33
2021-06-09 23:25:29 +08:00
parent 6eec264e70
commit bebee60901
9 changed files with 222 additions and 43 deletions

View File

@@ -18,6 +18,8 @@ import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpInterfaceDefaultImpl;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.temp.SaTempInterface;
import cn.dev33.satoken.temp.SaTempInterfaceDefaultImpl;
import cn.dev33.satoken.util.SaFoxUtil;
/**
@@ -143,6 +145,24 @@ public class SaManager {
return saTokenListener;
}
/**
* 临时验证模块 Bean
*/
private static SaTempInterface saTemp;
public static void setSaTemp(SaTempInterface saTemp) {
SaManager.saTemp = saTemp;
}
public static SaTempInterface getSaTemp() {
if (saTemp == null) {
synchronized (SaManager.class) {
if (saTemp == null) {
setSaTemp(new SaTempInterfaceDefaultImpl());
}
}
}
return saTemp;
}
/**
* StpLogic集合, 记录框架所有成功初始化的StpLogic
*/

View File

@@ -9,6 +9,7 @@ import java.util.concurrent.ConcurrentHashMap;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.fun.SaRetFunction;
import cn.dev33.satoken.util.SaFoxUtil;
/**
* Session Model
@@ -401,7 +402,7 @@ public class SaSession implements Serializable {
* @return 值
*/
public <T> T getModel(String key, Class<T> cs) {
return getValueByClass(get(key), cs);
return SaFoxUtil.getValueByType(get(key), cs);
}
/**
@@ -418,7 +419,7 @@ public class SaSession implements Serializable {
if(valueIsNull(value)) {
return (T)defaultValue;
}
return getValueByClass(value, cs);
return SaFoxUtil.getValueByType(value, cs);
}
// ---- 其他
@@ -480,44 +481,6 @@ public class SaSession implements Serializable {
return value == null || value.equals("");
}
/**
* 将指定值转化为指定类型
* @param <T> 泛型
* @param obj 值
* @param cs 类型
* @return 转换后的值
*/
@SuppressWarnings("unchecked")
protected <T> T getValueByClass(Object obj, Class<T> cs) {
// 如果 obj 本来就是 cs 类型
if(obj != null && obj.getClass().equals(cs)) {
return (T)obj;
}
// 开始转换
String obj2 = String.valueOf(obj);
Object obj3 = null;
if (cs.equals(String.class)) {
obj3 = obj2;
} else if (cs.equals(int.class) || cs.equals(Integer.class)) {
obj3 = Integer.valueOf(obj2);
} else if (cs.equals(long.class) || cs.equals(Long.class)) {
obj3 = Long.valueOf(obj2);
} else if (cs.equals(short.class) || cs.equals(Short.class)) {
obj3 = Short.valueOf(obj2);
} else if (cs.equals(byte.class) || cs.equals(Byte.class)) {
obj3 = Byte.valueOf(obj2);
} else if (cs.equals(float.class) || cs.equals(Float.class)) {
obj3 = Float.valueOf(obj2);
} else if (cs.equals(double.class) || cs.equals(Double.class)) {
obj3 = Double.valueOf(obj2);
} else if (cs.equals(boolean.class) || cs.equals(Boolean.class)) {
obj3 = Boolean.valueOf(obj2);
} else {
obj3 = (T)obj;
}
return (T)obj3;
}
/**
* 根据默认值来获取值
* @param <T> 泛型
@@ -535,7 +498,7 @@ public class SaSession implements Serializable {
// 开始转换
Class<T> cs = (Class<T>) defaultValue.getClass();
return getValueByClass(value, cs);
return SaFoxUtil.getValueByType(value, cs);
}

View File

@@ -0,0 +1,72 @@
package cn.dev33.satoken.temp;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.util.SaFoxUtil;
/**
* Sa-Token 临时验证模块接口
* @author kong
*
*/
public interface SaTempInterface {
/**
* 根据value创建一个token
* @param value 指定值
* @param time 有效期,单位:秒
* @return 生成的token
*/
public default String createToken(Object value, long timeout) {
// 生成 token
String token = SaManager.getSaTokenAction().createToken(null, null);
// 持久化映射关系
String key = splicingKeyTempToken(token);
SaManager.getSaTokenDao().setObject(key, value, timeout);
// 返回
return token;
}
/**
* 解析token获取value
* @param token 指定token
* @return See Note
*/
public default Object parseToken(String token) {
String key = splicingKeyTempToken(token);
return SaManager.getSaTokenDao().getObject(key);
}
/**
* 解析token获取value并转换为指定类型
* @param token 指定token
* @param cs 指定类型
* @return See Note
*/
public default<T> T parseToken(String token, Class<T> cs) {
return SaFoxUtil.getValueByType(parseToken(token), cs);
}
/**
* 返回指定token的剩余有效期单位
* <p> 返回值 -1 代表永久,-2 代表token无效
* @param token see note
* @return see note
*/
public default long getTimeout(String token) {
String key = splicingKeyTempToken(token);
return SaManager.getSaTokenDao().getObjectTimeout(key);
}
/**
* 获取映射关系的持久化key
* @param token token值
* @return key
*/
public default String splicingKeyTempToken(String token) {
return SaManager.getConfig().getTokenName() + ":temp-token:" + token;
}
}

View File

@@ -0,0 +1,10 @@
package cn.dev33.satoken.temp;
/**
* Sa-Token 临时验证模块 逻辑
* @author kong
*
*/
public class SaTempInterfaceDefaultImpl implements SaTempInterface {
}

View File

@@ -0,0 +1,51 @@
package cn.dev33.satoken.temp;
import cn.dev33.satoken.SaManager;
/**
* Sa-Token 临时验证模块
* @author kong
*
*/
public class SaTempUtil {
/**
* 根据value创建一个token
* @param value 指定值
* @param time 有效期,单位:秒
* @return 生成的token
*/
public static String createToken(Object value, long timeout) {
return SaManager.getSaTemp().createToken(value, timeout);
}
/**
* 解析token获取value
* @param token 指定token
* @return See Note
*/
public static Object parseToken(String token) {
return SaManager.getSaTemp().parseToken(token);
}
/**
* 解析token获取value并转换为指定类型
* @param token 指定token
* @param cs 指定类型
* @return See Note
*/
public static<T> T parseToken(String token, Class<T> cs) {
return SaManager.getSaTemp().parseToken(token, cs);
}
/**
* 返回指定token的剩余有效期单位
* <p> 返回值 -1 代表永久,-2 代表token无效
* @param token see note
* @return see note
*/
public static long getTimeout(String token) {
return SaManager.getSaTemp().getTimeout(token);
}
}

View File

@@ -147,4 +147,44 @@ public class SaFoxUtil {
return Pattern.matches(patt.replaceAll("\\*", ".*"), str);
}
/**
* 将指定值转化为指定类型
* @param <T> 泛型
* @param obj 值
* @param cs 类型
* @return 转换后的值
*/
@SuppressWarnings("unchecked")
public static <T> T getValueByType(Object obj, Class<T> cs) {
// 如果 obj 为 null 或者本来就是 cs 类型
if(obj == null || obj.getClass().equals(cs)) {
return (T)obj;
}
// 开始转换
String obj2 = String.valueOf(obj);
Object obj3 = null;
if (cs.equals(String.class)) {
obj3 = obj2;
} else if (cs.equals(int.class) || cs.equals(Integer.class)) {
obj3 = Integer.valueOf(obj2);
} else if (cs.equals(long.class) || cs.equals(Long.class)) {
obj3 = Long.valueOf(obj2);
} else if (cs.equals(short.class) || cs.equals(Short.class)) {
obj3 = Short.valueOf(obj2);
} else if (cs.equals(byte.class) || cs.equals(Byte.class)) {
obj3 = Byte.valueOf(obj2);
} else if (cs.equals(float.class) || cs.equals(Float.class)) {
obj3 = Float.valueOf(obj2);
} else if (cs.equals(double.class) || cs.equals(Double.class)) {
obj3 = Double.valueOf(obj2);
} else if (cs.equals(boolean.class) || cs.equals(Boolean.class)) {
obj3 = Boolean.valueOf(obj2);
} else {
obj3 = (T)obj;
}
return (T)obj3;
}
}