From 3cc66f911c6a7b0d55ddb58c67e16963195ea351 Mon Sep 17 00:00:00 2001 From: zhaoxiaohao <279049017@qq.com> Date: Mon, 08 Mar 2021 18:42:39 +0800 Subject: [PATCH] 账号强制被踢出登录状态逻辑的优化 --- kidgrow-uaa/kidgrow-uaa-server/src/main/java/com/kidgrow/oauth2/handler/OauthLogoutHandler.java | 18 ++++++++++++++++++ 1 files changed, 18 insertions(+), 0 deletions(-) diff --git a/kidgrow-uaa/kidgrow-uaa-server/src/main/java/com/kidgrow/oauth2/handler/OauthLogoutHandler.java b/kidgrow-uaa/kidgrow-uaa-server/src/main/java/com/kidgrow/oauth2/handler/OauthLogoutHandler.java index 823529c..7f31915 100644 --- a/kidgrow-uaa/kidgrow-uaa-server/src/main/java/com/kidgrow/oauth2/handler/OauthLogoutHandler.java +++ b/kidgrow-uaa/kidgrow-uaa-server/src/main/java/com/kidgrow/oauth2/handler/OauthLogoutHandler.java @@ -2,11 +2,15 @@ import cn.hutool.core.util.StrUtil; import com.kidgrow.authclient.util.AuthUtils; +import com.kidgrow.common.constant.SecurityConstants; +import com.kidgrow.redis.util.RedisUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.Authentication; import org.springframework.security.oauth2.common.OAuth2AccessToken; import org.springframework.security.oauth2.common.OAuth2RefreshToken; +import org.springframework.security.oauth2.provider.OAuth2Authentication; +import org.springframework.security.oauth2.provider.OAuth2Request; import org.springframework.security.oauth2.provider.token.TokenStore; import org.springframework.security.web.authentication.logout.LogoutHandler; import org.springframework.util.Assert; @@ -26,6 +30,8 @@ public class OauthLogoutHandler implements LogoutHandler { @Autowired private TokenStore tokenStore; + @Autowired + private RedisUtils redisUtils; @Override public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) { @@ -37,14 +43,26 @@ if(StrUtil.isNotEmpty(token)){ OAuth2AccessToken existingAccessToken = tokenStore.readAccessToken(token); OAuth2RefreshToken refreshToken; + String clientId="";//属于哪个服务 + String userName="";//用户的名称 if (existingAccessToken != null) { if (existingAccessToken.getRefreshToken() != null) { + //获取用户信息 + Object o = redisUtils.get(SecurityConstants.REDIS_TOKEN_AUTH + existingAccessToken.getValue()); + if(o!=null){ + OAuth2Authentication accessToken = (OAuth2Authentication)o; + OAuth2Request requestOuth = accessToken.getOAuth2Request(); + userName=accessToken.getName(); + clientId=requestOuth.getClientId(); + } log.info("remove refreshToken!", existingAccessToken.getRefreshToken()); refreshToken = existingAccessToken.getRefreshToken(); tokenStore.removeRefreshToken(refreshToken); } log.info("remove existingAccessToken!", existingAccessToken); tokenStore.removeAccessToken(existingAccessToken); + //将 token与 用户的关联 清除; + redisUtils.removeAll(SecurityConstants.REDIS_UNAME_TO_ACCESS+clientId+":"+userName); } } } -- Gitblit v1.8.0