单点登出优化

This commit is contained in:
zlt2000
2021-11-22 22:19:32 +08:00
parent 84121c77f8
commit ce1d48172f
6 changed files with 31 additions and 18 deletions
@@ -30,7 +30,9 @@ public class AuthProperties {
private UrlPermissionProperties urlPermission = new UrlPermissionProperties();
/**
* 是否开启登出统一通知
* 是否开启统一登出
* 1. 登出时把同一个用户名下的所有token都注销
* 2. 登出信息通知所有单点登录系统
*/
private Boolean logoutNotify = false;
private Boolean unifiedLogout = false;
}
@@ -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<TokenGranter> getAllTokenGranters() {
AuthorizationServerTokenServices tokenServices = tokenServices();
protected List<TokenGranter> 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);
@@ -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());
}
@@ -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");
@@ -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<String> 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<OAuth2AccessToken> 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));
}
}
+1 -1
View File
@@ -29,7 +29,7 @@ zlt:
# 忽略验证码的应用编号
ignoreClientCode: app
auth:
logoutNotify: true
unifiedLogout: true
#多租户配置
tenant:
enable: true