From 5752b0e7f22751b2734fecaa0ee2a11f1f0887a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= Date: Mon, 3 Aug 2020 21:18:25 +0800 Subject: [PATCH] :recycle: Refactoring code. @Cleanup replace try resource --- .../component/PigRedisTokenStore.java | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigRedisTokenStore.java b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigRedisTokenStore.java index 2de0a00b..e21f8c8e 100644 --- a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigRedisTokenStore.java +++ b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigRedisTokenStore.java @@ -2,6 +2,7 @@ package com.pig4cloud.pig.common.security.component; import cn.hutool.core.util.StrUtil; import com.pig4cloud.pig.common.core.constant.CacheConstants; +import lombok.Cleanup; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisStringCommands; @@ -33,15 +34,15 @@ public class PigRedisTokenStore extends RedisTokenStore { @Override public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) { super.storeAccessToken(token, authentication); - try (RedisConnection connection = connectionFactory.getConnection()) { - // KEY - byte[] key = StrUtil.bytes(CacheConstants.PROJECT_OAUTH_TOKEN + authentication.getName()); - // value - byte[] tokenVal = StrUtil.bytes(token.getValue()); - RedisStringCommands stringCommand = connection.stringCommands(); - stringCommand.set(key, tokenVal, Expiration.seconds(token.getExpiresIn()), - RedisStringCommands.SetOption.SET_IF_ABSENT); - } + @Cleanup + RedisConnection connection = connectionFactory.getConnection(); + // KEY + byte[] key = StrUtil.bytes(CacheConstants.PROJECT_OAUTH_TOKEN + authentication.getName()); + // value + byte[] tokenVal = StrUtil.bytes(token.getValue()); + RedisStringCommands stringCommand = connection.stringCommands(); + stringCommand.set(key, tokenVal, Expiration.seconds(token.getExpiresIn()), + RedisStringCommands.SetOption.SET_IF_ABSENT); } /** @@ -51,12 +52,12 @@ public class PigRedisTokenStore extends RedisTokenStore { @Override public void removeAccessToken(OAuth2AccessToken accessToken) { super.removeAccessToken(accessToken); - try (RedisConnection connection = connectionFactory.getConnection()) { - // KEY - OAuth2Authentication authentication = readAuthentication(accessToken); - byte[] key = StrUtil.bytes(CacheConstants.PROJECT_OAUTH_TOKEN + authentication.getName()); - connection.del(key); - } + @Cleanup + RedisConnection connection = connectionFactory.getConnection(); + // KEY + OAuth2Authentication authentication = readAuthentication(accessToken); + byte[] key = StrUtil.bytes(CacheConstants.PROJECT_OAUTH_TOKEN + authentication.getName()); + connection.del(key); } }