From f6691ace5771bdc0fd63d2cd55127fed0725913a Mon Sep 17 00:00:00 2001 From: zlt2000 Date: Sat, 29 Jan 2022 15:38:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E6=8E=88=E6=9D=83=E4=B8=AD?= =?UTF-8?q?=E5=BF=83UAA=E5=8F=AF=E9=85=8D=E7=BD=AE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../oauth2/common/properties/AuthProperties.java | 12 ++++++++++++ .../com/central/oauth/config/TokenGranterConfig.java | 11 ++++------- .../oauth/service/impl/CustomTokenServices.java | 12 ++++++------ zlt-uaa/src/main/resources/application.yml | 8 ++++---- 4 files changed, 26 insertions(+), 17 deletions(-) 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 c3f77106..d3323033 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 @@ -35,4 +35,16 @@ public class AuthProperties { * 2. 登出信息通知所有单点登录系统 */ private Boolean unifiedLogout = false; + + /** + * 是否同应用同账号登录互踢 + */ + private Boolean isSingleLogin = false; + + /** + * 是否同应用同账号登录时共用token + * true: 多个用户使用同一账号登录时共用一个token + * false: 就算使用同一账号登录时都会新建一个token + */ + private Boolean isShareToken = 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 f34405ad..579c4955 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 @@ -7,8 +7,8 @@ import com.central.oauth.service.IValidateCodeService; import com.central.oauth.service.impl.CustomTokenServices; import com.central.oauth.service.impl.UserDetailServiceFactory; import com.central.oauth.service.impl.UserDetailsByNameServiceFactoryWrapper; +import com.central.oauth2.common.properties.SecurityProperties; 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; @@ -71,11 +71,8 @@ public class TokenGranterConfig { private TokenGranter tokenGranter; - /** - * 是否登录同应用同账号互踢 - */ - @Value("${zlt.uaa.isSingleLogin:false}") - private boolean isSingleLogin; + @Resource + private SecurityProperties securityProperties; /** * 授权模式 @@ -161,7 +158,7 @@ public class TokenGranterConfig { @Bean @ConditionalOnMissingBean protected DefaultTokenServices createDefaultTokenServices() { - DefaultTokenServices tokenServices = new CustomTokenServices(isSingleLogin); + DefaultTokenServices tokenServices = new CustomTokenServices(securityProperties.getAuth()); tokenServices.setTokenStore(tokenStore); tokenServices.setSupportRefreshToken(true); tokenServices.setReuseRefreshToken(reuseRefreshToken); diff --git a/zlt-uaa/src/main/java/com/central/oauth/service/impl/CustomTokenServices.java b/zlt-uaa/src/main/java/com/central/oauth/service/impl/CustomTokenServices.java index 66e1c624..6ce79c31 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/service/impl/CustomTokenServices.java +++ b/zlt-uaa/src/main/java/com/central/oauth/service/impl/CustomTokenServices.java @@ -1,6 +1,7 @@ package com.central.oauth.service.impl; import com.central.common.constant.SecurityConstants; +import com.central.oauth2.common.properties.AuthProperties; import org.springframework.security.authentication.AbstractAuthenticationToken; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.core.Authentication; @@ -41,10 +42,10 @@ public class CustomTokenServices extends DefaultTokenServices { /** * 是否登录同应用同账号互踢 */ - private boolean isSingleLogin; + private final AuthProperties authProperties; - public CustomTokenServices(boolean isSingleLogin) { - this.isSingleLogin = isSingleLogin; + public CustomTokenServices(AuthProperties auth) { + this.authProperties = auth; } @Override @@ -53,7 +54,7 @@ public class CustomTokenServices extends DefaultTokenServices { OAuth2AccessToken existingAccessToken = tokenStore.getAccessToken(authentication); OAuth2RefreshToken refreshToken = null; if (existingAccessToken != null) { - if (isSingleLogin) { + if (authProperties.getIsSingleLogin()) { if (existingAccessToken.getRefreshToken() != null) { tokenStore.removeRefreshToken(existingAccessToken.getRefreshToken()); } @@ -67,8 +68,7 @@ public class CustomTokenServices extends DefaultTokenServices { tokenStore.removeRefreshToken(refreshToken); } tokenStore.removeAccessToken(existingAccessToken); - } - else { + } else if (authProperties.getIsShareToken()) { // oidc每次授权都刷新id_token existingAccessToken = refreshIdToken(existingAccessToken, authentication); // Re-store the access token in case the authentication has changed diff --git a/zlt-uaa/src/main/resources/application.yml b/zlt-uaa/src/main/resources/application.yml index d2cdfaf1..0527d6b4 100644 --- a/zlt-uaa/src/main/resources/application.yml +++ b/zlt-uaa/src/main/resources/application.yml @@ -29,12 +29,12 @@ zlt: # 忽略验证码的应用编号 ignoreClientCode: app auth: + # 开启统一登出 unifiedLogout: true + # 同账号登录互踢 + isSingleLogin: true #多租户配置 tenant: enable: true ignoreTables: - - oauth_client_details - #是否登录同应用同账号互踢 - uaa: - isSingleLogin: false \ No newline at end of file + - oauth_client_details \ No newline at end of file