From f98008cce01da104c6598a79e622e9714977a3f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= Date: Mon, 3 Aug 2020 13:27:29 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20Introducing=20new=20features.=20?= =?UTF-8?q?=E4=BB=A4=E7=89=8C=E7=AE=A1=E7=90=86=E6=94=AF=E6=8C=81=E6=A0=B9?= =?UTF-8?q?=E6=8D=AE=E7=94=A8=E6=88=B7=E5=90=8D=E6=A3=80=E7=B4=A2=20closed?= =?UTF-8?q?=20#I1PYDZ?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/AuthorizationServerConfig.java | 4 +- .../common/core/constant/CacheConstants.java | 5 ++ .../component/PigRedisTokenStore.java | 64 +++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigRedisTokenStore.java diff --git a/pig-auth/src/main/java/com/pig4cloud/pig/auth/config/AuthorizationServerConfig.java b/pig-auth/src/main/java/com/pig4cloud/pig/auth/config/AuthorizationServerConfig.java index d4686913..c5f3855c 100755 --- a/pig-auth/src/main/java/com/pig4cloud/pig/auth/config/AuthorizationServerConfig.java +++ b/pig-auth/src/main/java/com/pig4cloud/pig/auth/config/AuthorizationServerConfig.java @@ -20,6 +20,7 @@ package com.pig4cloud.pig.auth.config; import com.pig4cloud.pig.common.core.constant.CacheConstants; import com.pig4cloud.pig.common.core.constant.SecurityConstants; +import com.pig4cloud.pig.common.security.component.PigRedisTokenStore; import com.pig4cloud.pig.common.security.component.PigWebResponseExceptionTranslator; import com.pig4cloud.pig.common.security.service.PigClientDetailsService; import com.pig4cloud.pig.common.security.service.PigUser; @@ -39,7 +40,6 @@ import org.springframework.security.oauth2.config.annotation.web.configurers.Aut import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer; import org.springframework.security.oauth2.provider.token.TokenEnhancer; import org.springframework.security.oauth2.provider.token.TokenStore; -import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore; import javax.sql.DataSource; import java.util.HashMap; @@ -87,7 +87,7 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap @Bean public TokenStore tokenStore() { - RedisTokenStore tokenStore = new RedisTokenStore(redisConnectionFactory); + PigRedisTokenStore tokenStore = new PigRedisTokenStore(redisConnectionFactory); tokenStore.setPrefix(CacheConstants.PROJECT_OAUTH_ACCESS); return tokenStore; } diff --git a/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/constant/CacheConstants.java b/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/constant/CacheConstants.java index ed021268..0e6fcb9d 100644 --- a/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/constant/CacheConstants.java +++ b/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/constant/CacheConstants.java @@ -31,6 +31,11 @@ public interface CacheConstants { */ String PROJECT_OAUTH_ACCESS = "pig_oauth:access:"; + /** + * oauth 缓存令牌前缀 + */ + String PROJECT_OAUTH_TOKEN = "pig_oauth:token:"; + /** * 验证码前缀 */ 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 new file mode 100644 index 00000000..eee829a9 --- /dev/null +++ b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigRedisTokenStore.java @@ -0,0 +1,64 @@ +package com.pig4cloud.pig.common.security.component; + +import cn.hutool.core.util.StrUtil; +import com.pig4cloud.pig.common.core.constant.CacheConstants; +import org.springframework.data.redis.connection.RedisConnection; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.connection.RedisStringCommands; +import org.springframework.data.redis.core.types.Expiration; +import org.springframework.security.oauth2.common.OAuth2AccessToken; +import org.springframework.security.oauth2.provider.OAuth2Authentication; +import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore; + +/** + * @author lengleng + * @date 2020/8/3 + *

+ * 重写默认tokenStore 保存 username and token 关系 + */ +public class PigRedisTokenStore extends RedisTokenStore { + + private RedisConnectionFactory connectionFactory; + + public PigRedisTokenStore(RedisConnectionFactory connectionFactory) { + super(connectionFactory); + this.connectionFactory = connectionFactory; + } + + /** + * 序列化保存认证信息 + * @param token token 详细信息 + * @param authentication 认证相关信息 + */ + @Override + public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) { + super.storeAccessToken(token, authentication); + // KEY + byte[] key = StrUtil.bytes(CacheConstants.PROJECT_OAUTH_TOKEN + authentication.getName()); + // value + byte[] tokenVal = StrUtil.bytes(token.getValue()); + // 获取redis连接 + RedisConnection connection = connectionFactory.getConnection(); + RedisStringCommands stringCommand = connection.stringCommands(); + stringCommand.set(key, tokenVal, Expiration.seconds(token.getExpiresIn()), + RedisStringCommands.SetOption.SET_IF_ABSENT); + connection.close(); + } + + /** + * 删除token + * @param accessToken token + */ + @Override + public void removeAccessToken(OAuth2AccessToken accessToken) { + super.removeAccessToken(accessToken); + // KEY + OAuth2Authentication authentication = readAuthentication(accessToken); + byte[] key = StrUtil.bytes(CacheConstants.PROJECT_OAUTH_TOKEN + authentication.getName()); + // 获取redis连接 + RedisConnection connection = connectionFactory.getConnection(); + connection.del(key); + connection.close(); + } + +}