...
 
Commits (8)
    https://gitcode.net/shengzhang_/sa-token/-/commit/953ba965aa677a3eb7a977f4cdb10832629090ea 稍微简化一下cn.dev33.satoken.action.SaTokenActionDefaultImpl.checkMethodAnnotation的... 2021-05-24T11:24:22+08:00 xy 3293232930@qq.com https://gitcode.net/shengzhang_/sa-token/-/commit/ce0879c5b5ced2430616f08992e911ad90eae072 修改为valueOf的方式获取值, 避免产生新的实例 2021-05-24T11:46:47+08:00 xy 3293232930@qq.com https://gitcode.net/shengzhang_/sa-token/-/commit/afa09ac5e624a96df4a2b870175bb58431609c62 [refractor]update some bad smell code in SaTokenConfig 2021-05-24T15:44:21+08:00 guide koushuangbwcx@163.com https://gitcode.net/shengzhang_/sa-token/-/commit/ec9f12511c8a4c06836242cab745fb20cb485535 修改元素匹配逻辑, 目测会快很多 2021-05-24T17:24:50+08:00 xy 3293232930@qq.com https://gitcode.net/shengzhang_/sa-token/-/commit/73bdb6c89a950a71274924588de9fc6c3cecefd5 Merge pull request #87 from Snailclimb/dev 2021-05-24T20:49:11+08:00 click33 36243476+click33@users.noreply.github.com [refractor]update some bad smell code in SaTokenConfig https://gitcode.net/shengzhang_/sa-token/-/commit/34a62a2aa0843247a0527b3703ca015cfe96b48d Merge pull request #86 from ly-chn/dev 2021-05-24T22:06:02+08:00 click33 36243476+click33@users.noreply.github.com 简化部分逻辑 https://gitcode.net/shengzhang_/sa-token/-/commit/a54a8cc455ead5255fb652363d6971033821b7ca SaSession.get() 增加缓存API,简化代码 2021-05-24T22:42:50+08:00 shengzhang 2393584716@qq.com https://gitcode.net/shengzhang_/sa-token/-/commit/e9ab3758540309c860427a54ea969855e19476c3 整理pr 2021-05-24T23:03:23+08:00 shengzhang 2393584716@qq.com
