diff --git a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/properties/AuthProperties.java b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/properties/AuthProperties.java index 0a3f6337..c3f77106 100644 --- a/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/properties/AuthProperties.java +++ b/zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/properties/AuthProperties.java @@ -30,7 +30,9 @@ public class AuthProperties { private UrlPermissionProperties urlPermission = new UrlPermissionProperties(); /** - * 是否开启登出统一通知 + * 是否开启统一登出 + * 1. 登出时把同一个用户名下的所有token都注销 + * 2. 登出信息通知所有单点登录系统 */ - private Boolean logoutNotify = false; + private Boolean unifiedLogout = false; } diff --git a/zlt-uaa/src/main/java/com/central/oauth/config/TokenGranterConfig.java b/zlt-uaa/src/main/java/com/central/oauth/config/TokenGranterConfig.java index 514d70e7..f34405ad 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/config/TokenGranterConfig.java +++ b/zlt-uaa/src/main/java/com/central/oauth/config/TokenGranterConfig.java @@ -9,6 +9,7 @@ import com.central.oauth.service.impl.UserDetailServiceFactory; import com.central.oauth.service.impl.UserDetailsByNameServiceFactoryWrapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationManager; @@ -80,7 +81,8 @@ public class TokenGranterConfig { * 授权模式 */ @Bean - public TokenGranter tokenGranter() { + @ConditionalOnMissingBean + public TokenGranter tokenGranter(DefaultTokenServices tokenServices) { if (tokenGranter == null) { tokenGranter = new TokenGranter() { private CompositeTokenGranter delegate; @@ -88,7 +90,7 @@ public class TokenGranterConfig { @Override public OAuth2AccessToken grant(String grantType, TokenRequest tokenRequest) { if (delegate == null) { - delegate = new CompositeTokenGranter(getAllTokenGranters()); + delegate = new CompositeTokenGranter(getAllTokenGranters(tokenServices)); } return delegate.grant(grantType, tokenRequest); } @@ -100,8 +102,7 @@ public class TokenGranterConfig { /** * 所有授权模式:默认的5种模式 + 自定义的模式 */ - private List getAllTokenGranters() { - AuthorizationServerTokenServices tokenServices = tokenServices(); + protected List getAllTokenGranters(DefaultTokenServices tokenServices) { AuthorizationCodeServices authorizationCodeServices = authorizationCodeServices(); OAuth2RequestFactory requestFactory = requestFactory(); //获取默认的授权模式 @@ -157,7 +158,9 @@ public class TokenGranterConfig { return new DefaultOAuth2RequestFactory(clientDetailsService); } - private DefaultTokenServices createDefaultTokenServices() { + @Bean + @ConditionalOnMissingBean + protected DefaultTokenServices createDefaultTokenServices() { DefaultTokenServices tokenServices = new CustomTokenServices(isSingleLogin); tokenServices.setTokenStore(tokenStore); tokenServices.setSupportRefreshToken(true); diff --git a/zlt-uaa/src/main/java/com/central/oauth/handler/OauthLogoutHandler.java b/zlt-uaa/src/main/java/com/central/oauth/handler/OauthLogoutHandler.java index 5c99d747..b3a3d49f 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/handler/OauthLogoutHandler.java +++ b/zlt-uaa/src/main/java/com/central/oauth/handler/OauthLogoutHandler.java @@ -41,7 +41,7 @@ public class OauthLogoutHandler implements LogoutHandler { token = AuthUtils.extractToken(request); } if(StrUtil.isNotEmpty(token)){ - if (securityProperties.getAuth().getLogoutNotify()) { + if (securityProperties.getAuth().getUnifiedLogout()) { OAuth2Authentication oAuth2Authentication = tokenStore.readAuthentication(token); UsernameHolder.setContext(oAuth2Authentication.getName()); } diff --git a/zlt-uaa/src/main/java/com/central/oauth/handler/OauthLogoutSuccessHandler.java b/zlt-uaa/src/main/java/com/central/oauth/handler/OauthLogoutSuccessHandler.java index 61e7f8b7..78dae8e0 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/handler/OauthLogoutSuccessHandler.java +++ b/zlt-uaa/src/main/java/com/central/oauth/handler/OauthLogoutSuccessHandler.java @@ -3,7 +3,7 @@ package com.central.oauth.handler; import cn.hutool.core.util.StrUtil; import com.central.common.model.Result; import com.central.common.utils.JsonUtil; -import com.central.oauth.service.impl.LogoutNotifyService; +import com.central.oauth.service.impl.UnifiedLogoutService; import com.central.oauth2.common.properties.SecurityProperties; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; @@ -31,15 +31,15 @@ public class OauthLogoutSuccessHandler implements LogoutSuccessHandler { private final RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); @Resource - private LogoutNotifyService logoutNotifyService; + private UnifiedLogoutService unifiedLogoutService; @Resource private SecurityProperties securityProperties; @Override public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException { - if (securityProperties.getAuth().getLogoutNotify()) { - logoutNotifyService.notification(); + if (securityProperties.getAuth().getUnifiedLogout()) { + unifiedLogoutService.allLogout(); } String redirectUri = request.getParameter("redirect_uri"); diff --git a/zlt-uaa/src/main/java/com/central/oauth/service/impl/LogoutNotifyService.java b/zlt-uaa/src/main/java/com/central/oauth/service/impl/UnifiedLogoutService.java similarity index 85% rename from zlt-uaa/src/main/java/com/central/oauth/service/impl/LogoutNotifyService.java rename to zlt-uaa/src/main/java/com/central/oauth/service/impl/UnifiedLogoutService.java index 5e822047..8fe2bf36 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/service/impl/LogoutNotifyService.java +++ b/zlt-uaa/src/main/java/com/central/oauth/service/impl/UnifiedLogoutService.java @@ -4,9 +4,11 @@ import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import com.central.oauth.utils.UsernameHolder; import lombok.extern.slf4j.Slf4j; +import org.springframework.context.annotation.Lazy; import org.springframework.core.task.TaskExecutor; import org.springframework.security.oauth2.common.OAuth2AccessToken; import org.springframework.security.oauth2.provider.ClientDetails; +import org.springframework.security.oauth2.provider.token.DefaultTokenServices; import org.springframework.security.oauth2.provider.token.TokenStore; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @@ -15,7 +17,7 @@ import javax.annotation.Resource; import java.util.*; /** - * 登出通知服务 + * 统一登出服务 * * @author zlt * @version 1.0 @@ -26,7 +28,7 @@ import java.util.*; */ @Slf4j @Service -public class LogoutNotifyService { +public class UnifiedLogoutService { private final static String LOGOUT_NOTIFY_URL_KEY = "LOGOUT_NOTIFY_URL_LIST"; @Resource @@ -41,7 +43,11 @@ public class LogoutNotifyService { @Resource private TokenStore tokenStore; - public void notification() { + @Lazy + @Resource + private DefaultTokenServices tokenServices; + + public void allLogout() { Set urls = this.getLogoutNotifyUrl(); for (String url : urls) { taskExecutor.execute(() -> { @@ -68,8 +74,10 @@ public class LogoutNotifyService { String urls = (String)informationMap.get(LOGOUT_NOTIFY_URL_KEY); if (StrUtil.isNotEmpty(urls)) { Collection tokens = tokenStore.findTokensByClientIdAndUserName(client.getClientId(), username); - String tokenStr = getTokenValueStr(tokens); - if (StrUtil.isNotEmpty(tokenStr)) { + if (CollUtil.isNotEmpty(tokens)) { + //注销所有该用户名下的token + tokens.forEach(t -> tokenServices.revokeToken(t.getValue())); + String tokenStr = getTokenValueStr(tokens); logoutNotifyUrls.addAll(generateNotifyUrls(urls.split(","), tokenStr)); } } diff --git a/zlt-uaa/src/main/resources/application.yml b/zlt-uaa/src/main/resources/application.yml index 7b9e15e3..d2cdfaf1 100644 --- a/zlt-uaa/src/main/resources/application.yml +++ b/zlt-uaa/src/main/resources/application.yml @@ -29,7 +29,7 @@ zlt: # 忽略验证码的应用编号 ignoreClientCode: app auth: - logoutNotify: true + unifiedLogout: true #多租户配置 tenant: enable: true