diff --git a/README.md b/README.md index 97ab46bc..f64dc4c5 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@

logo

-

sa-token v1.14.0

+

sa-token v1.15.0

这可能是史上功能最全的Java权限认证框架!

- + diff --git a/pom.xml b/pom.xml index 6cd9eb42..261a6ec4 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ cn.dev33 sa-token-parent pom - 1.14.0 + 1.15.0 sa-token @@ -38,7 +38,7 @@ - 1.14.0 + 1.15.0 1.8 utf-8 utf-8 diff --git a/sa-token-core/pom.xml b/sa-token-core/pom.xml index 42d42eae..c225eba8 100644 --- a/sa-token-core/pom.xml +++ b/sa-token-core/pom.xml @@ -7,7 +7,7 @@ cn.dev33 sa-token-parent - 1.14.0 + 1.15.0 jar diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/SaTokenManager.java b/sa-token-core/src/main/java/cn/dev33/satoken/SaTokenManager.java index f57b82f6..4b2562ef 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/SaTokenManager.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/SaTokenManager.java @@ -53,7 +53,7 @@ public class SaTokenManager { private static SaTokenDao saTokenDao; public static void setSaTokenDao(SaTokenDao saTokenDao) { if(SaTokenManager.saTokenDao != null && (SaTokenManager.saTokenDao instanceof SaTokenDaoDefaultImpl)) { - ((SaTokenDaoDefaultImpl)SaTokenManager.saTokenDao).endRefreshTimer(); + ((SaTokenDaoDefaultImpl)SaTokenManager.saTokenDao).endRefreshThread(); } SaTokenManager.saTokenDao = saTokenDao; } @@ -149,7 +149,7 @@ public class SaTokenManager { public static Map stpLogicMap = new HashMap(); /** * 向集合中 put 一个 StpLogic - * @param stpLogic + * @param stpLogic StpLogic */ public static void putStpLogic(StpLogic stpLogic) { stpLogicMap.put(stpLogic.getLoginKey(), stpLogic); diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/dao/SaTokenDaoDefaultImpl.java b/sa-token-core/src/main/java/cn/dev33/satoken/dao/SaTokenDaoDefaultImpl.java index 9f438521..4cb2418e 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/dao/SaTokenDaoDefaultImpl.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/dao/SaTokenDaoDefaultImpl.java @@ -4,12 +4,9 @@ package cn.dev33.satoken.dao; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Timer; import java.util.concurrent.ConcurrentHashMap; import cn.dev33.satoken.SaTokenManager; -import cn.dev33.satoken.util.SaTaskUtil; -import cn.dev33.satoken.util.SaTaskUtil.FunctionRunClass; import cn.dev33.satoken.util.SaTokenInsideUtil; /** @@ -34,7 +31,7 @@ public class SaTokenDaoDefaultImpl implements SaTokenDao { * 构造函数 */ public SaTokenDaoDefaultImpl() { - initRefreshTimer(); + initRefreshThread(); } @@ -166,9 +163,15 @@ public class SaTokenDaoDefaultImpl implements SaTokenDao { // --------------------- 定时清理过期数据 /** - * 定时任务对象 + * 执行数据清理的线程 */ - public Timer refreshTimer; + public Thread refreshThread; + + /** + * 是否继续执行数据清理的线程标记 + */ + public boolean refreshFlag; + /** * 清理所有已经过期的key @@ -183,30 +186,46 @@ public class SaTokenDaoDefaultImpl implements SaTokenDao { /** * 初始化定时任务 */ - public void initRefreshTimer() { - // 如果已经被初始化过了, 则停止它 - if(this.refreshTimer != null) { - this.endRefreshTimer(); - } - - // 开始新的定时任务 - if(SaTokenManager.getConfig().getDataRefreshPeriod() < 0) { + public void initRefreshThread() { + + // 如果配置了<=0的值,则不启动定时清理 + if(SaTokenManager.getConfig().getDataRefreshPeriod() <= 0) { return; } - int period = SaTokenManager.getConfig().getDataRefreshPeriod() * 1000; - this.refreshTimer = SaTaskUtil.setInterval(new FunctionRunClass() { - @Override - public void run() { - refreshDataMap(); + // 启动定时刷新 + this.refreshFlag = true; + this.refreshThread = new Thread(() -> { + for (;;) { + try { + try { + // 如果已经被标记为结束 + if(refreshFlag == false) { + return; + } + // 执行清理 + refreshDataMap(); + } catch (Exception e) { + e.printStackTrace(); + } + // 休眠N秒 + int dataRefreshPeriod = SaTokenManager.getConfig().getDataRefreshPeriod(); + if(dataRefreshPeriod <= 0) { + dataRefreshPeriod = 1; + } + Thread.sleep(dataRefreshPeriod * 1000); + } catch (Exception e) { + e.printStackTrace(); + } } - }, period, period); + }); + refreshThread.start(); } /** * 结束定时任务 */ - public void endRefreshTimer() { - this.refreshTimer.cancel(); + public void endRefreshThread() { + this.refreshFlag = false; } diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/session/SaSession.java b/sa-token-core/src/main/java/cn/dev33/satoken/session/SaSession.java index cd32a021..52964941 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/session/SaSession.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/session/SaSession.java @@ -298,6 +298,138 @@ public class SaSession implements Serializable { // ----------------------- 存取值 (类型转换) + /** + * 写值 + * @param key 名称 + * @param value 值 + */ + public void set(String key, Object value) { + dataMap.put(key, value); + update(); + } + + /** + * 写值(只有在此key原本无值的时候才会写入) + * @param key 名称 + * @param value 值 + */ + public void setDefaultValue(String key, Object value) { + if(has(key) == false) { + dataMap.put(key, value); + update(); + } + } + + /** + * 取值 + * @param key key + * @return 值 + */ + public Object get(String key) { + return dataMap.get(key); + } + + /** + * + * 取值 (指定默认值) + * @param 默认值的类型 + * @param key key + * @param defaultValue 取不到值时返回的默认值 + * @return 值 + */ + public T get(String key, T defaultValue) { + return getValueByDefaultValue(get(key), defaultValue); + } + + /** + * 取值 (转String类型) + * @param key key + * @return 值 + */ + public String getString(String key) { + Object value = get(key); + if(value == null) { + return null; + } + return String.valueOf(value); + } + + /** + * 取值 (转int类型) + * @param key key + * @return 值 + */ + public int getInt(String key) { + return getValueByDefaultValue(get(key), 0); + } + + /** + * 取值 (转long类型) + * @param key key + * @return 值 + */ + public long getLong(String key) { + return getValueByDefaultValue(get(key), 0L); + } + + /** + * 取值 (转double类型) + * @param key key + * @return 值 + */ + public double getDouble(String key) { + return getValueByDefaultValue(get(key), 0.0); + } + + /** + * 取值 (转float类型) + * @param key key + * @return 值 + */ + public float getFloat(String key) { + return getValueByDefaultValue(get(key), 0.0f); + } + + /** + * 取值 (指定转换类型) + * @param 泛型 + * @param key key + * @param cs 指定转换类型 + * @return 值 + */ + public T getModel(String key, Class cs) { + return getValueByClass(get(key), cs); + } + + /** + * 取值 (指定转换类型, 并指定值为Null时返回的默认值) + * @param 泛型 + * @param key key + * @param cs 指定转换类型 + * @param defaultValue 值为Null时返回的默认值 + * @return 值 + */ + @SuppressWarnings("unchecked") + public T getModel(String key, Class cs, Object defaultValue) { + Object value = get(key); + if(valueIsNull(value)) { + return (T)defaultValue; + } + return getValueByClass(value, cs); + } + + /** + * 是否含有某个key + * @param key has + * @return 是否含有 + */ + public boolean has(String key) { + return !valueIsNull(get(key)); + } + + + // --------- 工具方法 + /** * 判断一个值是否为null * @param value 指定值 @@ -308,52 +440,63 @@ public class SaSession implements Serializable { } /** - * 从Session中取值,并转化为Object类型 - * @param key key - * @return 值 + * 将指定值转化为指定类型 + * @param 泛型 + * @param obj 值 + * @param cs 类型 + * @return 转换后的值 */ - public Object getObject(String key) { - return getAttribute(key); + @SuppressWarnings("unchecked") + protected T getValueByClass(Object obj, Class cs) { + // 如果 obj 本来就是 cs 类型 + if(obj != null && obj.getClass().equals(cs)) { + return (T)obj; + } + // 开始转换 + String obj2 = String.valueOf(obj); + Object obj3 = null; + if (cs.equals(String.class)) { + obj3 = obj2; + } else if (cs.equals(int.class) || cs.equals(Integer.class)) { + obj3 = Integer.valueOf(obj2); + } else if (cs.equals(long.class) || cs.equals(Long.class)) { + obj3 = Long.valueOf(obj2); + } else if (cs.equals(short.class) || cs.equals(Short.class)) { + obj3 = Short.valueOf(obj2); + } else if (cs.equals(byte.class) || cs.equals(Byte.class)) { + obj3 = Byte.valueOf(obj2); + } else if (cs.equals(float.class) || cs.equals(Float.class)) { + obj3 = Float.valueOf(obj2); + } else if (cs.equals(double.class) || cs.equals(Double.class)) { + obj3 = Double.valueOf(obj2); + } else if (cs.equals(boolean.class) || cs.equals(Boolean.class)) { + obj3 = Boolean.valueOf(obj2); + } else { + obj3 = (T)obj; + } + return (T)obj3; } /** - * 从Session中取值,并转化为String类型 - * @param key key - * @return 值 + * 根据默认值来获取值 + * @param 泛型 + * @param value 值 + * @param defaultValue 默认值 + * @return 转换后的值 */ - public String getString(String key) { - Object value = getObject(key); - if(value == null) { - return null; - } - return String.valueOf(value); - } - - /** - * 从Session中取值,并转化为int类型,如果value为空,则返回0 - * @param key key - * @return 值 - */ - public int getInt(String key) { - Object value = getObject(key); + @SuppressWarnings("unchecked") + protected T getValueByDefaultValue(Object value, T defaultValue) { + + // 如果 obj 为 null,则直接返回默认值 if(valueIsNull(value)) { - return 0; + return (T)defaultValue; } - return Integer.valueOf(String.valueOf(value)); - } - - /** - * 从Session中取值,并转化为long类型,如果value为空,则返回0 - * @param key key - * @return 值 - */ - public long getLong(String key) { - Object value = getObject(key); - if(valueIsNull(value)) { - return 0; - } - return Long.valueOf(String.valueOf(value)); + + // 开始转换 + Class cs = (Class) defaultValue.getClass(); + return getValueByClass(value, cs); } + } diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpLogic.java b/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpLogic.java index c02e7741..14718e5a 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpLogic.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpLogic.java @@ -90,6 +90,7 @@ public class StpLogic { /** * 在当前会话写入当前tokenValue * @param tokenValue token值 + * @param cookieTimeout Cookie存活时间(秒) */ public void setTokenValue(String tokenValue, int cookieTimeout){ SaTokenConfig config = getConfig(); diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpUtil.java b/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpUtil.java index 78faacb9..e74cdd22 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpUtil.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpUtil.java @@ -39,6 +39,7 @@ public class StpUtil { /** * 在当前会话写入当前tokenValue * @param tokenValue token值 + * @param cookieTimeout Cookie存活时间(秒) */ public static void setTokenValue(String tokenValue, int cookieTimeout){ stpLogic.setTokenValue(tokenValue, cookieTimeout); diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/util/SaTaskUtil.java b/sa-token-core/src/main/java/cn/dev33/satoken/util/SaTaskUtil.java deleted file mode 100644 index d6e7c8af..00000000 --- a/sa-token-core/src/main/java/cn/dev33/satoken/util/SaTaskUtil.java +++ /dev/null @@ -1,60 +0,0 @@ -package cn.dev33.satoken.util; - -import java.util.Timer; -import java.util.TimerTask; - -/** - * 任务调度Util - * @author kong - * - */ -public class SaTaskUtil { - - /** - * 延时指定毫秒执行一个函数 - * @param fc 要执行的函数 - * @param delay 延时的毫秒数 - * @return timer任务对象 - */ - public static Timer setTimeout(FunctionRunClass fc, int delay) { - Timer timer = new Timer(); - timer.schedule(new TimerTask() { - @Override - public void run() { - fc.run(); - timer.cancel(); - } - }, delay); - return timer; - } - - /** - * 延时delay毫秒,每隔period毫秒执行一个函数 - * @param fc 要执行的函数 - * @param delay 延时的毫秒数 - * @param period 每隔多少毫秒执行一次 - * @return timer任务对象 - */ - public static Timer setInterval(FunctionRunClass fc, int delay, int period) { - Timer timer = new Timer(); - timer.schedule(new TimerTask() { - @Override - public void run() { - fc.run(); - } - }, delay, period); - return timer; - } - - /** - * 封装一个内部类,便于操作 - * @author kong - */ - public static interface FunctionRunClass{ - /** - * 要执行的方法 - */ - public void run(); - } - -} diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/util/SaTokenConsts.java b/sa-token-core/src/main/java/cn/dev33/satoken/util/SaTokenConsts.java index 304fb321..1f6ba0d7 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/util/SaTokenConsts.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/util/SaTokenConsts.java @@ -13,7 +13,7 @@ public class SaTokenConsts { /** * sa-token 版本号 */ - public static final String VERSION_NO = "v1.14.0"; + public static final String VERSION_NO = "v1.15.0"; /** * sa-token 开源地址 diff --git a/sa-token-dao-redis-jackson/pom.xml b/sa-token-dao-redis-jackson/pom.xml index a54a35f4..403b6e2c 100644 --- a/sa-token-dao-redis-jackson/pom.xml +++ b/sa-token-dao-redis-jackson/pom.xml @@ -7,7 +7,7 @@ cn.dev33 sa-token-parent - 1.14.0 + 1.15.0 jar diff --git a/sa-token-dao-redis/pom.xml b/sa-token-dao-redis/pom.xml index f491fce8..ba28f0c3 100644 --- a/sa-token-dao-redis/pom.xml +++ b/sa-token-dao-redis/pom.xml @@ -7,7 +7,7 @@ cn.dev33 sa-token-parent - 1.14.0 + 1.15.0 jar diff --git a/sa-token-demo-jwt/pom.xml b/sa-token-demo-jwt/pom.xml index c84fce1c..665d5a0b 100644 --- a/sa-token-demo-jwt/pom.xml +++ b/sa-token-demo-jwt/pom.xml @@ -16,7 +16,7 @@ - 1.14.0 + 1.15.0 diff --git a/sa-token-demo-oauth2/sa-token-demo-oauth2-client/pom.xml b/sa-token-demo-oauth2/sa-token-demo-oauth2-client/pom.xml index a9736794..7295782e 100644 --- a/sa-token-demo-oauth2/sa-token-demo-oauth2-client/pom.xml +++ b/sa-token-demo-oauth2/sa-token-demo-oauth2-client/pom.xml @@ -17,7 +17,7 @@ 1.8 3.1.1 - 1.14.0 + 1.15.0 diff --git a/sa-token-demo-oauth2/sa-token-demo-oauth2-server/pom.xml b/sa-token-demo-oauth2/sa-token-demo-oauth2-server/pom.xml index 5a0d8038..f88cda08 100644 --- a/sa-token-demo-oauth2/sa-token-demo-oauth2-server/pom.xml +++ b/sa-token-demo-oauth2/sa-token-demo-oauth2-server/pom.xml @@ -17,7 +17,7 @@ 1.8 3.1.1 - 1.14.0 + 1.15.0 @@ -39,7 +39,7 @@ cn.dev33 sa-token-oauth2 - 1.14.0-alpha + 1.15.0-alpha diff --git a/sa-token-demo-springboot/pom.xml b/sa-token-demo-springboot/pom.xml index 6794467c..f59669e0 100644 --- a/sa-token-demo-springboot/pom.xml +++ b/sa-token-demo-springboot/pom.xml @@ -16,7 +16,7 @@ - 1.14.0 + 1.15.0 diff --git a/sa-token-demo-springboot/src/main/java/com/pj/SaTokenDemoApplication.java b/sa-token-demo-springboot/src/main/java/com/pj/SaTokenDemoApplication.java index 61e902b8..fee34dc7 100644 --- a/sa-token-demo-springboot/src/main/java/com/pj/SaTokenDemoApplication.java +++ b/sa-token-demo-springboot/src/main/java/com/pj/SaTokenDemoApplication.java @@ -1,5 +1,6 @@ package com.pj; + import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; diff --git a/sa-token-doc/doc/README.md b/sa-token-doc/doc/README.md index b7ea06d8..291e361c 100644 --- a/sa-token-doc/doc/README.md +++ b/sa-token-doc/doc/README.md @@ -1,11 +1,11 @@

logo

-

sa-token v1.14.0

+

sa-token v1.15.0

这可能是史上功能最全的Java权限认证框架!

- + diff --git a/sa-token-doc/doc/index.html b/sa-token-doc/doc/index.html index 86f4c321..4f36385a 100644 --- a/sa-token-doc/doc/index.html +++ b/sa-token-doc/doc/index.html @@ -21,6 +21,7 @@