mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-09-19 10:08:07 +08:00
单元测试改为 junit5
This commit is contained in:
@@ -3,8 +3,6 @@ package cn.dev33.satoken;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import cn.dev33.satoken.action.SaTokenAction;
|
|
||||||
import cn.dev33.satoken.action.SaTokenActionDefaultImpl;
|
|
||||||
import cn.dev33.satoken.config.SaTokenConfig;
|
import cn.dev33.satoken.config.SaTokenConfig;
|
||||||
import cn.dev33.satoken.config.SaTokenConfigFactory;
|
import cn.dev33.satoken.config.SaTokenConfigFactory;
|
||||||
import cn.dev33.satoken.context.SaTokenContext;
|
import cn.dev33.satoken.context.SaTokenContext;
|
||||||
@@ -28,7 +26,6 @@ import cn.dev33.satoken.util.SaFoxUtil;
|
|||||||
* @author kong
|
* @author kong
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
public class SaManager {
|
public class SaManager {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -93,24 +90,6 @@ public class SaManager {
|
|||||||
return stpInterface;
|
return stpInterface;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 框架行为 Bean
|
|
||||||
*/
|
|
||||||
private volatile static SaTokenAction saTokenAction;
|
|
||||||
public static void setSaTokenAction(SaTokenAction saTokenAction) {
|
|
||||||
SaManager.saTokenAction = saTokenAction;
|
|
||||||
}
|
|
||||||
public static SaTokenAction getSaTokenAction() {
|
|
||||||
if (saTokenAction == null) {
|
|
||||||
synchronized (SaManager.class) {
|
|
||||||
if (saTokenAction == null) {
|
|
||||||
setSaTokenAction(new SaTokenActionDefaultImpl());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return saTokenAction;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上下文Context Bean
|
* 上下文Context Bean
|
||||||
*/
|
*/
|
||||||
|
@@ -1,54 +1,54 @@
|
|||||||
package cn.dev33.satoken.action;
|
//package cn.dev33.satoken.action;
|
||||||
|
//
|
||||||
import java.lang.reflect.AnnotatedElement;
|
//import java.lang.reflect.AnnotatedElement;
|
||||||
import java.lang.reflect.Method;
|
//import java.lang.reflect.Method;
|
||||||
import java.util.List;
|
//import java.util.List;
|
||||||
|
//
|
||||||
import cn.dev33.satoken.session.SaSession;
|
//import cn.dev33.satoken.session.SaSession;
|
||||||
|
//
|
||||||
/**
|
///**
|
||||||
* <h1> v1.27+ 此接口已废弃,目前版本暂时向下兼容,请及时更换为 SaStrategy </h1>
|
// * <h1> v1.27+ 此接口已废弃,目前版本暂时向下兼容,请及时更换为 SaStrategy </h1>
|
||||||
* <p>Sa-Token 逻辑代理接口 </p>
|
// * <p>Sa-Token 逻辑代理接口 </p>
|
||||||
* <p>此接口将会代理框架内部的一些关键性逻辑,方便开发者进行按需重写</p>
|
// * <p>此接口将会代理框架内部的一些关键性逻辑,方便开发者进行按需重写</p>
|
||||||
* @author kong
|
// * @author kong
|
||||||
*
|
// *
|
||||||
*/
|
// */
|
||||||
@Deprecated
|
//@Deprecated
|
||||||
public interface SaTokenAction {
|
//public interface SaTokenAction {
|
||||||
|
//
|
||||||
/**
|
// /**
|
||||||
* 创建一个Token
|
// * 创建一个Token
|
||||||
* @param loginId 账号id
|
// * @param loginId 账号id
|
||||||
* @param loginType 账号类型
|
// * @param loginType 账号类型
|
||||||
* @return token
|
// * @return token
|
||||||
*/
|
// */
|
||||||
public String createToken(Object loginId, String loginType);
|
// public String createToken(Object loginId, String loginType);
|
||||||
|
//
|
||||||
/**
|
// /**
|
||||||
* 创建一个Session
|
// * 创建一个Session
|
||||||
* @param sessionId Session的Id
|
// * @param sessionId Session的Id
|
||||||
* @return 创建后的Session
|
// * @return 创建后的Session
|
||||||
*/
|
// */
|
||||||
public SaSession createSession(String sessionId);
|
// public SaSession createSession(String sessionId);
|
||||||
|
//
|
||||||
/**
|
// /**
|
||||||
* 判断:集合中是否包含指定元素(模糊匹配)
|
// * 判断:集合中是否包含指定元素(模糊匹配)
|
||||||
* @param list 集合
|
// * @param list 集合
|
||||||
* @param element 元素
|
// * @param element 元素
|
||||||
* @return 是否包含
|
// * @return 是否包含
|
||||||
*/
|
// */
|
||||||
public boolean hasElement(List<String> list, String element);
|
// public boolean hasElement(List<String> list, String element);
|
||||||
|
//
|
||||||
/**
|
// /**
|
||||||
* 对一个Method对象进行注解检查(注解鉴权内部实现)
|
// * 对一个Method对象进行注解检查(注解鉴权内部实现)
|
||||||
* @param method Method对象
|
// * @param method Method对象
|
||||||
*/
|
// */
|
||||||
public void checkMethodAnnotation(Method method);
|
// public void checkMethodAnnotation(Method method);
|
||||||
|
//
|
||||||
/**
|
// /**
|
||||||
* 从指定元素校验注解
|
// * 从指定元素校验注解
|
||||||
* @param target /
|
// * @param target /
|
||||||
*/
|
// */
|
||||||
public void validateAnnotation(AnnotatedElement target);
|
// public void validateAnnotation(AnnotatedElement target);
|
||||||
|
//
|
||||||
}
|
//}
|
||||||
|
@@ -1,150 +1,150 @@
|
|||||||
package cn.dev33.satoken.action;
|
//package cn.dev33.satoken.action;
|
||||||
|
//
|
||||||
import java.lang.reflect.AnnotatedElement;
|
//import java.lang.reflect.AnnotatedElement;
|
||||||
import java.lang.reflect.Method;
|
//import java.lang.reflect.Method;
|
||||||
import java.util.List;
|
//import java.util.List;
|
||||||
import java.util.UUID;
|
//import java.util.UUID;
|
||||||
|
//
|
||||||
import cn.dev33.satoken.SaManager;
|
//import cn.dev33.satoken.SaManager;
|
||||||
import cn.dev33.satoken.annotation.SaCheckBasic;
|
//import cn.dev33.satoken.annotation.SaCheckBasic;
|
||||||
import cn.dev33.satoken.annotation.SaCheckLogin;
|
//import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
//import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||||
import cn.dev33.satoken.annotation.SaCheckRole;
|
//import cn.dev33.satoken.annotation.SaCheckRole;
|
||||||
import cn.dev33.satoken.annotation.SaCheckSafe;
|
//import cn.dev33.satoken.annotation.SaCheckSafe;
|
||||||
import cn.dev33.satoken.basic.SaBasicUtil;
|
//import cn.dev33.satoken.basic.SaBasicUtil;
|
||||||
import cn.dev33.satoken.session.SaSession;
|
//import cn.dev33.satoken.session.SaSession;
|
||||||
import cn.dev33.satoken.strategy.SaStrategy;
|
//import cn.dev33.satoken.strategy.SaStrategy;
|
||||||
import cn.dev33.satoken.util.SaFoxUtil;
|
//import cn.dev33.satoken.util.SaFoxUtil;
|
||||||
import cn.dev33.satoken.util.SaTokenConsts;
|
//import cn.dev33.satoken.util.SaTokenConsts;
|
||||||
|
//
|
||||||
/**
|
///**
|
||||||
* <h1> v1.27+ 此接口已废弃,目前版本暂时向下兼容,请及时更换为 SaStrategy </h1>
|
// * <h1> v1.27+ 此接口已废弃,目前版本暂时向下兼容,请及时更换为 SaStrategy </h1>
|
||||||
* <p> Sa-Token 逻辑代理接口 [默认实现类] </p>
|
// * <p> Sa-Token 逻辑代理接口 [默认实现类] </p>
|
||||||
* @author kong
|
// * @author kong
|
||||||
*
|
// *
|
||||||
*/
|
// */
|
||||||
@Deprecated
|
//@Deprecated
|
||||||
public class SaTokenActionDefaultImpl implements SaTokenAction {
|
//public class SaTokenActionDefaultImpl implements SaTokenAction {
|
||||||
|
//
|
||||||
/**
|
// /**
|
||||||
* 创建一个Token
|
// * 创建一个Token
|
||||||
*/
|
// */
|
||||||
@Override
|
// @Override
|
||||||
public String createToken(Object loginId, String loginType) {
|
// public String createToken(Object loginId, String loginType) {
|
||||||
// 根据配置的tokenStyle生成不同风格的token
|
// // 根据配置的tokenStyle生成不同风格的token
|
||||||
String tokenStyle = SaManager.getConfig().getTokenStyle();
|
// String tokenStyle = SaManager.getConfig().getTokenStyle();
|
||||||
// uuid
|
// // uuid
|
||||||
if(SaTokenConsts.TOKEN_STYLE_UUID.equals(tokenStyle)) {
|
// if(SaTokenConsts.TOKEN_STYLE_UUID.equals(tokenStyle)) {
|
||||||
return UUID.randomUUID().toString();
|
// return UUID.randomUUID().toString();
|
||||||
}
|
// }
|
||||||
// 简单uuid (不带下划线)
|
// // 简单uuid (不带下划线)
|
||||||
if(SaTokenConsts.TOKEN_STYLE_SIMPLE_UUID.equals(tokenStyle)) {
|
// if(SaTokenConsts.TOKEN_STYLE_SIMPLE_UUID.equals(tokenStyle)) {
|
||||||
return UUID.randomUUID().toString().replaceAll("-", "");
|
// return UUID.randomUUID().toString().replaceAll("-", "");
|
||||||
}
|
// }
|
||||||
// 32位随机字符串
|
// // 32位随机字符串
|
||||||
if(SaTokenConsts.TOKEN_STYLE_RANDOM_32.equals(tokenStyle)) {
|
// if(SaTokenConsts.TOKEN_STYLE_RANDOM_32.equals(tokenStyle)) {
|
||||||
return SaFoxUtil.getRandomString(32);
|
// return SaFoxUtil.getRandomString(32);
|
||||||
}
|
// }
|
||||||
// 64位随机字符串
|
// // 64位随机字符串
|
||||||
if(SaTokenConsts.TOKEN_STYLE_RANDOM_64.equals(tokenStyle)) {
|
// if(SaTokenConsts.TOKEN_STYLE_RANDOM_64.equals(tokenStyle)) {
|
||||||
return SaFoxUtil.getRandomString(64);
|
// return SaFoxUtil.getRandomString(64);
|
||||||
}
|
// }
|
||||||
// 128位随机字符串
|
// // 128位随机字符串
|
||||||
if(SaTokenConsts.TOKEN_STYLE_RANDOM_128.equals(tokenStyle)) {
|
// if(SaTokenConsts.TOKEN_STYLE_RANDOM_128.equals(tokenStyle)) {
|
||||||
return SaFoxUtil.getRandomString(128);
|
// return SaFoxUtil.getRandomString(128);
|
||||||
}
|
// }
|
||||||
// tik风格 (2_14_16)
|
// // tik风格 (2_14_16)
|
||||||
if(SaTokenConsts.TOKEN_STYLE_TIK.equals(tokenStyle)) {
|
// if(SaTokenConsts.TOKEN_STYLE_TIK.equals(tokenStyle)) {
|
||||||
return SaFoxUtil.getRandomString(2) + "_" + SaFoxUtil.getRandomString(14) + "_" + SaFoxUtil.getRandomString(16) + "__";
|
// return SaFoxUtil.getRandomString(2) + "_" + SaFoxUtil.getRandomString(14) + "_" + SaFoxUtil.getRandomString(16) + "__";
|
||||||
}
|
// }
|
||||||
// 默认,还是uuid
|
// // 默认,还是uuid
|
||||||
return UUID.randomUUID().toString();
|
// return UUID.randomUUID().toString();
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
/**
|
// /**
|
||||||
* 创建一个Session
|
// * 创建一个Session
|
||||||
*/
|
// */
|
||||||
@Override
|
// @Override
|
||||||
public SaSession createSession(String sessionId) {
|
// public SaSession createSession(String sessionId) {
|
||||||
return new SaSession(sessionId);
|
// return new SaSession(sessionId);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
/**
|
// /**
|
||||||
* 判断:集合中是否包含指定元素(模糊匹配)
|
// * 判断:集合中是否包含指定元素(模糊匹配)
|
||||||
*/
|
// */
|
||||||
@Override
|
// @Override
|
||||||
public boolean hasElement(List<String> list, String element) {
|
// public boolean hasElement(List<String> list, String element) {
|
||||||
|
//
|
||||||
// 空集合直接返回false
|
// // 空集合直接返回false
|
||||||
if(list == null || list.size() == 0) {
|
// if(list == null || list.size() == 0) {
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// 先尝试一下简单匹配,如果可以匹配成功则无需继续模糊匹配
|
// // 先尝试一下简单匹配,如果可以匹配成功则无需继续模糊匹配
|
||||||
if (list.contains(element)) {
|
// if (list.contains(element)) {
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// 开始模糊匹配
|
// // 开始模糊匹配
|
||||||
for (String patt : list) {
|
// for (String patt : list) {
|
||||||
if(SaFoxUtil.vagueMatch(patt, element)) {
|
// if(SaFoxUtil.vagueMatch(patt, element)) {
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// 走出for循环说明没有一个元素可以匹配成功
|
// // 走出for循环说明没有一个元素可以匹配成功
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
/**
|
// /**
|
||||||
* 对一个Method对象进行注解检查(注解鉴权内部实现)
|
// * 对一个Method对象进行注解检查(注解鉴权内部实现)
|
||||||
*/
|
// */
|
||||||
@Override
|
// @Override
|
||||||
public void checkMethodAnnotation(Method method) {
|
// public void checkMethodAnnotation(Method method) {
|
||||||
|
//
|
||||||
// 先校验 Method 所属 Class 上的注解
|
// // 先校验 Method 所属 Class 上的注解
|
||||||
validateAnnotation(method.getDeclaringClass());
|
// validateAnnotation(method.getDeclaringClass());
|
||||||
|
//
|
||||||
// 再校验 Method 上的注解
|
// // 再校验 Method 上的注解
|
||||||
validateAnnotation(method);
|
// validateAnnotation(method);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
/**
|
// /**
|
||||||
* 从指定元素校验注解
|
// * 从指定元素校验注解
|
||||||
* @param target see note
|
// * @param target see note
|
||||||
*/
|
// */
|
||||||
public void validateAnnotation(AnnotatedElement target) {
|
// public void validateAnnotation(AnnotatedElement target) {
|
||||||
|
//
|
||||||
// 校验 @SaCheckLogin 注解
|
// // 校验 @SaCheckLogin 注解
|
||||||
SaCheckLogin checkLogin = (SaCheckLogin) SaStrategy.me.getAnnotation.apply(target, SaCheckLogin.class);
|
// SaCheckLogin checkLogin = (SaCheckLogin) SaStrategy.me.getAnnotation.apply(target, SaCheckLogin.class);
|
||||||
if(checkLogin != null) {
|
// if(checkLogin != null) {
|
||||||
SaManager.getStpLogic(checkLogin.type()).checkByAnnotation(checkLogin);
|
// SaManager.getStpLogic(checkLogin.type()).checkByAnnotation(checkLogin);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// 校验 @SaCheckRole 注解
|
// // 校验 @SaCheckRole 注解
|
||||||
SaCheckRole checkRole = (SaCheckRole) SaStrategy.me.getAnnotation.apply(target, SaCheckRole.class);
|
// SaCheckRole checkRole = (SaCheckRole) SaStrategy.me.getAnnotation.apply(target, SaCheckRole.class);
|
||||||
if(checkRole != null) {
|
// if(checkRole != null) {
|
||||||
SaManager.getStpLogic(checkRole.type()).checkByAnnotation(checkRole);
|
// SaManager.getStpLogic(checkRole.type()).checkByAnnotation(checkRole);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// 校验 @SaCheckPermission 注解
|
// // 校验 @SaCheckPermission 注解
|
||||||
SaCheckPermission checkPermission = (SaCheckPermission) SaStrategy.me.getAnnotation.apply(target, SaCheckPermission.class);
|
// SaCheckPermission checkPermission = (SaCheckPermission) SaStrategy.me.getAnnotation.apply(target, SaCheckPermission.class);
|
||||||
if(checkPermission != null) {
|
// if(checkPermission != null) {
|
||||||
SaManager.getStpLogic(checkPermission.type()).checkByAnnotation(checkPermission);
|
// SaManager.getStpLogic(checkPermission.type()).checkByAnnotation(checkPermission);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// 校验 @SaCheckSafe 注解
|
// // 校验 @SaCheckSafe 注解
|
||||||
SaCheckSafe checkSafe = (SaCheckSafe) SaStrategy.me.getAnnotation.apply(target, SaCheckSafe.class);
|
// SaCheckSafe checkSafe = (SaCheckSafe) SaStrategy.me.getAnnotation.apply(target, SaCheckSafe.class);
|
||||||
if(checkSafe != null) {
|
// if(checkSafe != null) {
|
||||||
SaManager.getStpLogic(checkSafe.type()).checkByAnnotation(checkSafe);
|
// SaManager.getStpLogic(checkSafe.type()).checkByAnnotation(checkSafe);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// 校验 @SaCheckBasic 注解
|
// // 校验 @SaCheckBasic 注解
|
||||||
SaCheckBasic checkBasic = (SaCheckBasic) SaStrategy.me.getAnnotation.apply(target, SaCheckBasic.class);
|
// SaCheckBasic checkBasic = (SaCheckBasic) SaStrategy.me.getAnnotation.apply(target, SaCheckBasic.class);
|
||||||
if(checkBasic != null) {
|
// if(checkBasic != null) {
|
||||||
SaBasicUtil.check(checkBasic.realm(), checkBasic.account());
|
// SaBasicUtil.check(checkBasic.realm(), checkBasic.account());
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
}
|
//}
|
||||||
|
@@ -4,12 +4,21 @@ import java.lang.annotation.Annotation;
|
|||||||
import java.lang.reflect.AnnotatedElement;
|
import java.lang.reflect.AnnotatedElement;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
import cn.dev33.satoken.SaManager;
|
import cn.dev33.satoken.SaManager;
|
||||||
|
import cn.dev33.satoken.annotation.SaCheckBasic;
|
||||||
|
import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||||
|
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||||
|
import cn.dev33.satoken.annotation.SaCheckRole;
|
||||||
|
import cn.dev33.satoken.annotation.SaCheckSafe;
|
||||||
|
import cn.dev33.satoken.basic.SaBasicUtil;
|
||||||
import cn.dev33.satoken.session.SaSession;
|
import cn.dev33.satoken.session.SaSession;
|
||||||
|
import cn.dev33.satoken.util.SaFoxUtil;
|
||||||
|
import cn.dev33.satoken.util.SaTokenConsts;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sa-Token 策略对象
|
* Sa-Token 策略对象
|
||||||
@@ -27,7 +36,6 @@ import cn.dev33.satoken.session.SaSession;
|
|||||||
* @author kong
|
* @author kong
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
public final class SaStrategy {
|
public final class SaStrategy {
|
||||||
|
|
||||||
private SaStrategy() {
|
private SaStrategy() {
|
||||||
@@ -47,7 +55,34 @@ public final class SaStrategy {
|
|||||||
* <p> 参数 [账号id, 账号类型]
|
* <p> 参数 [账号id, 账号类型]
|
||||||
*/
|
*/
|
||||||
public BiFunction<Object, String, String> createToken = (loginId, loginType) -> {
|
public BiFunction<Object, String, String> createToken = (loginId, loginType) -> {
|
||||||
return SaManager.getSaTokenAction().createToken(loginId, loginType);
|
// 根据配置的tokenStyle生成不同风格的token
|
||||||
|
String tokenStyle = SaManager.getConfig().getTokenStyle();
|
||||||
|
// uuid
|
||||||
|
if(SaTokenConsts.TOKEN_STYLE_UUID.equals(tokenStyle)) {
|
||||||
|
return UUID.randomUUID().toString();
|
||||||
|
}
|
||||||
|
// 简单uuid (不带下划线)
|
||||||
|
if(SaTokenConsts.TOKEN_STYLE_SIMPLE_UUID.equals(tokenStyle)) {
|
||||||
|
return UUID.randomUUID().toString().replaceAll("-", "");
|
||||||
|
}
|
||||||
|
// 32位随机字符串
|
||||||
|
if(SaTokenConsts.TOKEN_STYLE_RANDOM_32.equals(tokenStyle)) {
|
||||||
|
return SaFoxUtil.getRandomString(32);
|
||||||
|
}
|
||||||
|
// 64位随机字符串
|
||||||
|
if(SaTokenConsts.TOKEN_STYLE_RANDOM_64.equals(tokenStyle)) {
|
||||||
|
return SaFoxUtil.getRandomString(64);
|
||||||
|
}
|
||||||
|
// 128位随机字符串
|
||||||
|
if(SaTokenConsts.TOKEN_STYLE_RANDOM_128.equals(tokenStyle)) {
|
||||||
|
return SaFoxUtil.getRandomString(128);
|
||||||
|
}
|
||||||
|
// tik风格 (2_14_16)
|
||||||
|
if(SaTokenConsts.TOKEN_STYLE_TIK.equals(tokenStyle)) {
|
||||||
|
return SaFoxUtil.getRandomString(2) + "_" + SaFoxUtil.getRandomString(14) + "_" + SaFoxUtil.getRandomString(16) + "__";
|
||||||
|
}
|
||||||
|
// 默认,还是uuid
|
||||||
|
return UUID.randomUUID().toString();
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -55,7 +90,7 @@ public final class SaStrategy {
|
|||||||
* <p> 参数 [SessionId]
|
* <p> 参数 [SessionId]
|
||||||
*/
|
*/
|
||||||
public Function<String, SaSession> createSession = (sessionId) -> {
|
public Function<String, SaSession> createSession = (sessionId) -> {
|
||||||
return SaManager.getSaTokenAction().createSession(sessionId);
|
return new SaSession(sessionId);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -63,7 +98,26 @@ public final class SaStrategy {
|
|||||||
* <p> 参数 [集合, 元素]
|
* <p> 参数 [集合, 元素]
|
||||||
*/
|
*/
|
||||||
public BiFunction<List<String>, String, Boolean> hasElement = (list, element) -> {
|
public BiFunction<List<String>, String, Boolean> hasElement = (list, element) -> {
|
||||||
return SaManager.getSaTokenAction().hasElement(list, element);
|
|
||||||
|
// 空集合直接返回false
|
||||||
|
if(list == null || list.size() == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 先尝试一下简单匹配,如果可以匹配成功则无需继续模糊匹配
|
||||||
|
if (list.contains(element)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 开始模糊匹配
|
||||||
|
for (String patt : list) {
|
||||||
|
if(SaFoxUtil.vagueMatch(patt, element)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 走出for循环说明没有一个元素可以匹配成功
|
||||||
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -83,9 +137,37 @@ public final class SaStrategy {
|
|||||||
* 对一个 [元素] 对象进行注解校验 (注解鉴权内部实现)
|
* 对一个 [元素] 对象进行注解校验 (注解鉴权内部实现)
|
||||||
* <p> 参数 [element元素]
|
* <p> 参数 [element元素]
|
||||||
*/
|
*/
|
||||||
public Consumer<AnnotatedElement> checkElementAnnotation = (element) -> {
|
public Consumer<AnnotatedElement> checkElementAnnotation = (target) -> {
|
||||||
// 为了兼容旧版本
|
// 校验 @SaCheckLogin 注解
|
||||||
SaManager.getSaTokenAction().validateAnnotation(element);
|
SaCheckLogin checkLogin = (SaCheckLogin) SaStrategy.me.getAnnotation.apply(target, SaCheckLogin.class);
|
||||||
|
if(checkLogin != null) {
|
||||||
|
SaManager.getStpLogic(checkLogin.type()).checkByAnnotation(checkLogin);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 校验 @SaCheckRole 注解
|
||||||
|
SaCheckRole checkRole = (SaCheckRole) SaStrategy.me.getAnnotation.apply(target, SaCheckRole.class);
|
||||||
|
if(checkRole != null) {
|
||||||
|
SaManager.getStpLogic(checkRole.type()).checkByAnnotation(checkRole);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 校验 @SaCheckPermission 注解
|
||||||
|
SaCheckPermission checkPermission = (SaCheckPermission) SaStrategy.me.getAnnotation.apply(target, SaCheckPermission.class);
|
||||||
|
if(checkPermission != null) {
|
||||||
|
SaManager.getStpLogic(checkPermission.type()).checkByAnnotation(checkPermission);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 校验 @SaCheckSafe 注解
|
||||||
|
SaCheckSafe checkSafe = (SaCheckSafe) SaStrategy.me.getAnnotation.apply(target, SaCheckSafe.class);
|
||||||
|
if(checkSafe != null) {
|
||||||
|
SaManager.getStpLogic(checkSafe.type()).checkByAnnotation(checkSafe);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 校验 @SaCheckBasic 注解
|
||||||
|
SaCheckBasic checkBasic = (SaCheckBasic) SaStrategy.me.getAnnotation.apply(target, SaCheckBasic.class);
|
||||||
|
if(checkBasic != null) {
|
||||||
|
SaBasicUtil.check(checkBasic.realm(), checkBasic.account());
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -27,6 +27,10 @@ public class SaTokenDubboProviderFilter implements Filter {
|
|||||||
// RPC 调用鉴权
|
// RPC 调用鉴权
|
||||||
if(SaManager.getConfig().getCheckIdToken()) {
|
if(SaManager.getConfig().getCheckIdToken()) {
|
||||||
String idToken = invocation.getAttachment(SaIdUtil.ID_TOKEN);
|
String idToken = invocation.getAttachment(SaIdUtil.ID_TOKEN);
|
||||||
|
// dubbo部分协议会将参数变为小写,详细参考:https://gitee.com/dromara/sa-token/issues/I4WXQG
|
||||||
|
if(idToken == null) {
|
||||||
|
idToken = invocation.getAttachment(SaIdUtil.ID_TOKEN.toLowerCase());
|
||||||
|
}
|
||||||
SaIdUtil.checkToken(idToken);
|
SaIdUtil.checkToken(idToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -59,16 +59,6 @@ public class SaBeanInject {
|
|||||||
SaManager.setStpInterface(stpInterface);
|
SaManager.setStpInterface(stpInterface);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 注入框架行为Bean
|
|
||||||
*
|
|
||||||
* @param saTokenAction SaTokenAction对象
|
|
||||||
*/
|
|
||||||
@Autowired(required = false)
|
|
||||||
public void setSaTokenAction(@SuppressWarnings("deprecation") cn.dev33.satoken.action.SaTokenAction saTokenAction) {
|
|
||||||
SaManager.setSaTokenAction(saTokenAction);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注入上下文Bean
|
* 注入上下文Bean
|
||||||
*
|
*
|
||||||
|
@@ -6,7 +6,6 @@ import org.noear.solon.core.Aop;
|
|||||||
import org.noear.solon.core.Plugin;
|
import org.noear.solon.core.Plugin;
|
||||||
|
|
||||||
import cn.dev33.satoken.SaManager;
|
import cn.dev33.satoken.SaManager;
|
||||||
import cn.dev33.satoken.action.SaTokenAction;
|
|
||||||
import cn.dev33.satoken.annotation.SaCheckBasic;
|
import cn.dev33.satoken.annotation.SaCheckBasic;
|
||||||
import cn.dev33.satoken.annotation.SaCheckLogin;
|
import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||||
@@ -33,7 +32,6 @@ import cn.dev33.satoken.temp.SaTempInterface;
|
|||||||
* @author noear
|
* @author noear
|
||||||
* @since 1.4
|
* @since 1.4
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
public class XPluginImp implements Plugin {
|
public class XPluginImp implements Plugin {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -69,11 +67,6 @@ public class XPluginImp implements Plugin {
|
|||||||
SaManager.setSaTokenListener(bw.raw());
|
SaManager.setSaTokenListener(bw.raw());
|
||||||
});
|
});
|
||||||
|
|
||||||
// 注入框架行为 Bean
|
|
||||||
Aop.getAsyn(SaTokenAction.class, bw->{
|
|
||||||
SaManager.setSaTokenAction(bw.raw());
|
|
||||||
});
|
|
||||||
|
|
||||||
// 注入权限认证 Bean
|
// 注入权限认证 Bean
|
||||||
Aop.getAsyn(StpInterface.class, bw->{
|
Aop.getAsyn(StpInterface.class, bw->{
|
||||||
SaManager.setStpInterface(bw.raw());
|
SaManager.setStpInterface(bw.raw());
|
||||||
|
@@ -59,16 +59,6 @@ public class SaBeanInject {
|
|||||||
SaManager.setStpInterface(stpInterface);
|
SaManager.setStpInterface(stpInterface);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 注入框架行为Bean
|
|
||||||
*
|
|
||||||
* @param saTokenAction SaTokenAction对象
|
|
||||||
*/
|
|
||||||
@Autowired(required = false)
|
|
||||||
public void setSaTokenAction(@SuppressWarnings("deprecation") cn.dev33.satoken.action.SaTokenAction saTokenAction) {
|
|
||||||
SaManager.setSaTokenAction(saTokenAction);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注入上下文Bean
|
* 注入上下文Bean
|
||||||
*
|
*
|
||||||
|
@@ -31,14 +31,14 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<version>2.0.0.RELEASE</version>
|
<version>2.5.12</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- config -->
|
<!-- config -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||||
<version>2.0.0.RELEASE</version>
|
<version>2.5.12</version>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
@@ -1,9 +1,7 @@
|
|||||||
package cn.dev33.satoken.context.model;
|
package cn.dev33.satoken.context.model;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SaFoxUtil 工具类测试
|
* SaFoxUtil 工具类测试
|
||||||
@@ -11,7 +9,6 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
* @author kong
|
* @author kong
|
||||||
* @date: 2022-2-8 22:14:25
|
* @date: 2022-2-8 22:14:25
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
public class SaCookieTest {
|
public class SaCookieTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -24,7 +21,7 @@ public class SaCookieTest {
|
|||||||
.setHttpOnly(true)
|
.setHttpOnly(true)
|
||||||
.setSecure(true);
|
.setSecure(true);
|
||||||
|
|
||||||
Assert.assertEquals(cookie.toHeaderValue(), "satoken=xxxx-xxxx-xxxx-xxxx; Domain=https://sa-token.dev33.cn/; Path=/; Secure; HttpOnly; sameSite=Lax");
|
Assertions.assertEquals(cookie.toHeaderValue(), "satoken=xxxx-xxxx-xxxx-xxxx; Domain=https://sa-token.dev33.cn/; Path=/; Secure; HttpOnly; sameSite=Lax");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,7 @@
|
|||||||
package cn.dev33.satoken.dao;
|
package cn.dev33.satoken.dao;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
import cn.dev33.satoken.session.SaSession;
|
import cn.dev33.satoken.session.SaSession;
|
||||||
|
|
||||||
@@ -13,7 +11,6 @@ import cn.dev33.satoken.session.SaSession;
|
|||||||
* @author kong
|
* @author kong
|
||||||
* @date: 2022-2-9 15:39:38
|
* @date: 2022-2-9 15:39:38
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
public class SaTokenDaoTest {
|
public class SaTokenDaoTest {
|
||||||
|
|
||||||
SaTokenDao dao = new SaTokenDaoDefaultImpl();
|
SaTokenDao dao = new SaTokenDaoDefaultImpl();
|
||||||
@@ -21,34 +18,34 @@ public class SaTokenDaoTest {
|
|||||||
@Test
|
@Test
|
||||||
public void get() {
|
public void get() {
|
||||||
dao.set("name", "zhangsan", 60);
|
dao.set("name", "zhangsan", 60);
|
||||||
Assert.assertEquals(dao.get("name"), "zhangsan");
|
Assertions.assertEquals(dao.get("name"), "zhangsan");
|
||||||
Assert.assertTrue(dao.getTimeout("name") <= 60);
|
Assertions.assertTrue(dao.getTimeout("name") <= 60);
|
||||||
Assert.assertEquals(dao.getTimeout("name2"), -2);
|
Assertions.assertEquals(dao.getTimeout("name2"), -2);
|
||||||
|
|
||||||
dao.update("name", "lisi");
|
dao.update("name", "lisi");
|
||||||
Assert.assertEquals(dao.get("name"), "lisi");
|
Assertions.assertEquals(dao.get("name"), "lisi");
|
||||||
|
|
||||||
dao.updateTimeout("name", 100);
|
dao.updateTimeout("name", 100);
|
||||||
Assert.assertTrue(dao.getTimeout("name") <= 100);
|
Assertions.assertTrue(dao.getTimeout("name") <= 100);
|
||||||
|
|
||||||
dao.delete("name");
|
dao.delete("name");
|
||||||
Assert.assertEquals(dao.get("name"), null);
|
Assertions.assertEquals(dao.get("name"), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getObject() {
|
public void getObject() {
|
||||||
dao.setObject("name", "zhangsan", 60);
|
dao.setObject("name", "zhangsan", 60);
|
||||||
Assert.assertEquals(dao.getObject("name"), "zhangsan");
|
Assertions.assertEquals(dao.getObject("name"), "zhangsan");
|
||||||
Assert.assertTrue(dao.getObjectTimeout("name") <= 60);
|
Assertions.assertTrue(dao.getObjectTimeout("name") <= 60);
|
||||||
|
|
||||||
dao.updateObject("name", "lisi");
|
dao.updateObject("name", "lisi");
|
||||||
Assert.assertEquals(dao.getObject("name"), "lisi");
|
Assertions.assertEquals(dao.getObject("name"), "lisi");
|
||||||
|
|
||||||
dao.updateObjectTimeout("name", 100);
|
dao.updateObjectTimeout("name", 100);
|
||||||
Assert.assertTrue(dao.getObjectTimeout("name") <= 100);
|
Assertions.assertTrue(dao.getObjectTimeout("name") <= 100);
|
||||||
|
|
||||||
dao.deleteObject("name");
|
dao.deleteObject("name");
|
||||||
Assert.assertEquals(dao.getObject("name"), null);
|
Assertions.assertEquals(dao.getObject("name"), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -56,18 +53,18 @@ public class SaTokenDaoTest {
|
|||||||
SaSession session = new SaSession("session-1001");
|
SaSession session = new SaSession("session-1001");
|
||||||
|
|
||||||
dao.setSession(session, 60);
|
dao.setSession(session, 60);
|
||||||
Assert.assertEquals(dao.getSession("session-1001").getId(), session.getId());
|
Assertions.assertEquals(dao.getSession("session-1001").getId(), session.getId());
|
||||||
Assert.assertTrue(dao.getSessionTimeout("session-1001") <= 60);
|
Assertions.assertTrue(dao.getSessionTimeout("session-1001") <= 60);
|
||||||
|
|
||||||
SaSession session2 = new SaSession("session-1001");
|
SaSession session2 = new SaSession("session-1001");
|
||||||
dao.updateSession(session2);
|
dao.updateSession(session2);
|
||||||
Assert.assertEquals(dao.getSession("session-1001").getId(), session2.getId());
|
Assertions.assertEquals(dao.getSession("session-1001").getId(), session2.getId());
|
||||||
|
|
||||||
dao.updateSessionTimeout("session-1001", 100);
|
dao.updateSessionTimeout("session-1001", 100);
|
||||||
Assert.assertTrue(dao.getSessionTimeout("session-1001") <= 100);
|
Assertions.assertTrue(dao.getSessionTimeout("session-1001") <= 100);
|
||||||
|
|
||||||
dao.deleteSession("session-1001");
|
dao.deleteSession("session-1001");
|
||||||
Assert.assertEquals(dao.getSession("session-1001"), null);
|
Assertions.assertEquals(dao.getSession("session-1001"), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,7 @@
|
|||||||
package cn.dev33.satoken.fun;
|
package cn.dev33.satoken.fun;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* IsRunFunction 测试
|
* IsRunFunction 测试
|
||||||
@@ -11,7 +9,6 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
* @author kong
|
* @author kong
|
||||||
* @date: 2022-2-9 16:11:10
|
* @date: 2022-2-9 16:11:10
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
public class IsRunFunctionTest {
|
public class IsRunFunctionTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -29,7 +26,7 @@ public class IsRunFunctionTest {
|
|||||||
obj.count = 3;
|
obj.count = 3;
|
||||||
});
|
});
|
||||||
|
|
||||||
Assert.assertEquals(obj.count, 2);
|
Assertions.assertEquals(obj.count, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,7 @@
|
|||||||
package cn.dev33.satoken.secure;
|
package cn.dev33.satoken.secure;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* BCrypt 加密测试
|
* BCrypt 加密测试
|
||||||
@@ -11,15 +9,14 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
* @author dream.
|
* @author dream.
|
||||||
* @date 2022/1/20
|
* @date 2022/1/20
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
public class BCryptTest {
|
public class BCryptTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkpwTest() {
|
public void checkpwTest() {
|
||||||
final String hashed = BCrypt.hashpw("12345");
|
final String hashed = BCrypt.hashpw("12345");
|
||||||
// System.out.println(hashed);
|
// System.out.println(hashed);
|
||||||
Assert.assertTrue(BCrypt.checkpw("12345", hashed));
|
Assertions.assertTrue(BCrypt.checkpw("12345", hashed));
|
||||||
Assert.assertFalse(BCrypt.checkpw("123456", hashed));
|
Assertions.assertFalse(BCrypt.checkpw("123456", hashed));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@@ -1,9 +1,7 @@
|
|||||||
package cn.dev33.satoken.secure;
|
package cn.dev33.satoken.secure;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SaBase64Util 测试
|
* SaBase64Util 测试
|
||||||
@@ -11,7 +9,6 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
* @author kong
|
* @author kong
|
||||||
* @date: 2022-2-9
|
* @date: 2022-2-9
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
public class SaBase64UtilTest {
|
public class SaBase64UtilTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -21,11 +18,11 @@ public class SaBase64UtilTest {
|
|||||||
|
|
||||||
// 使用Base64编码
|
// 使用Base64编码
|
||||||
String base64Text = SaBase64Util.encode(text);
|
String base64Text = SaBase64Util.encode(text);
|
||||||
Assert.assertEquals(base64Text, "U2EtVG9rZW4g5LiA5Liq6L276YeP57qnamF2Yeadg+mZkOiupOivgeahhuaetg==");
|
Assertions.assertEquals(base64Text, "U2EtVG9rZW4g5LiA5Liq6L276YeP57qnamF2Yeadg+mZkOiupOivgeahhuaetg==");
|
||||||
|
|
||||||
// 使用Base64解码
|
// 使用Base64解码
|
||||||
String text2 = SaBase64Util.decode(base64Text);
|
String text2 = SaBase64Util.decode(base64Text);
|
||||||
Assert.assertEquals(text2, "Sa-Token 一个轻量级java权限认证框架");
|
Assertions.assertEquals(text2, "Sa-Token 一个轻量级java权限认证框架");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,7 @@
|
|||||||
package cn.dev33.satoken.secure;
|
package cn.dev33.satoken.secure;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SaSecureUtil 加密工具类 测试
|
* SaSecureUtil 加密工具类 测试
|
||||||
@@ -11,23 +9,22 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
* @author kong
|
* @author kong
|
||||||
* @date: 2022-2-9
|
* @date: 2022-2-9
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
public class SaSecureUtilTest {
|
public class SaSecureUtilTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
|
|
||||||
// md5加密
|
// md5加密
|
||||||
Assert.assertEquals(SaSecureUtil.md5("123456"), "e10adc3949ba59abbe56e057f20f883e");
|
Assertions.assertEquals(SaSecureUtil.md5("123456"), "e10adc3949ba59abbe56e057f20f883e");
|
||||||
|
|
||||||
// sha1加密
|
// sha1加密
|
||||||
Assert.assertEquals(SaSecureUtil.sha1("123456"), "7c4a8d09ca3762af61e59520943dc26494f8941b");
|
Assertions.assertEquals(SaSecureUtil.sha1("123456"), "7c4a8d09ca3762af61e59520943dc26494f8941b");
|
||||||
|
|
||||||
// sha256加密
|
// sha256加密
|
||||||
Assert.assertEquals(SaSecureUtil.sha256("123456"), "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92");
|
Assertions.assertEquals(SaSecureUtil.sha256("123456"), "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92");
|
||||||
|
|
||||||
// md5加盐加密: md5(md5(str) + md5(salt))
|
// md5加盐加密: md5(md5(str) + md5(salt))
|
||||||
Assert.assertEquals(SaSecureUtil.md5BySalt("123456", "salt"), "f52020dca765fd3943ed40a615dc2c5c");
|
Assertions.assertEquals(SaSecureUtil.md5BySalt("123456", "salt"), "f52020dca765fd3943ed40a615dc2c5c");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,11 +36,11 @@ public class SaSecureUtilTest {
|
|||||||
|
|
||||||
// 加密
|
// 加密
|
||||||
String ciphertext = SaSecureUtil.aesEncrypt(key, text);
|
String ciphertext = SaSecureUtil.aesEncrypt(key, text);
|
||||||
Assert.assertEquals(ciphertext, "KmSqfwxY5BRuWoHMWJqtebcOZ2lEEZaj2OSi1Ei8pRx4zdi24wsnwsTQVjbXRQ0M");
|
Assertions.assertEquals(ciphertext, "KmSqfwxY5BRuWoHMWJqtebcOZ2lEEZaj2OSi1Ei8pRx4zdi24wsnwsTQVjbXRQ0M");
|
||||||
|
|
||||||
// 解密
|
// 解密
|
||||||
String text2 = SaSecureUtil.aesDecrypt(key, ciphertext);
|
String text2 = SaSecureUtil.aesDecrypt(key, ciphertext);
|
||||||
Assert.assertEquals(text2, "Sa-Token 一个轻量级java权限认证框架");
|
Assertions.assertEquals(text2, "Sa-Token 一个轻量级java权限认证框架");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -61,7 +58,7 @@ public class SaSecureUtilTest {
|
|||||||
|
|
||||||
// 使用私钥解密
|
// 使用私钥解密
|
||||||
String text2 = SaSecureUtil.rsaDecryptByPrivate(privateKey, ciphertext);
|
String text2 = SaSecureUtil.rsaDecryptByPrivate(privateKey, ciphertext);
|
||||||
Assert.assertEquals(text2, "Sa-Token 一个轻量级java权限认证框架");
|
Assertions.assertEquals(text2, "Sa-Token 一个轻量级java权限认证框架");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,7 @@
|
|||||||
package cn.dev33.satoken.session;
|
package cn.dev33.satoken.session;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SaSession 测试
|
* SaSession 测试
|
||||||
@@ -11,21 +9,20 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
* @author kong
|
* @author kong
|
||||||
* @date: 2022-2-9
|
* @date: 2022-2-9
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
public class SaSessionTest {
|
public class SaSessionTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
SaSession session = new SaSession("session-1001");
|
SaSession session = new SaSession("session-1001");
|
||||||
Assert.assertEquals(session.getId(), "session-1001");
|
Assertions.assertEquals(session.getId(), "session-1001");
|
||||||
|
|
||||||
// 基础取值
|
// 基础取值
|
||||||
session.set("name", "zhangsan");
|
session.set("name", "zhangsan");
|
||||||
session.set("age", 18);
|
session.set("age", 18);
|
||||||
Assert.assertEquals(session.get("name"), "zhangsan");
|
Assertions.assertEquals(session.get("name"), "zhangsan");
|
||||||
Assert.assertEquals((int)session.get("age", 20), 18);
|
Assertions.assertEquals((int)session.get("age", 20), 18);
|
||||||
Assert.assertEquals((int)session.get("age2", 20), 20);
|
Assertions.assertEquals((int)session.get("age2", 20), 20);
|
||||||
Assert.assertEquals(session.getModel("age", Double.class).getClass(), Double.class);
|
Assertions.assertEquals(session.getModel("age", Double.class).getClass(), Double.class);
|
||||||
|
|
||||||
// 复杂取值
|
// 复杂取值
|
||||||
class User {
|
class User {
|
||||||
@@ -40,20 +37,20 @@ public class SaSessionTest {
|
|||||||
session.set("user", user);
|
session.set("user", user);
|
||||||
|
|
||||||
User user2 = session.getModel("user", User.class);
|
User user2 = session.getModel("user", User.class);
|
||||||
Assert.assertNotNull(user2);
|
Assertions.assertNotNull(user2);
|
||||||
Assert.assertEquals(user2.name, "zhangsan");
|
Assertions.assertEquals(user2.name, "zhangsan");
|
||||||
Assert.assertEquals(user2.age, 18);
|
Assertions.assertEquals(user2.age, 18);
|
||||||
|
|
||||||
// Token签名
|
// Token签名
|
||||||
session.addTokenSign("xxxx-xxxx-xxxx-xxxx-1", "PC");
|
session.addTokenSign("xxxx-xxxx-xxxx-xxxx-1", "PC");
|
||||||
session.addTokenSign("xxxx-xxxx-xxxx-xxxx-2", "APP");
|
session.addTokenSign("xxxx-xxxx-xxxx-xxxx-2", "APP");
|
||||||
|
|
||||||
Assert.assertEquals(session.getTokenSignList().size(), 2);
|
Assertions.assertEquals(session.getTokenSignList().size(), 2);
|
||||||
Assert.assertEquals(session.getTokenSign("xxxx-xxxx-xxxx-xxxx-1").getDevice(), "PC");
|
Assertions.assertEquals(session.getTokenSign("xxxx-xxxx-xxxx-xxxx-1").getDevice(), "PC");
|
||||||
Assert.assertEquals(session.getTokenSign("xxxx-xxxx-xxxx-xxxx-2").getDevice(), "APP");
|
Assertions.assertEquals(session.getTokenSign("xxxx-xxxx-xxxx-xxxx-2").getDevice(), "APP");
|
||||||
|
|
||||||
session.removeTokenSign("xxxx-xxxx-xxxx-xxxx-1");
|
session.removeTokenSign("xxxx-xxxx-xxxx-xxxx-1");
|
||||||
Assert.assertEquals(session.getTokenSignList().size(), 1);
|
Assertions.assertEquals(session.getTokenSignList().size(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,14 +1,11 @@
|
|||||||
package cn.dev33.satoken.util;
|
package cn.dev33.satoken.util;
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SaFoxUtil 工具类测试
|
* SaFoxUtil 工具类测试
|
||||||
@@ -16,33 +13,32 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
* @author kong
|
* @author kong
|
||||||
* @date: 2022-2-8 22:14:25
|
* @date: 2022-2-8 22:14:25
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
public class SaFoxUtilTest {
|
public class SaFoxUtilTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getRandomString() {
|
public void getRandomString() {
|
||||||
String randomString = SaFoxUtil.getRandomString(8);
|
String randomString = SaFoxUtil.getRandomString(8);
|
||||||
Assert.assertEquals(randomString.length(), 8);
|
Assertions.assertEquals(randomString.length(), 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isEmpty() {
|
public void isEmpty() {
|
||||||
Assert.assertFalse(SaFoxUtil.isEmpty("abc"));
|
Assertions.assertFalse(SaFoxUtil.isEmpty("abc"));
|
||||||
Assert.assertTrue(SaFoxUtil.isEmpty(""));
|
Assertions.assertTrue(SaFoxUtil.isEmpty(""));
|
||||||
Assert.assertTrue(SaFoxUtil.isEmpty(null));
|
Assertions.assertTrue(SaFoxUtil.isEmpty(null));
|
||||||
|
|
||||||
Assert.assertTrue(SaFoxUtil.isNotEmpty("abc"));
|
Assertions.assertTrue(SaFoxUtil.isNotEmpty("abc"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getMarking28() {
|
public void getMarking28() {
|
||||||
Assert.assertNotEquals(SaFoxUtil.getMarking28(), SaFoxUtil.getMarking28());
|
Assertions.assertNotEquals(SaFoxUtil.getMarking28(), SaFoxUtil.getMarking28());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void formatDate() {
|
public void formatDate() {
|
||||||
String formatDate = SaFoxUtil.formatDate(new Date(1644328600364L));
|
String formatDate = SaFoxUtil.formatDate(new Date(1644328600364L));
|
||||||
Assert.assertEquals(formatDate, "2022-02-08 21:56:40");
|
Assertions.assertEquals(formatDate, "2022-02-08 21:56:40");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -52,154 +48,154 @@ public class SaFoxUtilTest {
|
|||||||
|
|
||||||
// 分页
|
// 分页
|
||||||
List<String> list1 = SaFoxUtil.searchList(dataList, 1, 2);
|
List<String> list1 = SaFoxUtil.searchList(dataList, 1, 2);
|
||||||
Assert.assertEquals(list1.size(), 2);
|
Assertions.assertEquals(list1.size(), 2);
|
||||||
Assert.assertEquals(list1.get(0), "token2");
|
Assertions.assertEquals(list1.get(0), "token2");
|
||||||
Assert.assertEquals(list1.get(1), "token3");
|
Assertions.assertEquals(list1.get(1), "token3");
|
||||||
|
|
||||||
// 前缀筛选
|
// 前缀筛选
|
||||||
List<String> list2 = SaFoxUtil.searchList(dataList, "token", "", 0, 10);
|
List<String> list2 = SaFoxUtil.searchList(dataList, "token", "", 0, 10);
|
||||||
Assert.assertEquals(list2.size(), 5);
|
Assertions.assertEquals(list2.size(), 5);
|
||||||
|
|
||||||
// 关键字筛选
|
// 关键字筛选
|
||||||
List<String> list3 = SaFoxUtil.searchList(dataList, "", "1", 0, 10);
|
List<String> list3 = SaFoxUtil.searchList(dataList, "", "1", 0, 10);
|
||||||
Assert.assertEquals(list3.size(), 2);
|
Assertions.assertEquals(list3.size(), 2);
|
||||||
|
|
||||||
// 综合筛选
|
// 综合筛选
|
||||||
List<String> list4 = SaFoxUtil.searchList(dataList, "token", "1", 0, 10);
|
List<String> list4 = SaFoxUtil.searchList(dataList, "token", "1", 0, 10);
|
||||||
Assert.assertEquals(list4.size(), 1);
|
Assertions.assertEquals(list4.size(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void vagueMatch() {
|
public void vagueMatch() {
|
||||||
Assert.assertTrue(SaFoxUtil.vagueMatch("hello*", "hello"));
|
Assertions.assertTrue(SaFoxUtil.vagueMatch("hello*", "hello"));
|
||||||
Assert.assertTrue(SaFoxUtil.vagueMatch("hello*", "hello world"));
|
Assertions.assertTrue(SaFoxUtil.vagueMatch("hello*", "hello world"));
|
||||||
Assert.assertFalse(SaFoxUtil.vagueMatch("hello*", "he"));
|
Assertions.assertFalse(SaFoxUtil.vagueMatch("hello*", "he"));
|
||||||
Assert.assertTrue(SaFoxUtil.vagueMatch("hello*", "hello*"));
|
Assertions.assertTrue(SaFoxUtil.vagueMatch("hello*", "hello*"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getValueByType() {
|
public void getValueByType() {
|
||||||
Assert.assertEquals(SaFoxUtil.getValueByType("1", Integer.class).getClass(), Integer.class);
|
Assertions.assertEquals(SaFoxUtil.getValueByType("1", Integer.class).getClass(), Integer.class);
|
||||||
Assert.assertEquals(SaFoxUtil.getValueByType("1", Long.class).getClass(), Long.class);
|
Assertions.assertEquals(SaFoxUtil.getValueByType("1", Long.class).getClass(), Long.class);
|
||||||
Assert.assertEquals(SaFoxUtil.getValueByType("1", Double.class).getClass(), Double.class);
|
Assertions.assertEquals(SaFoxUtil.getValueByType("1", Double.class).getClass(), Double.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void joinParam() {
|
public void joinParam() {
|
||||||
Assert.assertEquals(SaFoxUtil.joinParam("https://sa-token.dev33.cn", "id=1"), "https://sa-token.dev33.cn?id=1");
|
Assertions.assertEquals(SaFoxUtil.joinParam("https://sa-token.dev33.cn", "id=1"), "https://sa-token.dev33.cn?id=1");
|
||||||
Assert.assertEquals(SaFoxUtil.joinParam("https://sa-token.dev33.cn?", "id=1"), "https://sa-token.dev33.cn?id=1");
|
Assertions.assertEquals(SaFoxUtil.joinParam("https://sa-token.dev33.cn?", "id=1"), "https://sa-token.dev33.cn?id=1");
|
||||||
Assert.assertEquals(SaFoxUtil.joinParam("https://sa-token.dev33.cn?name=zhang", "id=1"), "https://sa-token.dev33.cn?name=zhang&id=1");
|
Assertions.assertEquals(SaFoxUtil.joinParam("https://sa-token.dev33.cn?name=zhang", "id=1"), "https://sa-token.dev33.cn?name=zhang&id=1");
|
||||||
Assert.assertEquals(SaFoxUtil.joinParam("https://sa-token.dev33.cn?name=zhang&", "id=1"), "https://sa-token.dev33.cn?name=zhang&id=1");
|
Assertions.assertEquals(SaFoxUtil.joinParam("https://sa-token.dev33.cn?name=zhang&", "id=1"), "https://sa-token.dev33.cn?name=zhang&id=1");
|
||||||
|
|
||||||
Assert.assertEquals(SaFoxUtil.joinParam("https://sa-token.dev33.cn?name=zhang&", "id", 1), "https://sa-token.dev33.cn?name=zhang&id=1");
|
Assertions.assertEquals(SaFoxUtil.joinParam("https://sa-token.dev33.cn?name=zhang&", "id", 1), "https://sa-token.dev33.cn?name=zhang&id=1");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void joinSharpParam() {
|
public void joinSharpParam() {
|
||||||
Assert.assertEquals(SaFoxUtil.joinSharpParam("https://sa-token.dev33.cn", "id=1"), "https://sa-token.dev33.cn#id=1");
|
Assertions.assertEquals(SaFoxUtil.joinSharpParam("https://sa-token.dev33.cn", "id=1"), "https://sa-token.dev33.cn#id=1");
|
||||||
Assert.assertEquals(SaFoxUtil.joinSharpParam("https://sa-token.dev33.cn#", "id=1"), "https://sa-token.dev33.cn#id=1");
|
Assertions.assertEquals(SaFoxUtil.joinSharpParam("https://sa-token.dev33.cn#", "id=1"), "https://sa-token.dev33.cn#id=1");
|
||||||
Assert.assertEquals(SaFoxUtil.joinSharpParam("https://sa-token.dev33.cn#name=zhang", "id=1"), "https://sa-token.dev33.cn#name=zhang&id=1");
|
Assertions.assertEquals(SaFoxUtil.joinSharpParam("https://sa-token.dev33.cn#name=zhang", "id=1"), "https://sa-token.dev33.cn#name=zhang&id=1");
|
||||||
Assert.assertEquals(SaFoxUtil.joinSharpParam("https://sa-token.dev33.cn#name=zhang&", "id=1"), "https://sa-token.dev33.cn#name=zhang&id=1");
|
Assertions.assertEquals(SaFoxUtil.joinSharpParam("https://sa-token.dev33.cn#name=zhang&", "id=1"), "https://sa-token.dev33.cn#name=zhang&id=1");
|
||||||
|
|
||||||
Assert.assertEquals(SaFoxUtil.joinSharpParam("https://sa-token.dev33.cn#name=zhang&", "id", 1), "https://sa-token.dev33.cn#name=zhang&id=1");
|
Assertions.assertEquals(SaFoxUtil.joinSharpParam("https://sa-token.dev33.cn#name=zhang&", "id", 1), "https://sa-token.dev33.cn#name=zhang&id=1");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void arrayJoin() {
|
public void arrayJoin() {
|
||||||
Assert.assertEquals(SaFoxUtil.arrayJoin(new String[] {"a", "b", "c"}), "a,b,c");
|
Assertions.assertEquals(SaFoxUtil.arrayJoin(new String[] {"a", "b", "c"}), "a,b,c");
|
||||||
Assert.assertEquals(SaFoxUtil.arrayJoin(new String[] {}), "");
|
Assertions.assertEquals(SaFoxUtil.arrayJoin(new String[] {}), "");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isUrl() {
|
public void isUrl() {
|
||||||
Assert.assertTrue(SaFoxUtil.isUrl("https://sa-token.dev33.cn"));
|
Assertions.assertTrue(SaFoxUtil.isUrl("https://sa-token.dev33.cn"));
|
||||||
Assert.assertTrue(SaFoxUtil.isUrl("https://www.baidu.com/"));
|
Assertions.assertTrue(SaFoxUtil.isUrl("https://www.baidu.com/"));
|
||||||
|
|
||||||
Assert.assertFalse(SaFoxUtil.isUrl("htt://www.baidu.com/"));
|
Assertions.assertFalse(SaFoxUtil.isUrl("htt://www.baidu.com/"));
|
||||||
Assert.assertFalse(SaFoxUtil.isUrl("https:www.baidu.com/"));
|
Assertions.assertFalse(SaFoxUtil.isUrl("https:www.baidu.com/"));
|
||||||
Assert.assertFalse(SaFoxUtil.isUrl("httpswwwbaiducom/"));
|
Assertions.assertFalse(SaFoxUtil.isUrl("httpswwwbaiducom/"));
|
||||||
Assert.assertFalse(SaFoxUtil.isUrl("https://www.baidu.com/,"));
|
Assertions.assertFalse(SaFoxUtil.isUrl("https://www.baidu.com/,"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void encodeUrl() {
|
public void encodeUrl() {
|
||||||
Assert.assertEquals(SaFoxUtil.encodeUrl("https://sa-token.dev33.cn"), "https%3A%2F%2Fsa-token.dev33.cn");
|
Assertions.assertEquals(SaFoxUtil.encodeUrl("https://sa-token.dev33.cn"), "https%3A%2F%2Fsa-token.dev33.cn");
|
||||||
Assert.assertEquals(SaFoxUtil.decoderUrl("https%3A%2F%2Fsa-token.dev33.cn"), "https://sa-token.dev33.cn");
|
Assertions.assertEquals(SaFoxUtil.decoderUrl("https%3A%2F%2Fsa-token.dev33.cn"), "https://sa-token.dev33.cn");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertStringToList() {
|
public void convertStringToList() {
|
||||||
List<String> list = SaFoxUtil.convertStringToList("a,b,c");
|
List<String> list = SaFoxUtil.convertStringToList("a,b,c");
|
||||||
Assert.assertEquals(list.size(), 3);
|
Assertions.assertEquals(list.size(), 3);
|
||||||
Assert.assertEquals(list.get(0), "a");
|
Assertions.assertEquals(list.get(0), "a");
|
||||||
Assert.assertEquals(list.get(1), "b");
|
Assertions.assertEquals(list.get(1), "b");
|
||||||
Assert.assertEquals(list.get(2), "c");
|
Assertions.assertEquals(list.get(2), "c");
|
||||||
|
|
||||||
List<String> list2 = SaFoxUtil.convertStringToList("a,");
|
List<String> list2 = SaFoxUtil.convertStringToList("a,");
|
||||||
Assert.assertEquals(list2.size(), 1);
|
Assertions.assertEquals(list2.size(), 1);
|
||||||
|
|
||||||
List<String> list3 = SaFoxUtil.convertStringToList(",");
|
List<String> list3 = SaFoxUtil.convertStringToList(",");
|
||||||
Assert.assertEquals(list3.size(), 0);
|
Assertions.assertEquals(list3.size(), 0);
|
||||||
|
|
||||||
List<String> list4 = SaFoxUtil.convertStringToList("");
|
List<String> list4 = SaFoxUtil.convertStringToList("");
|
||||||
Assert.assertEquals(list4.size(), 0);
|
Assertions.assertEquals(list4.size(), 0);
|
||||||
|
|
||||||
List<String> list5 = SaFoxUtil.convertStringToList(null);
|
List<String> list5 = SaFoxUtil.convertStringToList(null);
|
||||||
Assert.assertEquals(list5.size(), 0);
|
Assertions.assertEquals(list5.size(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertListToString() {
|
public void convertListToString() {
|
||||||
List<String> list = Arrays.asList("a", "b", "c");
|
List<String> list = Arrays.asList("a", "b", "c");
|
||||||
Assert.assertEquals(SaFoxUtil.convertListToString(list), "a,b,c");
|
Assertions.assertEquals(SaFoxUtil.convertListToString(list), "a,b,c");
|
||||||
|
|
||||||
List<String> list2 = Arrays.asList();
|
List<String> list2 = Arrays.asList();
|
||||||
Assert.assertEquals(SaFoxUtil.convertListToString(list2), "");
|
Assertions.assertEquals(SaFoxUtil.convertListToString(list2), "");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertStringToArray() {
|
public void convertStringToArray() {
|
||||||
String[] array = SaFoxUtil.convertStringToArray("a,b,c");
|
String[] array = SaFoxUtil.convertStringToArray("a,b,c");
|
||||||
Assert.assertEquals(array.length, 3);
|
Assertions.assertEquals(array.length, 3);
|
||||||
Assert.assertEquals(array[0], "a");
|
Assertions.assertEquals(array[0], "a");
|
||||||
Assert.assertEquals(array[1], "b");
|
Assertions.assertEquals(array[1], "b");
|
||||||
Assert.assertEquals(array[2], "c");
|
Assertions.assertEquals(array[2], "c");
|
||||||
|
|
||||||
String[] array2 = SaFoxUtil.convertStringToArray("a,");
|
String[] array2 = SaFoxUtil.convertStringToArray("a,");
|
||||||
Assert.assertEquals(array2.length, 1);
|
Assertions.assertEquals(array2.length, 1);
|
||||||
|
|
||||||
String[] array3 = SaFoxUtil.convertStringToArray(",");
|
String[] array3 = SaFoxUtil.convertStringToArray(",");
|
||||||
Assert.assertEquals(array3.length, 0);
|
Assertions.assertEquals(array3.length, 0);
|
||||||
|
|
||||||
String[] array4 = SaFoxUtil.convertStringToArray("");
|
String[] array4 = SaFoxUtil.convertStringToArray("");
|
||||||
Assert.assertEquals(array4.length, 0);
|
Assertions.assertEquals(array4.length, 0);
|
||||||
|
|
||||||
String[] array5 = SaFoxUtil.convertStringToArray(null);
|
String[] array5 = SaFoxUtil.convertStringToArray(null);
|
||||||
Assert.assertEquals(array5.length, 0);
|
Assertions.assertEquals(array5.length, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertArrayToString() {
|
public void convertArrayToString() {
|
||||||
String[] array = new String[] {"a", "b", "c"};
|
String[] array = new String[] {"a", "b", "c"};
|
||||||
Assert.assertEquals(SaFoxUtil.convertArrayToString(array), "a,b,c");
|
Assertions.assertEquals(SaFoxUtil.convertArrayToString(array), "a,b,c");
|
||||||
|
|
||||||
String[] array2 = new String[] {};
|
String[] array2 = new String[] {};
|
||||||
Assert.assertEquals(SaFoxUtil.convertArrayToString(array2), "");
|
Assertions.assertEquals(SaFoxUtil.convertArrayToString(array2), "");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void emptyList() {
|
public void emptyList() {
|
||||||
List<String> list = SaFoxUtil.emptyList();
|
List<String> list = SaFoxUtil.emptyList();
|
||||||
Assert.assertEquals(list.size(), 0);
|
Assertions.assertEquals(list.size(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toList() {
|
public void toList() {
|
||||||
List<String> list = SaFoxUtil.toList("a","b", "c");
|
List<String> list = SaFoxUtil.toList("a","b", "c");
|
||||||
Assert.assertEquals(list.size(), 3);
|
Assertions.assertEquals(list.size(), 3);
|
||||||
Assert.assertEquals(list.get(0), "a");
|
Assertions.assertEquals(list.get(0), "a");
|
||||||
Assert.assertEquals(list.get(1), "b");
|
Assertions.assertEquals(list.get(1), "b");
|
||||||
Assert.assertEquals(list.get(2), "c");
|
Assertions.assertEquals(list.get(2), "c");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,7 @@
|
|||||||
package cn.dev33.satoken.util;
|
package cn.dev33.satoken.util;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SaResult 结果集 测试
|
* SaResult 结果集 测试
|
||||||
@@ -11,26 +9,25 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
* @author kong
|
* @author kong
|
||||||
* @date: 2022-2-8 22:14:25
|
* @date: 2022-2-8 22:14:25
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
public class SaResultTest {
|
public class SaResultTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
SaResult res = new SaResult(200, "ok", "zhangsan");
|
SaResult res = new SaResult(200, "ok", "zhangsan");
|
||||||
Assert.assertEquals((int)res.getCode(), 200);
|
Assertions.assertEquals((int)res.getCode(), 200);
|
||||||
Assert.assertEquals(res.getMsg(), "ok");
|
Assertions.assertEquals(res.getMsg(), "ok");
|
||||||
Assert.assertEquals(res.getData(), "zhangsan");
|
Assertions.assertEquals(res.getData(), "zhangsan");
|
||||||
|
|
||||||
res.set("age", 18);
|
res.set("age", 18);
|
||||||
Assert.assertEquals(res.get("age"), 18);
|
Assertions.assertEquals(res.get("age"), 18);
|
||||||
Assert.assertEquals(res.getOrDefault("age", 20), 18);
|
Assertions.assertEquals(res.getOrDefault("age", 20), 18);
|
||||||
Assert.assertEquals(res.getOrDefault("age2", 20), 20);
|
Assertions.assertEquals(res.getOrDefault("age2", 20), 20);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test2() {
|
public void test2() {
|
||||||
Assert.assertEquals((int)SaResult.ok().getCode(), 200);
|
Assertions.assertEquals((int)SaResult.ok().getCode(), 200);
|
||||||
Assert.assertEquals((int)SaResult.error().getCode(), 500);
|
Assertions.assertEquals((int)SaResult.error().getCode(), 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -2,14 +2,12 @@ package com.pj.test;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.AfterClass;
|
import org.junit.jupiter.api.AfterAll;
|
||||||
import org.junit.Assert;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
import cn.dev33.satoken.SaManager;
|
import cn.dev33.satoken.SaManager;
|
||||||
import cn.dev33.satoken.dao.SaTokenDao;
|
import cn.dev33.satoken.dao.SaTokenDao;
|
||||||
@@ -29,7 +27,6 @@ import cn.hutool.jwt.JWT;
|
|||||||
* @author kong
|
* @author kong
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(classes = StartUpApplication.class)
|
@SpringBootTest(classes = StartUpApplication.class)
|
||||||
public class JwtForMixTest {
|
public class JwtForMixTest {
|
||||||
|
|
||||||
@@ -38,14 +35,14 @@ public class JwtForMixTest {
|
|||||||
SaTokenDao dao = SaManager.getSaTokenDao();
|
SaTokenDao dao = SaManager.getSaTokenDao();
|
||||||
|
|
||||||
// 开始
|
// 开始
|
||||||
@BeforeClass
|
@BeforeAll
|
||||||
public static void beforeClass() {
|
public static void beforeClass() {
|
||||||
System.out.println("\n\n------------------------ JwtForMixTest star ...");
|
System.out.println("\n\n------------------------ JwtForMixTest star ...");
|
||||||
StpUtil.setStpLogic(new StpLogicJwtForMix());
|
StpUtil.setStpLogic(new StpLogicJwtForMix());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 结束
|
// 结束
|
||||||
@AfterClass
|
@AfterAll
|
||||||
public static void afterClass() {
|
public static void afterClass() {
|
||||||
System.out.println("\n\n------------------------ JwtForMixTest end ... \n");
|
System.out.println("\n\n------------------------ JwtForMixTest end ... \n");
|
||||||
}
|
}
|
||||||
@@ -58,26 +55,26 @@ public class JwtForMixTest {
|
|||||||
String token = StpUtil.getTokenValue();
|
String token = StpUtil.getTokenValue();
|
||||||
|
|
||||||
// API 验证
|
// API 验证
|
||||||
Assert.assertTrue(StpUtil.isLogin());
|
Assertions.assertTrue(StpUtil.isLogin());
|
||||||
Assert.assertNotNull(token); // token不为null
|
Assertions.assertNotNull(token); // token不为null
|
||||||
Assert.assertEquals(StpUtil.getLoginIdAsLong(), 10001); // loginId=10001
|
Assertions.assertEquals(StpUtil.getLoginIdAsLong(), 10001); // loginId=10001
|
||||||
Assert.assertEquals(StpUtil.getLoginDevice(), SaTokenConsts.DEFAULT_LOGIN_DEVICE); // 登录设备类型
|
Assertions.assertEquals(StpUtil.getLoginDevice(), SaTokenConsts.DEFAULT_LOGIN_DEVICE); // 登录设备类型
|
||||||
|
|
||||||
// token 验证
|
// token 验证
|
||||||
JWT jwt = JWT.of(token);
|
JWT jwt = JWT.of(token);
|
||||||
JSONObject payloads = jwt.getPayloads();
|
JSONObject payloads = jwt.getPayloads();
|
||||||
Assert.assertEquals(payloads.getStr(SaJwtUtil.LOGIN_ID), "10001"); // 账号
|
Assertions.assertEquals(payloads.getStr(SaJwtUtil.LOGIN_ID), "10001"); // 账号
|
||||||
Assert.assertEquals(payloads.getStr(SaJwtUtil.DEVICE), SaTokenConsts.DEFAULT_LOGIN_DEVICE); // 登录设备类型
|
Assertions.assertEquals(payloads.getStr(SaJwtUtil.DEVICE), SaTokenConsts.DEFAULT_LOGIN_DEVICE); // 登录设备类型
|
||||||
Assert.assertEquals(payloads.getStr(SaJwtUtil.LOGIN_TYPE), StpUtil.TYPE); // 账号类型
|
Assertions.assertEquals(payloads.getStr(SaJwtUtil.LOGIN_TYPE), StpUtil.TYPE); // 账号类型
|
||||||
|
|
||||||
// db数据 验证
|
// db数据 验证
|
||||||
// token不存在
|
// token不存在
|
||||||
Assert.assertNull(dao.get("satoken:login:token:" + token));
|
Assertions.assertNull(dao.get("satoken:login:token:" + token));
|
||||||
// Session 存在
|
// Session 存在
|
||||||
SaSession session = dao.getSession("satoken:login:session:" + 10001);
|
SaSession session = dao.getSession("satoken:login:session:" + 10001);
|
||||||
Assert.assertNotNull(session);
|
Assertions.assertNotNull(session);
|
||||||
Assert.assertEquals(session.getId(), "satoken:login:session:" + 10001);
|
Assertions.assertEquals(session.getId(), "satoken:login:session:" + 10001);
|
||||||
Assert.assertTrue(session.getTokenSignList().size() >= 1);
|
Assertions.assertTrue(session.getTokenSignList().size() >= 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:注销
|
// 测试:注销
|
||||||
@@ -86,13 +83,13 @@ public class JwtForMixTest {
|
|||||||
// 登录
|
// 登录
|
||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
String token = StpUtil.getTokenValue();
|
String token = StpUtil.getTokenValue();
|
||||||
Assert.assertEquals(JWT.of(token).getPayloads().getStr("loginId"), "10001");
|
Assertions.assertEquals(JWT.of(token).getPayloads().getStr("loginId"), "10001");
|
||||||
|
|
||||||
// 注销
|
// 注销
|
||||||
StpUtil.logout();
|
StpUtil.logout();
|
||||||
// token 应该被清除
|
// token 应该被清除
|
||||||
Assert.assertNull(StpUtil.getTokenValue());
|
Assertions.assertNull(StpUtil.getTokenValue());
|
||||||
Assert.assertFalse(StpUtil.isLogin());
|
Assertions.assertFalse(StpUtil.isLogin());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:Session会话
|
// 测试:Session会话
|
||||||
@@ -101,23 +98,23 @@ public class JwtForMixTest {
|
|||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
|
|
||||||
// API 应该可以获取 Session
|
// API 应该可以获取 Session
|
||||||
Assert.assertNotNull(StpUtil.getSession(false));
|
Assertions.assertNotNull(StpUtil.getSession(false));
|
||||||
|
|
||||||
// db中应该存在 Session
|
// db中应该存在 Session
|
||||||
SaSession session = dao.getSession("satoken:login:session:" + 10001);
|
SaSession session = dao.getSession("satoken:login:session:" + 10001);
|
||||||
Assert.assertNotNull(session);
|
Assertions.assertNotNull(session);
|
||||||
|
|
||||||
// 存取值
|
// 存取值
|
||||||
session.set("name", "zhang");
|
session.set("name", "zhang");
|
||||||
session.set("age", "18");
|
session.set("age", "18");
|
||||||
Assert.assertEquals(session.get("name"), "zhang");
|
Assertions.assertEquals(session.get("name"), "zhang");
|
||||||
Assert.assertEquals(session.getInt("age"), 18);
|
Assertions.assertEquals(session.getInt("age"), 18);
|
||||||
Assert.assertEquals((int)session.getModel("age", int.class), 18);
|
Assertions.assertEquals((int)session.getModel("age", int.class), 18);
|
||||||
Assert.assertEquals((int)session.get("age", 20), 18);
|
Assertions.assertEquals((int)session.get("age", 20), 18);
|
||||||
Assert.assertEquals((int)session.get("name2", 20), 20);
|
Assertions.assertEquals((int)session.get("name2", 20), 20);
|
||||||
Assert.assertEquals((int)session.get("name2", () -> 30), 30);
|
Assertions.assertEquals((int)session.get("name2", () -> 30), 30);
|
||||||
session.clear();
|
session.clear();
|
||||||
Assert.assertEquals(session.get("name"), null);
|
Assertions.assertEquals(session.get("name"), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:权限认证
|
// 测试:权限认证
|
||||||
@@ -126,17 +123,17 @@ public class JwtForMixTest {
|
|||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
|
|
||||||
// 权限认证
|
// 权限认证
|
||||||
Assert.assertTrue(StpUtil.hasPermission("user-add"));
|
Assertions.assertTrue(StpUtil.hasPermission("user-add"));
|
||||||
Assert.assertTrue(StpUtil.hasPermission("user-list"));
|
Assertions.assertTrue(StpUtil.hasPermission("user-list"));
|
||||||
Assert.assertTrue(StpUtil.hasPermission("user"));
|
Assertions.assertTrue(StpUtil.hasPermission("user"));
|
||||||
Assert.assertTrue(StpUtil.hasPermission("art-add"));
|
Assertions.assertTrue(StpUtil.hasPermission("art-add"));
|
||||||
Assert.assertFalse(StpUtil.hasPermission("get-user"));
|
Assertions.assertFalse(StpUtil.hasPermission("get-user"));
|
||||||
// and
|
// and
|
||||||
Assert.assertTrue(StpUtil.hasPermissionAnd("art-add", "art-get"));
|
Assertions.assertTrue(StpUtil.hasPermissionAnd("art-add", "art-get"));
|
||||||
Assert.assertFalse(StpUtil.hasPermissionAnd("art-add", "comment-add"));
|
Assertions.assertFalse(StpUtil.hasPermissionAnd("art-add", "comment-add"));
|
||||||
// or
|
// or
|
||||||
Assert.assertTrue(StpUtil.hasPermissionOr("art-add", "comment-add"));
|
Assertions.assertTrue(StpUtil.hasPermissionOr("art-add", "comment-add"));
|
||||||
Assert.assertFalse(StpUtil.hasPermissionOr("comment-add", "comment-delete"));
|
Assertions.assertFalse(StpUtil.hasPermissionOr("comment-add", "comment-delete"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:角色认证
|
// 测试:角色认证
|
||||||
@@ -145,39 +142,41 @@ public class JwtForMixTest {
|
|||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
|
|
||||||
// 角色认证
|
// 角色认证
|
||||||
Assert.assertTrue(StpUtil.hasRole("admin"));
|
Assertions.assertTrue(StpUtil.hasRole("admin"));
|
||||||
Assert.assertFalse(StpUtil.hasRole("teacher"));
|
Assertions.assertFalse(StpUtil.hasRole("teacher"));
|
||||||
// and
|
// and
|
||||||
Assert.assertTrue(StpUtil.hasRoleAnd("admin", "super-admin"));
|
Assertions.assertTrue(StpUtil.hasRoleAnd("admin", "super-admin"));
|
||||||
Assert.assertFalse(StpUtil.hasRoleAnd("admin", "ceo"));
|
Assertions.assertFalse(StpUtil.hasRoleAnd("admin", "ceo"));
|
||||||
// or
|
// or
|
||||||
Assert.assertTrue(StpUtil.hasRoleOr("admin", "ceo"));
|
Assertions.assertTrue(StpUtil.hasRoleOr("admin", "ceo"));
|
||||||
Assert.assertFalse(StpUtil.hasRoleOr("ceo", "cto"));
|
Assertions.assertFalse(StpUtil.hasRoleOr("ceo", "cto"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:根据token强制注销
|
// 测试:根据token强制注销
|
||||||
@Test(expected = ApiDisabledException.class)
|
@Test
|
||||||
public void testLogoutByToken() {
|
public void testLogoutByToken() {
|
||||||
|
Assertions.assertThrows(ApiDisabledException.class, () -> {
|
||||||
// 先登录上
|
// 先登录上
|
||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
Assert.assertTrue(StpUtil.isLogin());
|
Assertions.assertTrue(StpUtil.isLogin());
|
||||||
String token = StpUtil.getTokenValue();
|
String token = StpUtil.getTokenValue();
|
||||||
|
|
||||||
// 根据token注销
|
// 根据token注销
|
||||||
StpUtil.logoutByTokenValue(token);
|
StpUtil.logoutByTokenValue(token);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:根据账号id强制注销
|
// 测试:根据账号id强制注销
|
||||||
@Test(expected = ApiDisabledException.class)
|
@Test
|
||||||
public void testLogoutByLoginId() {
|
public void testLogoutByLoginId() {
|
||||||
|
Assertions.assertThrows(ApiDisabledException.class, () -> {
|
||||||
// 先登录上
|
// 先登录上
|
||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
Assert.assertTrue(StpUtil.isLogin());
|
Assertions.assertTrue(StpUtil.isLogin());
|
||||||
|
|
||||||
// 根据账号id注销
|
// 根据账号id注销
|
||||||
StpUtil.logout(10001);
|
StpUtil.logout(10001);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试Token-Session
|
// 测试Token-Session
|
||||||
@@ -189,34 +188,35 @@ public class JwtForMixTest {
|
|||||||
String token = StpUtil.getTokenValue();
|
String token = StpUtil.getTokenValue();
|
||||||
|
|
||||||
// 刚开始不存在
|
// 刚开始不存在
|
||||||
Assert.assertNull(StpUtil.stpLogic.getTokenSession(false));
|
Assertions.assertNull(StpUtil.stpLogic.getTokenSession(false));
|
||||||
SaSession session = dao.getSession("satoken:login:token-session:" + token);
|
SaSession session = dao.getSession("satoken:login:token-session:" + token);
|
||||||
Assert.assertNull(session);
|
Assertions.assertNull(session);
|
||||||
|
|
||||||
// 调用一次就存在了
|
// 调用一次就存在了
|
||||||
StpUtil.getTokenSession();
|
StpUtil.getTokenSession();
|
||||||
Assert.assertNotNull(StpUtil.stpLogic.getTokenSession(false));
|
Assertions.assertNotNull(StpUtil.stpLogic.getTokenSession(false));
|
||||||
SaSession session2 = dao.getSession("satoken:login:token-session:" + token);
|
SaSession session2 = dao.getSession("satoken:login:token-session:" + token);
|
||||||
Assert.assertNotNull(session2);
|
Assertions.assertNotNull(session2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:账号封禁
|
// 测试:账号封禁
|
||||||
@Test(expected = DisableLoginException.class)
|
@Test
|
||||||
public void testDisable() {
|
public void testDisable() {
|
||||||
|
Assertions.assertThrows(DisableLoginException.class, () -> {
|
||||||
// 封号
|
// 封号
|
||||||
StpUtil.disable(10007, 200);
|
StpUtil.disable(10007, 200);
|
||||||
Assert.assertTrue(StpUtil.isDisable(10007));
|
Assertions.assertTrue(StpUtil.isDisable(10007));
|
||||||
Assert.assertEquals(dao.get("satoken:login:disable:" + 10007), DisableLoginException.BE_VALUE);
|
Assertions.assertEquals(dao.get("satoken:login:disable:" + 10007), DisableLoginException.BE_VALUE);
|
||||||
|
|
||||||
// 解封
|
// 解封
|
||||||
StpUtil.untieDisable(10007);
|
StpUtil.untieDisable(10007);
|
||||||
Assert.assertFalse(StpUtil.isDisable(10007));
|
Assertions.assertFalse(StpUtil.isDisable(10007));
|
||||||
Assert.assertEquals(dao.get("satoken:login:disable:" + 10007), null);
|
Assertions.assertEquals(dao.get("satoken:login:disable:" + 10007), null);
|
||||||
|
|
||||||
// 封号后登陆 (会抛出 DisableLoginException 异常)
|
// 封号后登陆 (会抛出 DisableLoginException 异常)
|
||||||
StpUtil.disable(10007, 200);
|
StpUtil.disable(10007, 200);
|
||||||
StpUtil.login(10007);
|
StpUtil.login(10007);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:身份切换
|
// 测试:身份切换
|
||||||
@@ -224,23 +224,24 @@ public class JwtForMixTest {
|
|||||||
public void testSwitch() {
|
public void testSwitch() {
|
||||||
// 登录
|
// 登录
|
||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
Assert.assertFalse(StpUtil.isSwitch());
|
Assertions.assertFalse(StpUtil.isSwitch());
|
||||||
Assert.assertEquals(StpUtil.getLoginIdAsLong(), 10001);
|
Assertions.assertEquals(StpUtil.getLoginIdAsLong(), 10001);
|
||||||
|
|
||||||
// 开始身份切换
|
// 开始身份切换
|
||||||
StpUtil.switchTo(10044);
|
StpUtil.switchTo(10044);
|
||||||
Assert.assertTrue(StpUtil.isSwitch());
|
Assertions.assertTrue(StpUtil.isSwitch());
|
||||||
Assert.assertEquals(StpUtil.getLoginIdAsLong(), 10044);
|
Assertions.assertEquals(StpUtil.getLoginIdAsLong(), 10044);
|
||||||
|
|
||||||
// 结束切换
|
// 结束切换
|
||||||
StpUtil.endSwitch();
|
StpUtil.endSwitch();
|
||||||
Assert.assertFalse(StpUtil.isSwitch());
|
Assertions.assertFalse(StpUtil.isSwitch());
|
||||||
Assert.assertEquals(StpUtil.getLoginIdAsLong(), 10001);
|
Assertions.assertEquals(StpUtil.getLoginIdAsLong(), 10001);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:会话管理
|
// 测试:会话管理
|
||||||
@Test(expected = ApiDisabledException.class)
|
@Test
|
||||||
public void testSearchTokenValue() {
|
public void testSearchTokenValue() {
|
||||||
|
Assertions.assertThrows(ApiDisabledException.class, () -> {
|
||||||
// 登录
|
// 登录
|
||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
StpUtil.login(10002);
|
StpUtil.login(10002);
|
||||||
@@ -250,7 +251,8 @@ public class JwtForMixTest {
|
|||||||
|
|
||||||
// 查询
|
// 查询
|
||||||
List<String> list = StpUtil.searchTokenValue("", 0, 10);
|
List<String> list = StpUtil.searchTokenValue("", 0, 10);
|
||||||
Assert.assertTrue(list.size() >= 5);
|
Assertions.assertTrue(list.size() >= 5);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,13 +1,11 @@
|
|||||||
package com.pj.test;
|
package com.pj.test;
|
||||||
|
|
||||||
import org.junit.AfterClass;
|
import org.junit.jupiter.api.AfterAll;
|
||||||
import org.junit.Assert;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
import cn.dev33.satoken.SaManager;
|
import cn.dev33.satoken.SaManager;
|
||||||
import cn.dev33.satoken.dao.SaTokenDao;
|
import cn.dev33.satoken.dao.SaTokenDao;
|
||||||
@@ -25,7 +23,6 @@ import cn.hutool.jwt.JWT;
|
|||||||
* @author kong
|
* @author kong
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(classes = StartUpApplication.class)
|
@SpringBootTest(classes = StartUpApplication.class)
|
||||||
public class JwtForStatelessTest {
|
public class JwtForStatelessTest {
|
||||||
|
|
||||||
@@ -34,14 +31,14 @@ public class JwtForStatelessTest {
|
|||||||
SaTokenDao dao = SaManager.getSaTokenDao();
|
SaTokenDao dao = SaManager.getSaTokenDao();
|
||||||
|
|
||||||
// 开始
|
// 开始
|
||||||
@BeforeClass
|
@BeforeAll
|
||||||
public static void beforeClass() {
|
public static void beforeClass() {
|
||||||
System.out.println("\n\n------------------------ JwtForStatelessTest star ...");
|
System.out.println("\n\n------------------------ JwtForStatelessTest star ...");
|
||||||
StpUtil.setStpLogic(new StpLogicJwtForStateless());
|
StpUtil.setStpLogic(new StpLogicJwtForStateless());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 结束
|
// 结束
|
||||||
@AfterClass
|
@AfterAll
|
||||||
public static void afterClass() {
|
public static void afterClass() {
|
||||||
System.out.println("\n\n------------------------ JwtForStatelessTest end ... \n");
|
System.out.println("\n\n------------------------ JwtForStatelessTest end ... \n");
|
||||||
}
|
}
|
||||||
@@ -54,26 +51,26 @@ public class JwtForStatelessTest {
|
|||||||
String token = StpUtil.getTokenValue();
|
String token = StpUtil.getTokenValue();
|
||||||
|
|
||||||
// API 验证
|
// API 验证
|
||||||
Assert.assertTrue(StpUtil.isLogin());
|
Assertions.assertTrue(StpUtil.isLogin());
|
||||||
Assert.assertNotNull(token); // token不为null
|
Assertions.assertNotNull(token); // token不为null
|
||||||
Assert.assertEquals(StpUtil.getLoginIdAsLong(), 10001); // loginId=10001
|
Assertions.assertEquals(StpUtil.getLoginIdAsLong(), 10001); // loginId=10001
|
||||||
Assert.assertEquals(StpUtil.getLoginDevice(), SaTokenConsts.DEFAULT_LOGIN_DEVICE); // 登录设备类型
|
Assertions.assertEquals(StpUtil.getLoginDevice(), SaTokenConsts.DEFAULT_LOGIN_DEVICE); // 登录设备类型
|
||||||
|
|
||||||
// token 验证
|
// token 验证
|
||||||
JWT jwt = JWT.of(token);
|
JWT jwt = JWT.of(token);
|
||||||
JSONObject payloads = jwt.getPayloads();
|
JSONObject payloads = jwt.getPayloads();
|
||||||
Assert.assertEquals(payloads.getStr(SaJwtUtil.LOGIN_ID), "10001"); // 账号
|
Assertions.assertEquals(payloads.getStr(SaJwtUtil.LOGIN_ID), "10001"); // 账号
|
||||||
Assert.assertEquals(payloads.getStr(SaJwtUtil.DEVICE), SaTokenConsts.DEFAULT_LOGIN_DEVICE); // 登录设备类型
|
Assertions.assertEquals(payloads.getStr(SaJwtUtil.DEVICE), SaTokenConsts.DEFAULT_LOGIN_DEVICE); // 登录设备类型
|
||||||
Assert.assertEquals(payloads.getStr(SaJwtUtil.LOGIN_TYPE), StpUtil.TYPE); // 账号类型
|
Assertions.assertEquals(payloads.getStr(SaJwtUtil.LOGIN_TYPE), StpUtil.TYPE); // 账号类型
|
||||||
|
|
||||||
// 时间
|
// 时间
|
||||||
Assert.assertTrue(StpUtil.getTokenTimeout() <= SaManager.getConfig().getTimeout());
|
Assertions.assertTrue(StpUtil.getTokenTimeout() <= SaManager.getConfig().getTimeout());
|
||||||
Assert.assertTrue(StpUtil.getTokenTimeout() > SaManager.getConfig().getTimeout() - 10000);
|
Assertions.assertTrue(StpUtil.getTokenTimeout() > SaManager.getConfig().getTimeout() - 10000);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 尝试获取Session会抛出异常
|
// 尝试获取Session会抛出异常
|
||||||
StpUtil.getSession();
|
StpUtil.getSession();
|
||||||
Assert.assertTrue(false);
|
Assertions.assertTrue(false);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -84,23 +81,25 @@ public class JwtForStatelessTest {
|
|||||||
// 登录
|
// 登录
|
||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
String token = StpUtil.getTokenValue();
|
String token = StpUtil.getTokenValue();
|
||||||
Assert.assertEquals(JWT.of(token).getPayloads().getStr("loginId"), "10001");
|
Assertions.assertEquals(JWT.of(token).getPayloads().getStr("loginId"), "10001");
|
||||||
|
|
||||||
// 注销
|
// 注销
|
||||||
StpUtil.logout();
|
StpUtil.logout();
|
||||||
|
|
||||||
// token 应该被清除
|
// token 应该被清除
|
||||||
Assert.assertNull(StpUtil.getTokenValue());
|
Assertions.assertNull(StpUtil.getTokenValue());
|
||||||
Assert.assertFalse(StpUtil.isLogin());
|
Assertions.assertFalse(StpUtil.isLogin());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:Session会话
|
// 测试:Session会话
|
||||||
@Test(expected = ApiDisabledException.class)
|
@Test
|
||||||
public void testSession() {
|
public void testSession() {
|
||||||
|
Assertions.assertThrows(ApiDisabledException.class, () -> {
|
||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
|
|
||||||
// 会抛异常
|
// 会抛异常
|
||||||
StpUtil.getSession();
|
StpUtil.getSession();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:权限认证
|
// 测试:权限认证
|
||||||
@@ -109,17 +108,17 @@ public class JwtForStatelessTest {
|
|||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
|
|
||||||
// 权限认证
|
// 权限认证
|
||||||
Assert.assertTrue(StpUtil.hasPermission("user-add"));
|
Assertions.assertTrue(StpUtil.hasPermission("user-add"));
|
||||||
Assert.assertTrue(StpUtil.hasPermission("user-list"));
|
Assertions.assertTrue(StpUtil.hasPermission("user-list"));
|
||||||
Assert.assertTrue(StpUtil.hasPermission("user"));
|
Assertions.assertTrue(StpUtil.hasPermission("user"));
|
||||||
Assert.assertTrue(StpUtil.hasPermission("art-add"));
|
Assertions.assertTrue(StpUtil.hasPermission("art-add"));
|
||||||
Assert.assertFalse(StpUtil.hasPermission("get-user"));
|
Assertions.assertFalse(StpUtil.hasPermission("get-user"));
|
||||||
// and
|
// and
|
||||||
Assert.assertTrue(StpUtil.hasPermissionAnd("art-add", "art-get"));
|
Assertions.assertTrue(StpUtil.hasPermissionAnd("art-add", "art-get"));
|
||||||
Assert.assertFalse(StpUtil.hasPermissionAnd("art-add", "comment-add"));
|
Assertions.assertFalse(StpUtil.hasPermissionAnd("art-add", "comment-add"));
|
||||||
// or
|
// or
|
||||||
Assert.assertTrue(StpUtil.hasPermissionOr("art-add", "comment-add"));
|
Assertions.assertTrue(StpUtil.hasPermissionOr("art-add", "comment-add"));
|
||||||
Assert.assertFalse(StpUtil.hasPermissionOr("comment-add", "comment-delete"));
|
Assertions.assertFalse(StpUtil.hasPermissionOr("comment-add", "comment-delete"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:角色认证
|
// 测试:角色认证
|
||||||
@@ -128,39 +127,41 @@ public class JwtForStatelessTest {
|
|||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
|
|
||||||
// 角色认证
|
// 角色认证
|
||||||
Assert.assertTrue(StpUtil.hasRole("admin"));
|
Assertions.assertTrue(StpUtil.hasRole("admin"));
|
||||||
Assert.assertFalse(StpUtil.hasRole("teacher"));
|
Assertions.assertFalse(StpUtil.hasRole("teacher"));
|
||||||
// and
|
// and
|
||||||
Assert.assertTrue(StpUtil.hasRoleAnd("admin", "super-admin"));
|
Assertions.assertTrue(StpUtil.hasRoleAnd("admin", "super-admin"));
|
||||||
Assert.assertFalse(StpUtil.hasRoleAnd("admin", "ceo"));
|
Assertions.assertFalse(StpUtil.hasRoleAnd("admin", "ceo"));
|
||||||
// or
|
// or
|
||||||
Assert.assertTrue(StpUtil.hasRoleOr("admin", "ceo"));
|
Assertions.assertTrue(StpUtil.hasRoleOr("admin", "ceo"));
|
||||||
Assert.assertFalse(StpUtil.hasRoleOr("ceo", "cto"));
|
Assertions.assertFalse(StpUtil.hasRoleOr("ceo", "cto"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:根据token强制注销
|
// 测试:根据token强制注销
|
||||||
@Test(expected = ApiDisabledException.class)
|
@Test
|
||||||
public void testLogoutByToken() {
|
public void testLogoutByToken() {
|
||||||
|
Assertions.assertThrows(ApiDisabledException.class, () -> {
|
||||||
// 先登录上
|
// 先登录上
|
||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
Assert.assertTrue(StpUtil.isLogin());
|
Assertions.assertTrue(StpUtil.isLogin());
|
||||||
String token = StpUtil.getTokenValue();
|
String token = StpUtil.getTokenValue();
|
||||||
|
|
||||||
// 根据token注销
|
// 根据token注销
|
||||||
StpUtil.logoutByTokenValue(token);
|
StpUtil.logoutByTokenValue(token);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:根据账号id强制注销
|
// 测试:根据账号id强制注销
|
||||||
@Test(expected = ApiDisabledException.class)
|
@Test
|
||||||
public void testLogoutByLoginId() {
|
public void testLogoutByLoginId() {
|
||||||
|
Assertions.assertThrows(ApiDisabledException.class, () -> {
|
||||||
// 先登录上
|
// 先登录上
|
||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
Assert.assertTrue(StpUtil.isLogin());
|
Assertions.assertTrue(StpUtil.isLogin());
|
||||||
|
|
||||||
// 根据账号id注销
|
// 根据账号id注销
|
||||||
StpUtil.logout(10001);
|
StpUtil.logout(10001);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,12 +1,10 @@
|
|||||||
package com.pj.test;
|
package com.pj.test;
|
||||||
|
|
||||||
import org.junit.AfterClass;
|
import org.junit.jupiter.api.AfterAll;
|
||||||
import org.junit.Assert;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
import cn.dev33.satoken.SaManager;
|
import cn.dev33.satoken.SaManager;
|
||||||
import cn.dev33.satoken.dao.SaTokenDao;
|
import cn.dev33.satoken.dao.SaTokenDao;
|
||||||
@@ -23,7 +21,7 @@ import cn.hutool.jwt.JWT;
|
|||||||
* @author kong
|
* @author kong
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
//@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = StartUpApplication.class)
|
@SpringBootTest(classes = StartUpApplication.class)
|
||||||
public class JwtForStyleTest {
|
public class JwtForStyleTest {
|
||||||
|
|
||||||
@@ -31,7 +29,7 @@ public class JwtForStyleTest {
|
|||||||
static SaTokenDao dao;
|
static SaTokenDao dao;
|
||||||
|
|
||||||
// 开始
|
// 开始
|
||||||
@BeforeClass
|
@BeforeAll
|
||||||
public static void beforeClass() {
|
public static void beforeClass() {
|
||||||
System.out.println("\n\n------------------------ JwtForStyleTest star ...");
|
System.out.println("\n\n------------------------ JwtForStyleTest star ...");
|
||||||
dao = SaManager.getSaTokenDao();
|
dao = SaManager.getSaTokenDao();
|
||||||
@@ -39,7 +37,7 @@ public class JwtForStyleTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 结束
|
// 结束
|
||||||
@AfterClass
|
@AfterAll
|
||||||
public static void afterClass() {
|
public static void afterClass() {
|
||||||
System.out.println("\n\n------------------------ JwtForStyleTest end ... \n");
|
System.out.println("\n\n------------------------ JwtForStyleTest end ... \n");
|
||||||
}
|
}
|
||||||
@@ -52,24 +50,24 @@ public class JwtForStyleTest {
|
|||||||
String token = StpUtil.getTokenValue();
|
String token = StpUtil.getTokenValue();
|
||||||
|
|
||||||
// API 验证
|
// API 验证
|
||||||
Assert.assertTrue(StpUtil.isLogin());
|
Assertions.assertTrue(StpUtil.isLogin());
|
||||||
Assert.assertNotNull(token); // token不为null
|
Assertions.assertNotNull(token); // token不为null
|
||||||
Assert.assertEquals(StpUtil.getLoginIdAsLong(), 10001); // loginId=10001
|
Assertions.assertEquals(StpUtil.getLoginIdAsLong(), 10001); // loginId=10001
|
||||||
Assert.assertEquals(StpUtil.getLoginDevice(), SaTokenConsts.DEFAULT_LOGIN_DEVICE); // 登录设备类型
|
Assertions.assertEquals(StpUtil.getLoginDevice(), SaTokenConsts.DEFAULT_LOGIN_DEVICE); // 登录设备类型
|
||||||
|
|
||||||
// token 验证
|
// token 验证
|
||||||
JWT jwt = JWT.of(token);
|
JWT jwt = JWT.of(token);
|
||||||
JSONObject payloads = jwt.getPayloads();
|
JSONObject payloads = jwt.getPayloads();
|
||||||
Assert.assertEquals(payloads.getStr("loginId"), "10001");
|
Assertions.assertEquals(payloads.getStr("loginId"), "10001");
|
||||||
|
|
||||||
// db数据 验证
|
// db数据 验证
|
||||||
// token存在
|
// token存在
|
||||||
Assert.assertEquals(dao.get("satoken:login:token:" + token), "10001");
|
Assertions.assertEquals(dao.get("satoken:login:token:" + token), "10001");
|
||||||
// Session 存在
|
// Session 存在
|
||||||
SaSession session = dao.getSession("satoken:login:session:" + 10001);
|
SaSession session = dao.getSession("satoken:login:session:" + 10001);
|
||||||
Assert.assertNotNull(session);
|
Assertions.assertNotNull(session);
|
||||||
Assert.assertEquals(session.getId(), "satoken:login:session:" + 10001);
|
Assertions.assertEquals(session.getId(), "satoken:login:session:" + 10001);
|
||||||
Assert.assertTrue(session.getTokenSignList().size() >= 1);
|
Assertions.assertTrue(session.getTokenSignList().size() >= 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -2,14 +2,12 @@ package com.pj.test;
|
|||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.Before;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
import org.springframework.test.web.servlet.MvcResult;
|
import org.springframework.test.web.servlet.MvcResult;
|
||||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||||
@@ -25,7 +23,6 @@ import com.pj.test.util.SoMap;
|
|||||||
* @author Auster
|
* @author Auster
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(classes = StartUpApplication.class)
|
@SpringBootTest(classes = StartUpApplication.class)
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
public class LoginControllerTest {
|
public class LoginControllerTest {
|
||||||
@@ -36,7 +33,7 @@ public class LoginControllerTest {
|
|||||||
private MockMvc mvc;
|
private MockMvc mvc;
|
||||||
|
|
||||||
// 开始
|
// 开始
|
||||||
@Before
|
@BeforeEach
|
||||||
public void before() {
|
public void before() {
|
||||||
mvc = MockMvcBuilders.webAppContextSetup(wac).build();
|
mvc = MockMvcBuilders.webAppContextSetup(wac).build();
|
||||||
}
|
}
|
||||||
@@ -61,9 +58,9 @@ public class LoginControllerTest {
|
|||||||
String token = so.getString("token");
|
String token = so.getString("token");
|
||||||
|
|
||||||
// 断言
|
// 断言
|
||||||
Assert.assertTrue(mvcResult.getResponse().getHeader("Set-Cookie") != null);
|
Assertions.assertTrue(mvcResult.getResponse().getHeader("Set-Cookie") != null);
|
||||||
Assert.assertEquals(so.getInt("code"), 200);
|
Assertions.assertEquals(so.getInt("code"), 200);
|
||||||
Assert.assertNotNull(token);
|
Assertions.assertNotNull(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -71,26 +68,26 @@ public class LoginControllerTest {
|
|||||||
public void testLogin2() throws Exception{
|
public void testLogin2() throws Exception{
|
||||||
// 获取token
|
// 获取token
|
||||||
SoMap so = request("/acc/doLogin?name=zhang&pwd=123456");
|
SoMap so = request("/acc/doLogin?name=zhang&pwd=123456");
|
||||||
Assert.assertNotNull(so.getString("token"));
|
Assertions.assertNotNull(so.getString("token"));
|
||||||
|
|
||||||
String token = so.getString("token");
|
String token = so.getString("token");
|
||||||
|
|
||||||
// 是否登录
|
// 是否登录
|
||||||
SoMap so2 = request("/acc/isLogin?satoken=" + token);
|
SoMap so2 = request("/acc/isLogin?satoken=" + token);
|
||||||
Assert.assertTrue(so2.getBoolean("data"));
|
Assertions.assertTrue(so2.getBoolean("data"));
|
||||||
|
|
||||||
// tokenInfo
|
// tokenInfo
|
||||||
SoMap so3 = request("/acc/tokenInfo?satoken=" + token);
|
SoMap so3 = request("/acc/tokenInfo?satoken=" + token);
|
||||||
SoMap so4 = SoMap.getSoMap((Map<String, ?>)so3.get("data"));
|
SoMap so4 = SoMap.getSoMap((Map<String, ?>)so3.get("data"));
|
||||||
Assert.assertEquals(so4.getString("tokenName"), "satoken");
|
Assertions.assertEquals(so4.getString("tokenName"), "satoken");
|
||||||
Assert.assertEquals(so4.getString("tokenValue"), token);
|
Assertions.assertEquals(so4.getString("tokenValue"), token);
|
||||||
|
|
||||||
// 注销
|
// 注销
|
||||||
request("/acc/logout?satoken=" + token);
|
request("/acc/logout?satoken=" + token);
|
||||||
|
|
||||||
// 是否登录
|
// 是否登录
|
||||||
SoMap so5 = request("/acc/isLogin?satoken=" + token);
|
SoMap so5 = request("/acc/isLogin?satoken=" + token);
|
||||||
Assert.assertFalse(so5.getBoolean("data"));
|
Assertions.assertFalse(so5.getBoolean("data"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 封装请求
|
// 封装请求
|
||||||
|
@@ -2,14 +2,12 @@ package com.pj.test;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.AfterClass;
|
import org.junit.jupiter.api.AfterAll;
|
||||||
import org.junit.Assert;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
import cn.dev33.satoken.SaManager;
|
import cn.dev33.satoken.SaManager;
|
||||||
import cn.dev33.satoken.dao.SaTokenDao;
|
import cn.dev33.satoken.dao.SaTokenDao;
|
||||||
@@ -28,7 +26,6 @@ import cn.dev33.satoken.util.SaTokenConsts;
|
|||||||
* @author Auster
|
* @author Auster
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(classes = StartUpApplication.class)
|
@SpringBootTest(classes = StartUpApplication.class)
|
||||||
public class BasicsTest {
|
public class BasicsTest {
|
||||||
|
|
||||||
@@ -37,13 +34,13 @@ public class BasicsTest {
|
|||||||
SaTokenDao dao = SaManager.getSaTokenDao();
|
SaTokenDao dao = SaManager.getSaTokenDao();
|
||||||
|
|
||||||
// 开始
|
// 开始
|
||||||
@BeforeClass
|
@BeforeAll
|
||||||
public static void beforeClass() {
|
public static void beforeClass() {
|
||||||
System.out.println("\n\n------------------------ 基础测试 star ...");
|
System.out.println("\n\n------------------------ 基础测试 star ...");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 结束
|
// 结束
|
||||||
@AfterClass
|
@AfterAll
|
||||||
public static void afterClass() {
|
public static void afterClass() {
|
||||||
System.out.println("\n\n------------------------ 基础测试 end ... \n");
|
System.out.println("\n\n------------------------ 基础测试 end ... \n");
|
||||||
}
|
}
|
||||||
@@ -56,19 +53,19 @@ public class BasicsTest {
|
|||||||
String token = StpUtil.getTokenValue();
|
String token = StpUtil.getTokenValue();
|
||||||
|
|
||||||
// API 验证
|
// API 验证
|
||||||
Assert.assertTrue(StpUtil.isLogin());
|
Assertions.assertTrue(StpUtil.isLogin());
|
||||||
Assert.assertNotNull(token); // token不为null
|
Assertions.assertNotNull(token); // token不为null
|
||||||
Assert.assertEquals(StpUtil.getLoginIdAsLong(), 10001); // loginId=10001
|
Assertions.assertEquals(StpUtil.getLoginIdAsLong(), 10001); // loginId=10001
|
||||||
Assert.assertEquals(StpUtil.getLoginDevice(), SaTokenConsts.DEFAULT_LOGIN_DEVICE); // 登录设备类型
|
Assertions.assertEquals(StpUtil.getLoginDevice(), SaTokenConsts.DEFAULT_LOGIN_DEVICE); // 登录设备类型
|
||||||
|
|
||||||
// db数据 验证
|
// db数据 验证
|
||||||
// token存在
|
// token存在
|
||||||
Assert.assertEquals(dao.get("satoken:login:token:" + token), "10001");
|
Assertions.assertEquals(dao.get("satoken:login:token:" + token), "10001");
|
||||||
// Session 存在
|
// Session 存在
|
||||||
SaSession session = dao.getSession("satoken:login:session:" + 10001);
|
SaSession session = dao.getSession("satoken:login:session:" + 10001);
|
||||||
Assert.assertNotNull(session);
|
Assertions.assertNotNull(session);
|
||||||
Assert.assertEquals(session.getId(), "satoken:login:session:" + 10001);
|
Assertions.assertEquals(session.getId(), "satoken:login:session:" + 10001);
|
||||||
Assert.assertTrue(session.getTokenSignList().size() >= 1);
|
Assertions.assertTrue(session.getTokenSignList().size() >= 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:注销
|
// 测试:注销
|
||||||
@@ -77,17 +74,17 @@ public class BasicsTest {
|
|||||||
// 登录
|
// 登录
|
||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
String token = StpUtil.getTokenValue();
|
String token = StpUtil.getTokenValue();
|
||||||
Assert.assertEquals(dao.get("satoken:login:token:" + token), "10001");
|
Assertions.assertEquals(dao.get("satoken:login:token:" + token), "10001");
|
||||||
|
|
||||||
// 注销
|
// 注销
|
||||||
StpUtil.logout();
|
StpUtil.logout();
|
||||||
// token 应该被清除
|
// token 应该被清除
|
||||||
Assert.assertNull(StpUtil.getTokenValue());
|
Assertions.assertNull(StpUtil.getTokenValue());
|
||||||
Assert.assertFalse(StpUtil.isLogin());
|
Assertions.assertFalse(StpUtil.isLogin());
|
||||||
Assert.assertNull(dao.get("satoken:login:token:" + token));
|
Assertions.assertNull(dao.get("satoken:login:token:" + token));
|
||||||
// Session 应该被清除
|
// Session 应该被清除
|
||||||
SaSession session = dao.getSession("satoken:login:session:" + 10001);
|
SaSession session = dao.getSession("satoken:login:session:" + 10001);
|
||||||
Assert.assertNull(session);
|
Assertions.assertNull(session);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:Session会话
|
// 测试:Session会话
|
||||||
@@ -96,23 +93,23 @@ public class BasicsTest {
|
|||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
|
|
||||||
// API 应该可以获取 Session
|
// API 应该可以获取 Session
|
||||||
Assert.assertNotNull(StpUtil.getSession(false));
|
Assertions.assertNotNull(StpUtil.getSession(false));
|
||||||
|
|
||||||
// db中应该存在 Session
|
// db中应该存在 Session
|
||||||
SaSession session = dao.getSession("satoken:login:session:" + 10001);
|
SaSession session = dao.getSession("satoken:login:session:" + 10001);
|
||||||
Assert.assertNotNull(session);
|
Assertions.assertNotNull(session);
|
||||||
|
|
||||||
// 存取值
|
// 存取值
|
||||||
session.set("name", "zhang");
|
session.set("name", "zhang");
|
||||||
session.set("age", "18");
|
session.set("age", "18");
|
||||||
Assert.assertEquals(session.get("name"), "zhang");
|
Assertions.assertEquals(session.get("name"), "zhang");
|
||||||
Assert.assertEquals(session.getInt("age"), 18);
|
Assertions.assertEquals(session.getInt("age"), 18);
|
||||||
Assert.assertEquals((int)session.getModel("age", int.class), 18);
|
Assertions.assertEquals((int)session.getModel("age", int.class), 18);
|
||||||
Assert.assertEquals((int)session.get("age", 20), 18);
|
Assertions.assertEquals((int)session.get("age", 20), 18);
|
||||||
Assert.assertEquals((int)session.get("name2", 20), 20);
|
Assertions.assertEquals((int)session.get("name2", 20), 20);
|
||||||
Assert.assertEquals((int)session.get("name2", () -> 30), 30);
|
Assertions.assertEquals((int)session.get("name2", () -> 30), 30);
|
||||||
session.clear();
|
session.clear();
|
||||||
Assert.assertEquals(session.get("name"), null);
|
Assertions.assertEquals(session.get("name"), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:权限认证
|
// 测试:权限认证
|
||||||
@@ -121,17 +118,17 @@ public class BasicsTest {
|
|||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
|
|
||||||
// 权限认证
|
// 权限认证
|
||||||
Assert.assertTrue(StpUtil.hasPermission("user-add"));
|
Assertions.assertTrue(StpUtil.hasPermission("user-add"));
|
||||||
Assert.assertTrue(StpUtil.hasPermission("user-list"));
|
Assertions.assertTrue(StpUtil.hasPermission("user-list"));
|
||||||
Assert.assertTrue(StpUtil.hasPermission("user"));
|
Assertions.assertTrue(StpUtil.hasPermission("user"));
|
||||||
Assert.assertTrue(StpUtil.hasPermission("art-add"));
|
Assertions.assertTrue(StpUtil.hasPermission("art-add"));
|
||||||
Assert.assertFalse(StpUtil.hasPermission("get-user"));
|
Assertions.assertFalse(StpUtil.hasPermission("get-user"));
|
||||||
// and
|
// and
|
||||||
Assert.assertTrue(StpUtil.hasPermissionAnd("art-add", "art-get"));
|
Assertions.assertTrue(StpUtil.hasPermissionAnd("art-add", "art-get"));
|
||||||
Assert.assertFalse(StpUtil.hasPermissionAnd("art-add", "comment-add"));
|
Assertions.assertFalse(StpUtil.hasPermissionAnd("art-add", "comment-add"));
|
||||||
// or
|
// or
|
||||||
Assert.assertTrue(StpUtil.hasPermissionOr("art-add", "comment-add"));
|
Assertions.assertTrue(StpUtil.hasPermissionOr("art-add", "comment-add"));
|
||||||
Assert.assertFalse(StpUtil.hasPermissionOr("comment-add", "comment-delete"));
|
Assertions.assertFalse(StpUtil.hasPermissionOr("comment-add", "comment-delete"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:角色认证
|
// 测试:角色认证
|
||||||
@@ -140,14 +137,14 @@ public class BasicsTest {
|
|||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
|
|
||||||
// 角色认证
|
// 角色认证
|
||||||
Assert.assertTrue(StpUtil.hasRole("admin"));
|
Assertions.assertTrue(StpUtil.hasRole("admin"));
|
||||||
Assert.assertFalse(StpUtil.hasRole("teacher"));
|
Assertions.assertFalse(StpUtil.hasRole("teacher"));
|
||||||
// and
|
// and
|
||||||
Assert.assertTrue(StpUtil.hasRoleAnd("admin", "super-admin"));
|
Assertions.assertTrue(StpUtil.hasRoleAnd("admin", "super-admin"));
|
||||||
Assert.assertFalse(StpUtil.hasRoleAnd("admin", "ceo"));
|
Assertions.assertFalse(StpUtil.hasRoleAnd("admin", "ceo"));
|
||||||
// or
|
// or
|
||||||
Assert.assertTrue(StpUtil.hasRoleOr("admin", "ceo"));
|
Assertions.assertTrue(StpUtil.hasRoleOr("admin", "ceo"));
|
||||||
Assert.assertFalse(StpUtil.hasRoleOr("ceo", "cto"));
|
Assertions.assertFalse(StpUtil.hasRoleOr("ceo", "cto"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:根据token强制注销
|
// 测试:根据token强制注销
|
||||||
@@ -156,24 +153,24 @@ public class BasicsTest {
|
|||||||
|
|
||||||
// 先登录上
|
// 先登录上
|
||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
Assert.assertTrue(StpUtil.isLogin());
|
Assertions.assertTrue(StpUtil.isLogin());
|
||||||
String token = StpUtil.getTokenValue();
|
String token = StpUtil.getTokenValue();
|
||||||
|
|
||||||
// 根据token注销
|
// 根据token注销
|
||||||
StpUtil.logoutByTokenValue(token);
|
StpUtil.logoutByTokenValue(token);
|
||||||
Assert.assertFalse(StpUtil.isLogin());
|
Assertions.assertFalse(StpUtil.isLogin());
|
||||||
|
|
||||||
// token 应该被清除
|
// token 应该被清除
|
||||||
Assert.assertNull(dao.get("satoken:login:token:" + token));
|
Assertions.assertNull(dao.get("satoken:login:token:" + token));
|
||||||
// Session 应该被清除
|
// Session 应该被清除
|
||||||
SaSession session = dao.getSession("satoken:login:session:" + 10001);
|
SaSession session = dao.getSession("satoken:login:session:" + 10001);
|
||||||
Assert.assertNull(session);
|
Assertions.assertNull(session);
|
||||||
|
|
||||||
// 场景值应该是token无效
|
// 场景值应该是token无效
|
||||||
try {
|
try {
|
||||||
StpUtil.checkLogin();
|
StpUtil.checkLogin();
|
||||||
} catch (NotLoginException e) {
|
} catch (NotLoginException e) {
|
||||||
Assert.assertEquals(e.getType(), NotLoginException.INVALID_TOKEN);
|
Assertions.assertEquals(e.getType(), NotLoginException.INVALID_TOKEN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -183,24 +180,24 @@ public class BasicsTest {
|
|||||||
|
|
||||||
// 先登录上
|
// 先登录上
|
||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
Assert.assertTrue(StpUtil.isLogin());
|
Assertions.assertTrue(StpUtil.isLogin());
|
||||||
String token = StpUtil.getTokenValue();
|
String token = StpUtil.getTokenValue();
|
||||||
|
|
||||||
// 根据账号id注销
|
// 根据账号id注销
|
||||||
StpUtil.logout(10001);
|
StpUtil.logout(10001);
|
||||||
Assert.assertFalse(StpUtil.isLogin());
|
Assertions.assertFalse(StpUtil.isLogin());
|
||||||
|
|
||||||
// token 应该被清除
|
// token 应该被清除
|
||||||
Assert.assertNull(dao.get("satoken:login:token:" + token));
|
Assertions.assertNull(dao.get("satoken:login:token:" + token));
|
||||||
// Session 应该被清除
|
// Session 应该被清除
|
||||||
SaSession session = dao.getSession("satoken:login:session:" + 10001);
|
SaSession session = dao.getSession("satoken:login:session:" + 10001);
|
||||||
Assert.assertNull(session);
|
Assertions.assertNull(session);
|
||||||
|
|
||||||
// 场景值应该是token无效
|
// 场景值应该是token无效
|
||||||
try {
|
try {
|
||||||
StpUtil.checkLogin();
|
StpUtil.checkLogin();
|
||||||
} catch (NotLoginException e) {
|
} catch (NotLoginException e) {
|
||||||
Assert.assertEquals(e.getType(), NotLoginException.INVALID_TOKEN);
|
Assertions.assertEquals(e.getType(), NotLoginException.INVALID_TOKEN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,40 +210,40 @@ public class BasicsTest {
|
|||||||
String token = StpUtil.getTokenValue();
|
String token = StpUtil.getTokenValue();
|
||||||
|
|
||||||
// 刚开始不存在
|
// 刚开始不存在
|
||||||
Assert.assertNull(StpUtil.stpLogic.getTokenSession(false));
|
Assertions.assertNull(StpUtil.stpLogic.getTokenSession(false));
|
||||||
SaSession session = dao.getSession("satoken:login:token-session:" + token);
|
SaSession session = dao.getSession("satoken:login:token-session:" + token);
|
||||||
Assert.assertNull(session);
|
Assertions.assertNull(session);
|
||||||
|
|
||||||
// 调用一次就存在了
|
// 调用一次就存在了
|
||||||
StpUtil.getTokenSession();
|
StpUtil.getTokenSession();
|
||||||
Assert.assertNotNull(StpUtil.stpLogic.getTokenSession(false));
|
Assertions.assertNotNull(StpUtil.stpLogic.getTokenSession(false));
|
||||||
SaSession session2 = dao.getSession("satoken:login:token-session:" + token);
|
SaSession session2 = dao.getSession("satoken:login:token-session:" + token);
|
||||||
Assert.assertNotNull(session2);
|
Assertions.assertNotNull(session2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试自定义Session
|
// 测试自定义Session
|
||||||
@Test
|
@Test
|
||||||
public void testCustomSession() {
|
public void testCustomSession() {
|
||||||
// 刚开始不存在
|
// 刚开始不存在
|
||||||
Assert.assertFalse(SaSessionCustomUtil.isExists("art-1"));
|
Assertions.assertFalse(SaSessionCustomUtil.isExists("art-1"));
|
||||||
SaSession session = dao.getSession("satoken:custom:session:" + "art-1");
|
SaSession session = dao.getSession("satoken:custom:session:" + "art-1");
|
||||||
Assert.assertNull(session);
|
Assertions.assertNull(session);
|
||||||
|
|
||||||
// 调用一下
|
// 调用一下
|
||||||
SaSessionCustomUtil.getSessionById("art-1");
|
SaSessionCustomUtil.getSessionById("art-1");
|
||||||
|
|
||||||
// 就存在了
|
// 就存在了
|
||||||
Assert.assertTrue(SaSessionCustomUtil.isExists("art-1"));
|
Assertions.assertTrue(SaSessionCustomUtil.isExists("art-1"));
|
||||||
SaSession session2 = dao.getSession("satoken:custom:session:" + "art-1");
|
SaSession session2 = dao.getSession("satoken:custom:session:" + "art-1");
|
||||||
Assert.assertNotNull(session2);
|
Assertions.assertNotNull(session2);
|
||||||
|
|
||||||
// 给删除掉
|
// 给删除掉
|
||||||
SaSessionCustomUtil.deleteSessionById("art-1");
|
SaSessionCustomUtil.deleteSessionById("art-1");
|
||||||
|
|
||||||
// 就又不存在了
|
// 就又不存在了
|
||||||
Assert.assertFalse(SaSessionCustomUtil.isExists("art-1"));
|
Assertions.assertFalse(SaSessionCustomUtil.isExists("art-1"));
|
||||||
SaSession session3 = dao.getSession("satoken:custom:session:" + "art-1");
|
SaSession session3 = dao.getSession("satoken:custom:session:" + "art-1");
|
||||||
Assert.assertNull(session3);
|
Assertions.assertNull(session3);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:根据账号id踢人
|
// 测试:根据账号id踢人
|
||||||
@@ -259,33 +256,34 @@ public class BasicsTest {
|
|||||||
StpUtil.kickout(10001);
|
StpUtil.kickout(10001);
|
||||||
|
|
||||||
// token 应该被打标记
|
// token 应该被打标记
|
||||||
Assert.assertEquals(dao.get("satoken:login:token:" + token), NotLoginException.KICK_OUT);
|
Assertions.assertEquals(dao.get("satoken:login:token:" + token), NotLoginException.KICK_OUT);
|
||||||
|
|
||||||
// 场景值应该是token已被踢下线
|
// 场景值应该是token已被踢下线
|
||||||
try {
|
try {
|
||||||
StpUtil.checkLogin();
|
StpUtil.checkLogin();
|
||||||
} catch (NotLoginException e) {
|
} catch (NotLoginException e) {
|
||||||
Assert.assertEquals(e.getType(), NotLoginException.KICK_OUT);
|
Assertions.assertEquals(e.getType(), NotLoginException.KICK_OUT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:账号封禁
|
// 测试:账号封禁
|
||||||
@Test(expected = DisableLoginException.class)
|
@Test
|
||||||
public void testDisable() {
|
public void testDisable() {
|
||||||
|
Assertions.assertThrows(DisableLoginException.class, () -> {
|
||||||
// 封号
|
// 封号
|
||||||
StpUtil.disable(10007, 200);
|
StpUtil.disable(10007, 200);
|
||||||
Assert.assertTrue(StpUtil.isDisable(10007));
|
Assertions.assertTrue(StpUtil.isDisable(10007));
|
||||||
Assert.assertEquals(dao.get("satoken:login:disable:" + 10007), DisableLoginException.BE_VALUE);
|
Assertions.assertEquals(dao.get("satoken:login:disable:" + 10007), DisableLoginException.BE_VALUE);
|
||||||
|
|
||||||
// 解封
|
// 解封
|
||||||
StpUtil.untieDisable(10007);
|
StpUtil.untieDisable(10007);
|
||||||
Assert.assertFalse(StpUtil.isDisable(10007));
|
Assertions.assertFalse(StpUtil.isDisable(10007));
|
||||||
Assert.assertEquals(dao.get("satoken:login:disable:" + 10007), null);
|
Assertions.assertEquals(dao.get("satoken:login:disable:" + 10007), null);
|
||||||
|
|
||||||
// 封号后登陆 (会抛出 DisableLoginException 异常)
|
// 封号后登陆 (会抛出 DisableLoginException 异常)
|
||||||
StpUtil.disable(10007, 200);
|
StpUtil.disable(10007, 200);
|
||||||
StpUtil.login(10007);
|
StpUtil.login(10007);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:身份切换
|
// 测试:身份切换
|
||||||
@@ -293,18 +291,18 @@ public class BasicsTest {
|
|||||||
public void testSwitch() {
|
public void testSwitch() {
|
||||||
// 登录
|
// 登录
|
||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
Assert.assertFalse(StpUtil.isSwitch());
|
Assertions.assertFalse(StpUtil.isSwitch());
|
||||||
Assert.assertEquals(StpUtil.getLoginIdAsLong(), 10001);
|
Assertions.assertEquals(StpUtil.getLoginIdAsLong(), 10001);
|
||||||
|
|
||||||
// 开始身份切换
|
// 开始身份切换
|
||||||
StpUtil.switchTo(10044);
|
StpUtil.switchTo(10044);
|
||||||
Assert.assertTrue(StpUtil.isSwitch());
|
Assertions.assertTrue(StpUtil.isSwitch());
|
||||||
Assert.assertEquals(StpUtil.getLoginIdAsLong(), 10044);
|
Assertions.assertEquals(StpUtil.getLoginIdAsLong(), 10044);
|
||||||
|
|
||||||
// 结束切换
|
// 结束切换
|
||||||
StpUtil.endSwitch();
|
StpUtil.endSwitch();
|
||||||
Assert.assertFalse(StpUtil.isSwitch());
|
Assertions.assertFalse(StpUtil.isSwitch());
|
||||||
Assert.assertEquals(StpUtil.getLoginIdAsLong(), 10001);
|
Assertions.assertEquals(StpUtil.getLoginIdAsLong(), 10001);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:会话管理
|
// 测试:会话管理
|
||||||
@@ -319,7 +317,7 @@ public class BasicsTest {
|
|||||||
|
|
||||||
// 查询
|
// 查询
|
||||||
List<String> list = StpUtil.searchTokenValue("", 0, 10);
|
List<String> list = StpUtil.searchTokenValue("", 0, 10);
|
||||||
Assert.assertTrue(list.size() >= 5);
|
Assertions.assertTrue(list.size() >= 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:临时Token认证模块
|
// 测试:临时Token认证模块
|
||||||
@@ -327,22 +325,22 @@ public class BasicsTest {
|
|||||||
public void testSaTemp() {
|
public void testSaTemp() {
|
||||||
// 生成token
|
// 生成token
|
||||||
String token = SaTempUtil.createToken("group-1014", 200);
|
String token = SaTempUtil.createToken("group-1014", 200);
|
||||||
Assert.assertNotNull(token);
|
Assertions.assertNotNull(token);
|
||||||
|
|
||||||
// 解析token
|
// 解析token
|
||||||
String value = SaTempUtil.parseToken(token, String.class);
|
String value = SaTempUtil.parseToken(token, String.class);
|
||||||
Assert.assertEquals(value, "group-1014");
|
Assertions.assertEquals(value, "group-1014");
|
||||||
Assert.assertEquals(dao.getObject("satoken:temp-token:" + token), "group-1014");
|
Assertions.assertEquals(dao.getObject("satoken:temp-token:" + token), "group-1014");
|
||||||
|
|
||||||
// 过期时间
|
// 过期时间
|
||||||
long timeout = SaTempUtil.getTimeout(token);
|
long timeout = SaTempUtil.getTimeout(token);
|
||||||
Assert.assertTrue(timeout > 195);
|
Assertions.assertTrue(timeout > 195);
|
||||||
|
|
||||||
// 回收token
|
// 回收token
|
||||||
SaTempUtil.deleteToken(token);
|
SaTempUtil.deleteToken(token);
|
||||||
String value2 = SaTempUtil.parseToken(token, String.class);
|
String value2 = SaTempUtil.parseToken(token, String.class);
|
||||||
Assert.assertEquals(value2, null);
|
Assertions.assertEquals(value2, null);
|
||||||
Assert.assertEquals(dao.getObject("satoken:temp-token:" + token), null);
|
Assertions.assertEquals(dao.getObject("satoken:temp-token:" + token), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:二级认证
|
// 测试:二级认证
|
||||||
@@ -350,21 +348,21 @@ public class BasicsTest {
|
|||||||
public void testSafe() {
|
public void testSafe() {
|
||||||
// 登录
|
// 登录
|
||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
Assert.assertFalse(StpUtil.isSafe());
|
Assertions.assertFalse(StpUtil.isSafe());
|
||||||
|
|
||||||
// 开启二级认证
|
// 开启二级认证
|
||||||
StpUtil.openSafe(2);
|
StpUtil.openSafe(2);
|
||||||
Assert.assertTrue(StpUtil.isSafe());
|
Assertions.assertTrue(StpUtil.isSafe());
|
||||||
Assert.assertTrue(StpUtil.getSafeTime() > 0);
|
Assertions.assertTrue(StpUtil.getSafeTime() > 0);
|
||||||
|
|
||||||
// 自然结束
|
// 自然结束
|
||||||
// Thread.sleep(2500);
|
// Thread.sleep(2500);
|
||||||
// Assert.assertFalse(StpUtil.isSafe());
|
// Assertions.assertFalse(StpUtil.isSafe());
|
||||||
|
|
||||||
// 手动结束
|
// 手动结束
|
||||||
// StpUtil.openSafe(2);
|
// StpUtil.openSafe(2);
|
||||||
StpUtil.closeSafe();
|
StpUtil.closeSafe();
|
||||||
Assert.assertFalse(StpUtil.isSafe());
|
Assertions.assertFalse(StpUtil.isSafe());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -2,14 +2,12 @@ package com.pj.test;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.AfterClass;
|
import org.junit.jupiter.api.AfterAll;
|
||||||
import org.junit.Assert;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
import cn.dev33.satoken.SaManager;
|
import cn.dev33.satoken.SaManager;
|
||||||
import cn.dev33.satoken.config.SaTokenConfig;
|
import cn.dev33.satoken.config.SaTokenConfig;
|
||||||
@@ -24,7 +22,6 @@ import cn.dev33.satoken.stp.StpUtil;
|
|||||||
* @author kong
|
* @author kong
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(classes = StartUpApplication.class)
|
@SpringBootTest(classes = StartUpApplication.class)
|
||||||
public class ManyLoginTest {
|
public class ManyLoginTest {
|
||||||
|
|
||||||
@@ -33,12 +30,12 @@ public class ManyLoginTest {
|
|||||||
SaTokenDao dao = SaManager.getSaTokenDao();
|
SaTokenDao dao = SaManager.getSaTokenDao();
|
||||||
|
|
||||||
// 开始
|
// 开始
|
||||||
@BeforeClass
|
@BeforeAll
|
||||||
public static void beforeClass() {
|
public static void beforeClass() {
|
||||||
System.out.println("\n------------ 多端登录测试 star ...");
|
System.out.println("\n------------ 多端登录测试 star ...");
|
||||||
}
|
}
|
||||||
// 结束
|
// 结束
|
||||||
@AfterClass
|
@AfterAll
|
||||||
public static void afterClass() {
|
public static void afterClass() {
|
||||||
// System.out.println("\n---------- 多端登录测试 end ... \n");
|
// System.out.println("\n---------- 多端登录测试 end ... \n");
|
||||||
}
|
}
|
||||||
@@ -54,7 +51,7 @@ public class ManyLoginTest {
|
|||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
String token2 = StpUtil.getTokenValue();
|
String token2 = StpUtil.getTokenValue();
|
||||||
|
|
||||||
Assert.assertEquals(token1, token2);
|
Assertions.assertEquals(token1, token2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:并发登录、共享token、不同端
|
// 测试:并发登录、共享token、不同端
|
||||||
@@ -68,7 +65,7 @@ public class ManyLoginTest {
|
|||||||
StpUtil.login(10001, "PC");
|
StpUtil.login(10001, "PC");
|
||||||
String token2 = StpUtil.getTokenValue();
|
String token2 = StpUtil.getTokenValue();
|
||||||
|
|
||||||
Assert.assertNotEquals(token1, token2);
|
Assertions.assertNotEquals(token1, token2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:并发登录、不共享token
|
// 测试:并发登录、不共享token
|
||||||
@@ -82,7 +79,7 @@ public class ManyLoginTest {
|
|||||||
StpUtil.login(10001);
|
StpUtil.login(10001);
|
||||||
String token2 = StpUtil.getTokenValue();
|
String token2 = StpUtil.getTokenValue();
|
||||||
|
|
||||||
Assert.assertNotEquals(token1, token2);
|
Assertions.assertNotEquals(token1, token2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:禁并发登录,后者顶出前者
|
// 测试:禁并发登录,后者顶出前者
|
||||||
@@ -97,15 +94,15 @@ public class ManyLoginTest {
|
|||||||
String token2 = StpUtil.getTokenValue();
|
String token2 = StpUtil.getTokenValue();
|
||||||
|
|
||||||
// token不同
|
// token不同
|
||||||
Assert.assertNotEquals(token1, token2);
|
Assertions.assertNotEquals(token1, token2);
|
||||||
|
|
||||||
// token1会被标记为:已被顶下线
|
// token1会被标记为:已被顶下线
|
||||||
Assert.assertEquals(dao.get("satoken:login:token:" + token1), "-4");
|
Assertions.assertEquals(dao.get("satoken:login:token:" + token1), "-4");
|
||||||
|
|
||||||
// User-Session里的 token1 签名会被移除
|
// User-Session里的 token1 签名会被移除
|
||||||
List<TokenSign> tokenSignList = StpUtil.getSessionByLoginId(10001).getTokenSignList();
|
List<TokenSign> tokenSignList = StpUtil.getSessionByLoginId(10001).getTokenSignList();
|
||||||
for (TokenSign tokenSign : tokenSignList) {
|
for (TokenSign tokenSign : tokenSignList) {
|
||||||
Assert.assertNotEquals(tokenSign.getValue(), token1);
|
Assertions.assertNotEquals(tokenSign.getValue(), token1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,13 +124,13 @@ public class ManyLoginTest {
|
|||||||
StpUtil.logout(10001);
|
StpUtil.logout(10001);
|
||||||
|
|
||||||
// 三个Token应该全部无效
|
// 三个Token应该全部无效
|
||||||
Assert.assertNull(dao.get("satoken:login:token:" + token1));
|
Assertions.assertNull(dao.get("satoken:login:token:" + token1));
|
||||||
Assert.assertNull(dao.get("satoken:login:token:" + token2));
|
Assertions.assertNull(dao.get("satoken:login:token:" + token2));
|
||||||
Assert.assertNull(dao.get("satoken:login:token:" + token3));
|
Assertions.assertNull(dao.get("satoken:login:token:" + token3));
|
||||||
|
|
||||||
// User-Session也应该被清除掉
|
// User-Session也应该被清除掉
|
||||||
Assert.assertNull(StpUtil.getSessionByLoginId(10001, false));
|
Assertions.assertNull(StpUtil.getSessionByLoginId(10001, false));
|
||||||
Assert.assertNull(dao.getSession("satoken:login:session:" + 10001));
|
Assertions.assertNull(dao.getSession("satoken:login:session:" + 10001));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:多端登录,一起强制踢下线
|
// 测试:多端登录,一起强制踢下线
|
||||||
@@ -154,13 +151,13 @@ public class ManyLoginTest {
|
|||||||
StpUtil.kickout(10001);
|
StpUtil.kickout(10001);
|
||||||
|
|
||||||
// 三个Token应该全部无效
|
// 三个Token应该全部无效
|
||||||
Assert.assertEquals(dao.get("satoken:login:token:" + token1), "-5");
|
Assertions.assertEquals(dao.get("satoken:login:token:" + token1), "-5");
|
||||||
Assert.assertEquals(dao.get("satoken:login:token:" + token2), "-5");
|
Assertions.assertEquals(dao.get("satoken:login:token:" + token2), "-5");
|
||||||
Assert.assertEquals(dao.get("satoken:login:token:" + token3), "-5");
|
Assertions.assertEquals(dao.get("satoken:login:token:" + token3), "-5");
|
||||||
|
|
||||||
// User-Session也应该被清除掉
|
// User-Session也应该被清除掉
|
||||||
Assert.assertNull(StpUtil.getSessionByLoginId(10001, false));
|
Assertions.assertNull(StpUtil.getSessionByLoginId(10001, false));
|
||||||
Assert.assertNull(dao.getSession("satoken:login:session:" + 10001));
|
Assertions.assertNull(dao.getSession("satoken:login:session:" + 10001));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 测试:多账号模式,在一个账号体系里登录成功,在另一个账号体系不会校验通过
|
// 测试:多账号模式,在一个账号体系里登录成功,在另一个账号体系不会校验通过
|
||||||
@@ -173,7 +170,7 @@ public class ManyLoginTest {
|
|||||||
|
|
||||||
StpLogic stp = new StpLogic("user");
|
StpLogic stp = new StpLogic("user");
|
||||||
|
|
||||||
Assert.assertNotNull(StpUtil.getLoginIdByToken(token1));
|
Assertions.assertNotNull(StpUtil.getLoginIdByToken(token1));
|
||||||
Assert.assertNull(stp.getLoginIdByToken(token1));
|
Assertions.assertNull(stp.getLoginIdByToken(token1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user