提交 57cb7fb0 编写于 作者: 不合群的混子's avatar 不合群的混子

抽取 cache 接口,方便用户自行集成 cache

上级 92bc4ab3
...@@ -13,6 +13,9 @@ import java.util.concurrent.atomic.AtomicInteger; ...@@ -13,6 +13,9 @@ import java.util.concurrent.atomic.AtomicInteger;
*/ */
public enum AuthCacheScheduler { public enum AuthCacheScheduler {
/**
* 当前实例
*/
INSTANCE; INSTANCE;
private AtomicInteger cacheTaskNumber = new AtomicInteger(1); private AtomicInteger cacheTaskNumber = new AtomicInteger(1);
......
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);
}
}
package me.zhyd.oauth.cache; package me.zhyd.oauth.cache;
/** /**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com) * <p>
* @version 1.0 * State缓存接口,方便用户扩展
* @since 1.8 * </p>
*
* @author yangkai.shen
* @date Created in 2019-08-02 10:55
*/ */
public class AuthStateCache { public interface AuthStateCache {
private static AuthCache authCache = new AuthDefaultCache();
/** /**
* 存入缓存 * 存入缓存
* *
* @param key 缓存key * @param key 缓存key
* @param value 缓存内容 * @param value 缓存内容
*/ */
public static void cache(String key, String value) { void cache(String key, String value);
authCache.set(key, value);
}
/** /**
* 存入缓存 * 存入缓存
...@@ -25,9 +24,7 @@ public class AuthStateCache { ...@@ -25,9 +24,7 @@ public class AuthStateCache {
* @param value 缓存内容 * @param value 缓存内容
* @param timeout 指定缓存过期时间(毫秒) * @param timeout 指定缓存过期时间(毫秒)
*/ */
public static void cache(String key, String value, long timeout) { void cache(String key, String value, long timeout);
authCache.set(key, value, timeout);
}
/** /**
* 获取缓存内容 * 获取缓存内容
...@@ -35,9 +32,7 @@ public class AuthStateCache { ...@@ -35,9 +32,7 @@ public class AuthStateCache {
* @param key 缓存key * @param key 缓存key
* @return 缓存内容 * @return 缓存内容
*/ */
public static String get(String key) { String get(String key);
return authCache.get(key);
}
/** /**
* 是否存在key,如果对应key的value值已过期,也返回false * 是否存在key,如果对应key的value值已过期,也返回false
...@@ -45,7 +40,5 @@ public class AuthStateCache { ...@@ -45,7 +40,5 @@ public class AuthStateCache {
* @param key 缓存key * @param key 缓存key
* @return true:存在key,并且value没过期;false:key不存在或者已过期 * @return true:存在key,并且value没过期;false:key不存在或者已过期
*/ */
public static boolean containsKey(String key) { boolean containsKey(String key);
return authCache.containsKey(key);
}
} }
...@@ -3,6 +3,7 @@ package me.zhyd.oauth.request; ...@@ -3,6 +3,7 @@ package me.zhyd.oauth.request;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpResponse;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.cache.AuthDefaultStateCache;
import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthSource; import me.zhyd.oauth.config.AuthSource;
...@@ -28,10 +29,16 @@ import me.zhyd.oauth.utils.UuidUtils; ...@@ -28,10 +29,16 @@ import me.zhyd.oauth.utils.UuidUtils;
public abstract class AuthDefaultRequest implements AuthRequest { public abstract class AuthDefaultRequest implements AuthRequest {
protected AuthConfig config; protected AuthConfig config;
protected AuthSource source; protected AuthSource source;
protected AuthStateCache authStateCache;
public AuthDefaultRequest(AuthConfig config, AuthSource source) { public AuthDefaultRequest(AuthConfig config, AuthSource source) {
this(config, source, AuthDefaultStateCache.INSTANCE);
}
public AuthDefaultRequest(AuthConfig config, AuthSource source, AuthStateCache authStateCache) {
this.config = config; this.config = config;
this.source = source; this.source = source;
this.authStateCache = authStateCache;
if (!AuthChecker.isSupportedAuth(config, source)) { if (!AuthChecker.isSupportedAuth(config, source)) {
throw new AuthException(AuthResponseStatus.PARAMETER_INCOMPLETE); throw new AuthException(AuthResponseStatus.PARAMETER_INCOMPLETE);
} }
...@@ -189,7 +196,7 @@ public abstract class AuthDefaultRequest implements AuthRequest { ...@@ -189,7 +196,7 @@ public abstract class AuthDefaultRequest implements AuthRequest {
state = UuidUtils.getUUID(); state = UuidUtils.getUUID();
} }
// 缓存state // 缓存state
AuthStateCache.cache(state, state); authStateCache.cache(state, state);
return state; return state;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册