From 482bc3d9b7ca478a34f09ab90f7a49c2455a8df8 Mon Sep 17 00:00:00 2001 From: zlt Date: Fri, 12 Jul 2019 14:21:26 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=AD=E7=AD=BE=E6=97=B6=E9=97=B4=E6=AF=94?= =?UTF-8?q?=E4=BE=8B=E5=8F=82=E6=95=B0=E7=A7=BB=E5=88=B0RenewProperties?= =?UTF-8?q?=E4=B8=AD=EF=BC=8C=E6=96=B9=E4=BE=BF=E9=80=9A=E8=BF=87yml?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../oauth2/common/properties/RenewProperties.java | 5 +++++ .../oauth2/common/store/CustomRedisTokenStore.java | 11 ++--------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/properties/RenewProperties.java b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/properties/RenewProperties.java index e3c975d4..7dfa1ad7 100644 --- a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/properties/RenewProperties.java +++ b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/properties/RenewProperties.java @@ -31,4 +31,9 @@ public class RenewProperties { * 配置enable为true时才生效 */ private List exclusiveClientIds = new ArrayList<>(); + + /** + * 续签时间比例,当前剩余时间小于小于过期总时长的50%则续签 + */ + private Double timeRatio = 0.5; } diff --git a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/CustomRedisTokenStore.java b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/CustomRedisTokenStore.java index 024e1834..ffcc6ac6 100644 --- a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/CustomRedisTokenStore.java +++ b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/CustomRedisTokenStore.java @@ -5,8 +5,6 @@ import com.central.oauth2.common.properties.SecurityProperties; import org.springframework.data.redis.connection.RedisClusterConnection; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisConnectionFactory; -import org.springframework.data.redis.core.Cursor; -import org.springframework.data.redis.core.ScanOptions; import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; import org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken; import org.springframework.security.oauth2.common.OAuth2AccessToken; @@ -34,8 +32,7 @@ import java.util.List; * 优化自Spring Security的RedisTokenStore * 1. 支持redis cluster模式 * 2. 非cluster模式时使用pipeline减少连接次数 - * 3. CLIENT_ID_TO_ACCESS和UNAME_TO_ACCESS集合改为list,方便业务顺序遍历 - * 4. 自动续签token(可配置是否开启) + * 3. 自动续签token(可配置是否开启) * * @author zlt * @date 2019/7/7 @@ -47,10 +44,6 @@ public class CustomRedisTokenStore implements TokenStore { private static final String ACCESS_TO_REFRESH = "access_to_refresh:"; private static final String REFRESH = "refresh:"; private static final String REFRESH_TO_ACCESS = "refresh_to_access:"; - /** - * 续签时间比例,当前剩余时间小于小于过期总时长的50%则续签 - */ - private static final Double RENEW_RATIO = 0.5; private static final boolean springDataRedis_2_0 = ClassUtils.isPresent( "org.springframework.data.redis.connection.RedisStandaloneConfiguration", @@ -169,7 +162,7 @@ public class CustomRedisTokenStore implements TokenStore { if (validitySeconds > 0) { double expiresRatio = token.getExpiresIn() / (double)validitySeconds; //判断是否需要续签,当前剩余时间小于过期时长的50%则续签 - if (expiresRatio <= RENEW_RATIO) { + if (expiresRatio <= securityProperties.getAuth().getRenew().getTimeRatio()) { //更新AccessToken过期时间 DefaultOAuth2AccessToken oAuth2AccessToken = (DefaultOAuth2AccessToken) token; oAuth2AccessToken.setExpiration(new Date(System.currentTimeMillis() + (validitySeconds * 1000L)));