package cn.dev33.satoken.action;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.List;
import java.util.UUID;
......@@ -71,12 +72,19 @@ public class SaTokenActionDefaultImpl implements SaTokenAction {
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;
}
......@@ -87,46 +95,36 @@ public class SaTokenActionDefaultImpl implements SaTokenAction {
@Override
public void checkMethodAnnotation(Method method) {
// 获取这个 Method 所属的 Class
Class<?> clazz = method.getDeclaringClass();
// 先校验 Method 所属 Class 上的注解
validateAnnotation(method.getDeclaringClass());
// 从 Class 校验 @SaCheckLogin 注解
if(clazz.isAnnotationPresent(SaCheckLogin.class)) {
SaCheckLogin at = clazz.getAnnotation(SaCheckLogin.class);
SaManager.getStpLogic(at.key()).checkByAnnotation(at);
}
// 从 Class 校验 @SaCheckRole 注解
if(clazz.isAnnotationPresent(SaCheckRole.class)) {
SaCheckRole at = clazz.getAnnotation(SaCheckRole.class);
SaManager.getStpLogic(at.key()).checkByAnnotation(at);
}
// 从 Class 校验 @SaCheckPermission 注解
if(clazz.isAnnotationPresent(SaCheckPermission.class)) {
SaCheckPermission at = clazz.getAnnotation(SaCheckPermission.class);
SaManager.getStpLogic(at.key()).checkByAnnotation(at);
}
// 再校验 Method 上的注解
validateAnnotation(method);
}
// 从 Method 校验 @SaCheckLogin 注解
if(method.isAnnotationPresent(SaCheckLogin.class)) {
SaCheckLogin at = method.getAnnotation(SaCheckLogin.class);
/**
* 从指定元素校验注解
* @param target see note
*/
protected void validateAnnotation(AnnotatedElement target) {
// 校验 @SaCheckLogin 注解
if(target.isAnnotationPresent(SaCheckLogin.class)) {
SaCheckLogin at = target.getAnnotation(SaCheckLogin.class);
SaManager.getStpLogic(at.key()).checkByAnnotation(at);
}
// 从 Method 校验 @SaCheckRole 注解
if(method.isAnnotationPresent(SaCheckRole.class)) {
SaCheckRole at = method.getAnnotation(SaCheckRole.class);
// 校验 @SaCheckRole 注解
if(target.isAnnotationPresent(SaCheckRole.class)) {
SaCheckRole at = target.getAnnotation(SaCheckRole.class);
SaManager.getStpLogic(at.key()).checkByAnnotation(at);
}
// 从 Method 校验 @SaCheckPermission 注解
if(method.isAnnotationPresent(SaCheckPermission.class)) {
SaCheckPermission at = method.getAnnotation(SaCheckPermission.class);
// 校验 @SaCheckPermission 注解
if(target.isAnnotationPresent(SaCheckPermission.class)) {
SaCheckPermission at = target.getAnnotation(SaCheckPermission.class);
SaManager.getStpLogic(at.key()).checkByAnnotation(at);
}
}
}
......@@ -94,9 +94,7 @@ public class SaTokenConfigFactory {
Object valueConvert = getObjectByClass(value, field.getType());
field.setAccessible(true);
field.set(obj, valueConvert);
} catch (IllegalArgumentException e) {
throw new RuntimeException("属性赋值出错:" + field.getName(), e);
} catch (IllegalAccessException e) {
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new RuntimeException("属性赋值出错:" + field.getName(), e);
}
}
......@@ -112,23 +110,23 @@ public class SaTokenConfigFactory {
*/
@SuppressWarnings("unchecked")
private static <T> T getObjectByClass(String str, Class<T> cs) {
Object value = null;
Object value;
if (str == null) {
value = null;
} else if (cs.equals(String.class)) {
value = str;
} else if (cs.equals(int.class) || cs.equals(Integer.class)) {
value = new Integer(str);
value = Integer.valueOf(str);
} else if (cs.equals(long.class) || cs.equals(Long.class)) {
value = new Long(str);
value = Long.valueOf(str);
} else if (cs.equals(short.class) || cs.equals(Short.class)) {
value = new Short(str);
value = Short.valueOf(str);
} else if (cs.equals(float.class) || cs.equals(Float.class)) {
value = new Float(str);
value = Float.valueOf(str);
} else if (cs.equals(double.class) || cs.equals(Double.class)) {
value = new Double(str);
value = Double.valueOf(str);
} else if (cs.equals(boolean.class) || cs.equals(Boolean.class)) {
value = new Boolean(str);
value = Boolean.valueOf(str);
} else {
throw new RuntimeException("未能将值:" + str + ",转换类型为:" + cs, null);
}
......
package cn.dev33.satoken.fun;
/**
* 设定一个函数,并返回一个值,方便在Lambda表达式下的函数式编程
* @author kong
*
*/
public interface SaRetFunction {
/**
* 执行的方法
* @return 返回值
*/
public Object run();
}
......@@ -8,6 +8,7 @@ import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.fun.SaRetFunction;
/**
* Session Model
......@@ -325,6 +326,24 @@ public class SaSession implements Serializable {
return getValueByDefaultValue(get(key), defaultValue);
}
/**
*
* 取值 (如果值为null,则执行fun函数获取值)
* @param <T> 返回值的类型
* @param key key
* @param fun 值为null时执行的函数
* @return 值
*/
@SuppressWarnings("unchecked")
public <T> T get(String key, SaRetFunction fun) {
Object value = get(key);
if(value == null) {
value = fun.run();
set(key, value);
}
return (T) value;
}
/**
* 取值 (转String类型)
* @param key key
......
package cn.dev33.satoken.stp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
......@@ -37,7 +37,7 @@ public class StpLogic {
/**
* 持久化的key前缀,多账号认证体系时以此值区分,比如:login、user、admin
*/
public String loginKey = "";
public String loginKey;
/**
* 初始化StpLogic, 并指定LoginKey
......@@ -106,7 +106,7 @@ public class StpLogic {
}
// 注入Cookie
if(config.getIsReadCookie() == true){
if(config.getIsReadCookie()){
SaResponse response = SaHolder.getResponse();
response.addCookie(getTokenName(), tokenValue, "/", config.getCookieDomain(), cookieTimeout);
}
......@@ -143,7 +143,7 @@ public class StpLogic {
// 5. 如果打开了前缀模式
String tokenPrefix = getConfig().getTokenPrefix();
if(SaFoxUtil.isEmpty(tokenPrefix) == false && SaFoxUtil.isEmpty(tokenValue) == false) {
if(!SaFoxUtil.isEmpty(tokenPrefix) && !SaFoxUtil.isEmpty(tokenValue)) {
// 如果token以指定的前缀开头, 则裁剪掉它, 否则视为未提供token
if(tokenValue.startsWith(tokenPrefix + SaTokenConsts.TOKEN_CONNECTOR_CHAT)) {
tokenValue = tokenValue.substring(tokenPrefix.length() + SaTokenConsts.TOKEN_CONNECTOR_CHAT.length());
......@@ -226,9 +226,9 @@ public class StpLogic {
// ------ 2、生成一个token
String tokenValue = null;
// --- 如果允许并发登录
if(config.getAllowConcurrentLogin() == true) {
if(config.getAllowConcurrentLogin()) {
// 如果配置为共享token, 则尝试从Session签名记录里取出token
if(config.getIsShare() == true) {
if(config.getIsShare()) {
tokenValue = getTokenValueByLoginId(loginId, loginModel.getDevice());
}
} else {
......@@ -290,7 +290,7 @@ public class StpLogic {
return;
}
// 如果打开了cookie模式,第一步,先把cookie清除掉
if(getConfig().getIsReadCookie() == true){
if(getConfig().getIsReadCookie()){
SaHolder.getResponse().deleteCookie(getTokenName());
}
logoutByTokenValue(tokenValue);
......@@ -539,7 +539,7 @@ public class StpLogic {
* @return 账号id
*/
public int getLoginIdAsInt() {
return Integer.valueOf(String.valueOf(getLoginId()));
return Integer.parseInt(String.valueOf(getLoginId()));
}
/**
......@@ -547,7 +547,7 @@ public class StpLogic {
* @return 账号id
*/
public long getLoginIdAsLong() {
return Long.valueOf(String.valueOf(getLoginId()));
return Long.parseLong(String.valueOf(getLoginId()));
}
/**
......@@ -865,7 +865,7 @@ public class StpLogic {
return SaTokenDao.NOT_VALUE_EXPIRE;
}
// 计算相差时间
long lastActivityTime = Long.valueOf(lastActivityTimeString);
long lastActivityTime = Long.parseLong(lastActivityTimeString);
long apartSecond = (System.currentTimeMillis() - lastActivityTime) / 1000;
long timeout = getConfig().getActivityTimeout() - apartSecond;
// 如果 < 0, 代表已经过期 ,返回-2
......@@ -904,7 +904,7 @@ public class StpLogic {
* @param role 角色标识
*/
public void checkRole(String role) {
if(hasRole(role) == false) {
if(!hasRole(role)) {
throw new NotRoleException(role, this.loginKey);
}
}
......@@ -917,7 +917,7 @@ public class StpLogic {
Object loginId = getLoginId();
List<String> roleList = SaManager.getStpInterface().getRoleList(loginId, loginKey);
for (String role : roleArray) {
if(SaManager.getSaTokenAction().hasElement(roleList, role) == false) {
if(!SaManager.getSaTokenAction().hasElement(roleList, role)) {
throw new NotRoleException(role, this.loginKey);
}
}
......@@ -931,7 +931,7 @@ public class StpLogic {
Object loginId = getLoginId();
List<String> roleList = SaManager.getStpInterface().getRoleList(loginId, loginKey);
for (String role : roleArray) {
if(SaManager.getSaTokenAction().hasElement(roleList, role) == true) {
if(SaManager.getSaTokenAction().hasElement(roleList, role)) {
// 有的话提前退出
return;
}
......@@ -983,7 +983,7 @@ public class StpLogic {
Object loginId = getLoginId();
List<String> permissionList = SaManager.getStpInterface().getPermissionList(loginId, loginKey);
for (String permission : permissionArray) {
if(SaManager.getSaTokenAction().hasElement(permissionList, permission) == false) {
if(!SaManager.getSaTokenAction().hasElement(permissionList, permission)) {
throw new NotPermissionException(permission, this.loginKey);
}
}
......@@ -997,7 +997,7 @@ public class StpLogic {
Object loginId = getLoginId();
List<String> permissionList = SaManager.getStpInterface().getPermissionList(loginId, loginKey);
for (String permission : permissionArray) {
if(SaManager.getSaTokenAction().hasElement(permissionList, permission) == true) {
if(SaManager.getSaTokenAction().hasElement(permissionList, permission)) {
// 有的话提前退出
return;
}
......@@ -1053,7 +1053,7 @@ public class StpLogic {
// 如果session为null的话直接返回空集合
SaSession session = getSessionByLoginId(loginId, false);
if(session == null) {
return Arrays.asList();
return Collections.emptyList();
}
// 遍历解析
List<TokenSign> tokenSignList = session.getTokenSignList();
......@@ -1077,7 +1077,7 @@ public class StpLogic {
return null;
}
// 如果还未登录,直接返回 null
if(isLogin() == false) {
if(!isLogin()) {
return null;
}
// 如果session为null的话直接返回 null
......
......@@ -129,14 +129,14 @@ public class SaFoxUtil {
}
return list2;
}
/**
* 字符串模糊匹配
* <p>example:
* <p> user* user-add -- true
* <p> user* art-add -- false
* @param patt 表达式
* @param str 待匹配的字符串
* @param patt 表达式
* @param str 待匹配的字符串
* @return 是否可以匹配
*/
public static boolean vagueMatch(String patt, String str) {
......
......@@ -17,8 +17,6 @@ spring:
is-share: true
# token风格
token-style: uuid
# redis配置
redis:
# Redis数据库索引(默认为0)
......
......@@ -241,7 +241,6 @@ public class TestController {
@RequestMapping("test")
public AjaxJson test() {
System.out.println("进来了");
System.out.println(StpUtil.getTokenInfo());
return AjaxJson.getSuccess("访问成功");
}
......