From 57cb7fb0d10750c685383031e86f1148a5c9597e Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Fri, 2 Aug 2019 11:15:37 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=8A=BD=E5=8F=96=20cache=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=EF=BC=8C=E6=96=B9=E4=BE=BF=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E8=87=AA=E8=A1=8C=E9=9B=86=E6=88=90=20cache?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhyd/oauth/cache/AuthCacheScheduler.java | 3 + .../oauth/cache/AuthDefaultStateCache.java | 65 +++++++++++++++++++ .../me/zhyd/oauth/cache/AuthStateCache.java | 29 ++++----- .../oauth/request/AuthDefaultRequest.java | 9 ++- 4 files changed, 87 insertions(+), 19 deletions(-) create mode 100644 src/main/java/me/zhyd/oauth/cache/AuthDefaultStateCache.java diff --git a/src/main/java/me/zhyd/oauth/cache/AuthCacheScheduler.java b/src/main/java/me/zhyd/oauth/cache/AuthCacheScheduler.java index fbdfa88..1cf7c01 100644 --- a/src/main/java/me/zhyd/oauth/cache/AuthCacheScheduler.java +++ b/src/main/java/me/zhyd/oauth/cache/AuthCacheScheduler.java @@ -13,6 +13,9 @@ import java.util.concurrent.atomic.AtomicInteger; */ public enum AuthCacheScheduler { + /** + * 当前实例 + */ INSTANCE; private AtomicInteger cacheTaskNumber = new AtomicInteger(1); diff --git a/src/main/java/me/zhyd/oauth/cache/AuthDefaultStateCache.java b/src/main/java/me/zhyd/oauth/cache/AuthDefaultStateCache.java new file mode 100644 index 0000000..eb0fe83 --- /dev/null +++ b/src/main/java/me/zhyd/oauth/cache/AuthDefaultStateCache.java @@ -0,0 +1,65 @@ +package me.zhyd.oauth.cache; + +/** + * @author yadong.zhang (yadong.zhang0415(a)gmail.com) + * @version 1.0 + * @since 1.8 + */ +public enum AuthDefaultStateCache implements AuthStateCache { + + /** + * 当前实例 + */ + INSTANCE; + + private AuthCache authCache; + + AuthDefaultStateCache() { + authCache = new AuthDefaultCache(); + } + + /** + * 存入缓存 + * + * @param key 缓存key + * @param value 缓存内容 + */ + @Override + public void cache(String key, String value) { + authCache.set(key, value); + } + + /** + * 存入缓存 + * + * @param key 缓存key + * @param value 缓存内容 + * @param timeout 指定缓存过期时间(毫秒) + */ + @Override + public void cache(String key, String value, long timeout) { + authCache.set(key, value, timeout); + } + + /** + * 获取缓存内容 + * + * @param key 缓存key + * @return 缓存内容 + */ + @Override + public String get(String key) { + return authCache.get(key); + } + + /** + * 是否存在key,如果对应key的value值已过期,也返回false + * + * @param key 缓存key + * @return true:存在key,并且value没过期;false:key不存在或者已过期 + */ + @Override + public boolean containsKey(String key) { + return authCache.containsKey(key); + } +} diff --git a/src/main/java/me/zhyd/oauth/cache/AuthStateCache.java b/src/main/java/me/zhyd/oauth/cache/AuthStateCache.java index e667829..77a11e2 100644 --- a/src/main/java/me/zhyd/oauth/cache/AuthStateCache.java +++ b/src/main/java/me/zhyd/oauth/cache/AuthStateCache.java @@ -1,22 +1,21 @@ package me.zhyd.oauth.cache; /** - * @author yadong.zhang (yadong.zhang0415(a)gmail.com) - * @version 1.0 - * @since 1.8 + *

+ * State缓存接口,方便用户扩展 + *

+ * + * @author yangkai.shen + * @date Created in 2019-08-02 10:55 */ -public class AuthStateCache { - private static AuthCache authCache = new AuthDefaultCache(); - +public interface AuthStateCache { /** * 存入缓存 * * @param key 缓存key * @param value 缓存内容 */ - public static void cache(String key, String value) { - authCache.set(key, value); - } + void cache(String key, String value); /** * 存入缓存 @@ -25,9 +24,7 @@ public class AuthStateCache { * @param value 缓存内容 * @param timeout 指定缓存过期时间(毫秒) */ - public static void cache(String key, String value, long timeout) { - authCache.set(key, value, timeout); - } + void cache(String key, String value, long timeout); /** * 获取缓存内容 @@ -35,9 +32,7 @@ public class AuthStateCache { * @param key 缓存key * @return 缓存内容 */ - public static String get(String key) { - return authCache.get(key); - } + String get(String key); /** * 是否存在key,如果对应key的value值已过期,也返回false @@ -45,7 +40,5 @@ public class AuthStateCache { * @param key 缓存key * @return true:存在key,并且value没过期;false:key不存在或者已过期 */ - public static boolean containsKey(String key) { - return authCache.containsKey(key); - } + boolean containsKey(String key); } diff --git a/src/main/java/me/zhyd/oauth/request/AuthDefaultRequest.java b/src/main/java/me/zhyd/oauth/request/AuthDefaultRequest.java index 401c8b4..da7028c 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthDefaultRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthDefaultRequest.java @@ -3,6 +3,7 @@ package me.zhyd.oauth.request; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import lombok.extern.slf4j.Slf4j; +import me.zhyd.oauth.cache.AuthDefaultStateCache; import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthSource; @@ -28,10 +29,16 @@ import me.zhyd.oauth.utils.UuidUtils; public abstract class AuthDefaultRequest implements AuthRequest { protected AuthConfig config; protected AuthSource source; + protected AuthStateCache authStateCache; public AuthDefaultRequest(AuthConfig config, AuthSource source) { + this(config, source, AuthDefaultStateCache.INSTANCE); + } + + public AuthDefaultRequest(AuthConfig config, AuthSource source, AuthStateCache authStateCache) { this.config = config; this.source = source; + this.authStateCache = authStateCache; if (!AuthChecker.isSupportedAuth(config, source)) { throw new AuthException(AuthResponseStatus.PARAMETER_INCOMPLETE); } @@ -189,7 +196,7 @@ public abstract class AuthDefaultRequest implements AuthRequest { state = UuidUtils.getUUID(); } // 缓存state - AuthStateCache.cache(state, state); + authStateCache.cache(state, state); return state; } -- GitLab