单点登出优化
This commit is contained in:
+4
-2
@@ -30,7 +30,9 @@ public class AuthProperties {
|
|||||||
private UrlPermissionProperties urlPermission = new UrlPermissionProperties();
|
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 com.central.oauth.service.impl.UserDetailsByNameServiceFactoryWrapper;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
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.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
@@ -80,7 +81,8 @@ public class TokenGranterConfig {
|
|||||||
* 授权模式
|
* 授权模式
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public TokenGranter tokenGranter() {
|
@ConditionalOnMissingBean
|
||||||
|
public TokenGranter tokenGranter(DefaultTokenServices tokenServices) {
|
||||||
if (tokenGranter == null) {
|
if (tokenGranter == null) {
|
||||||
tokenGranter = new TokenGranter() {
|
tokenGranter = new TokenGranter() {
|
||||||
private CompositeTokenGranter delegate;
|
private CompositeTokenGranter delegate;
|
||||||
@@ -88,7 +90,7 @@ public class TokenGranterConfig {
|
|||||||
@Override
|
@Override
|
||||||
public OAuth2AccessToken grant(String grantType, TokenRequest tokenRequest) {
|
public OAuth2AccessToken grant(String grantType, TokenRequest tokenRequest) {
|
||||||
if (delegate == null) {
|
if (delegate == null) {
|
||||||
delegate = new CompositeTokenGranter(getAllTokenGranters());
|
delegate = new CompositeTokenGranter(getAllTokenGranters(tokenServices));
|
||||||
}
|
}
|
||||||
return delegate.grant(grantType, tokenRequest);
|
return delegate.grant(grantType, tokenRequest);
|
||||||
}
|
}
|
||||||
@@ -100,8 +102,7 @@ public class TokenGranterConfig {
|
|||||||
/**
|
/**
|
||||||
* 所有授权模式:默认的5种模式 + 自定义的模式
|
* 所有授权模式:默认的5种模式 + 自定义的模式
|
||||||
*/
|
*/
|
||||||
private List<TokenGranter> getAllTokenGranters() {
|
protected List<TokenGranter> getAllTokenGranters(DefaultTokenServices tokenServices) {
|
||||||
AuthorizationServerTokenServices tokenServices = tokenServices();
|
|
||||||
AuthorizationCodeServices authorizationCodeServices = authorizationCodeServices();
|
AuthorizationCodeServices authorizationCodeServices = authorizationCodeServices();
|
||||||
OAuth2RequestFactory requestFactory = requestFactory();
|
OAuth2RequestFactory requestFactory = requestFactory();
|
||||||
//获取默认的授权模式
|
//获取默认的授权模式
|
||||||
@@ -157,7 +158,9 @@ public class TokenGranterConfig {
|
|||||||
return new DefaultOAuth2RequestFactory(clientDetailsService);
|
return new DefaultOAuth2RequestFactory(clientDetailsService);
|
||||||
}
|
}
|
||||||
|
|
||||||
private DefaultTokenServices createDefaultTokenServices() {
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
protected DefaultTokenServices createDefaultTokenServices() {
|
||||||
DefaultTokenServices tokenServices = new CustomTokenServices(isSingleLogin);
|
DefaultTokenServices tokenServices = new CustomTokenServices(isSingleLogin);
|
||||||
tokenServices.setTokenStore(tokenStore);
|
tokenServices.setTokenStore(tokenStore);
|
||||||
tokenServices.setSupportRefreshToken(true);
|
tokenServices.setSupportRefreshToken(true);
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ public class OauthLogoutHandler implements LogoutHandler {
|
|||||||
token = AuthUtils.extractToken(request);
|
token = AuthUtils.extractToken(request);
|
||||||
}
|
}
|
||||||
if(StrUtil.isNotEmpty(token)){
|
if(StrUtil.isNotEmpty(token)){
|
||||||
if (securityProperties.getAuth().getLogoutNotify()) {
|
if (securityProperties.getAuth().getUnifiedLogout()) {
|
||||||
OAuth2Authentication oAuth2Authentication = tokenStore.readAuthentication(token);
|
OAuth2Authentication oAuth2Authentication = tokenStore.readAuthentication(token);
|
||||||
UsernameHolder.setContext(oAuth2Authentication.getName());
|
UsernameHolder.setContext(oAuth2Authentication.getName());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package com.central.oauth.handler;
|
|||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.central.common.model.Result;
|
import com.central.common.model.Result;
|
||||||
import com.central.common.utils.JsonUtil;
|
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 com.central.oauth2.common.properties.SecurityProperties;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
@@ -31,15 +31,15 @@ public class OauthLogoutSuccessHandler implements LogoutSuccessHandler {
|
|||||||
private final RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
|
private final RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private LogoutNotifyService logoutNotifyService;
|
private UnifiedLogoutService unifiedLogoutService;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private SecurityProperties securityProperties;
|
private SecurityProperties securityProperties;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
|
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
|
||||||
if (securityProperties.getAuth().getLogoutNotify()) {
|
if (securityProperties.getAuth().getUnifiedLogout()) {
|
||||||
logoutNotifyService.notification();
|
unifiedLogoutService.allLogout();
|
||||||
}
|
}
|
||||||
|
|
||||||
String redirectUri = request.getParameter("redirect_uri");
|
String redirectUri = request.getParameter("redirect_uri");
|
||||||
|
|||||||
+12
-4
@@ -4,9 +4,11 @@ import cn.hutool.core.collection.CollUtil;
|
|||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.central.oauth.utils.UsernameHolder;
|
import com.central.oauth.utils.UsernameHolder;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.context.annotation.Lazy;
|
||||||
import org.springframework.core.task.TaskExecutor;
|
import org.springframework.core.task.TaskExecutor;
|
||||||
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
||||||
import org.springframework.security.oauth2.provider.ClientDetails;
|
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.security.oauth2.provider.token.TokenStore;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
@@ -15,7 +17,7 @@ import javax.annotation.Resource;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 登出通知服务
|
* 统一登出服务
|
||||||
*
|
*
|
||||||
* @author zlt
|
* @author zlt
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
@@ -26,7 +28,7 @@ import java.util.*;
|
|||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class LogoutNotifyService {
|
public class UnifiedLogoutService {
|
||||||
private final static String LOGOUT_NOTIFY_URL_KEY = "LOGOUT_NOTIFY_URL_LIST";
|
private final static String LOGOUT_NOTIFY_URL_KEY = "LOGOUT_NOTIFY_URL_LIST";
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
@@ -41,7 +43,11 @@ public class LogoutNotifyService {
|
|||||||
@Resource
|
@Resource
|
||||||
private TokenStore tokenStore;
|
private TokenStore tokenStore;
|
||||||
|
|
||||||
public void notification() {
|
@Lazy
|
||||||
|
@Resource
|
||||||
|
private DefaultTokenServices tokenServices;
|
||||||
|
|
||||||
|
public void allLogout() {
|
||||||
Set<String> urls = this.getLogoutNotifyUrl();
|
Set<String> urls = this.getLogoutNotifyUrl();
|
||||||
for (String url : urls) {
|
for (String url : urls) {
|
||||||
taskExecutor.execute(() -> {
|
taskExecutor.execute(() -> {
|
||||||
@@ -68,8 +74,10 @@ public class LogoutNotifyService {
|
|||||||
String urls = (String)informationMap.get(LOGOUT_NOTIFY_URL_KEY);
|
String urls = (String)informationMap.get(LOGOUT_NOTIFY_URL_KEY);
|
||||||
if (StrUtil.isNotEmpty(urls)) {
|
if (StrUtil.isNotEmpty(urls)) {
|
||||||
Collection<OAuth2AccessToken> tokens = tokenStore.findTokensByClientIdAndUserName(client.getClientId(), username);
|
Collection<OAuth2AccessToken> tokens = tokenStore.findTokensByClientIdAndUserName(client.getClientId(), username);
|
||||||
|
if (CollUtil.isNotEmpty(tokens)) {
|
||||||
|
//注销所有该用户名下的token
|
||||||
|
tokens.forEach(t -> tokenServices.revokeToken(t.getValue()));
|
||||||
String tokenStr = getTokenValueStr(tokens);
|
String tokenStr = getTokenValueStr(tokens);
|
||||||
if (StrUtil.isNotEmpty(tokenStr)) {
|
|
||||||
logoutNotifyUrls.addAll(generateNotifyUrls(urls.split(","), tokenStr));
|
logoutNotifyUrls.addAll(generateNotifyUrls(urls.split(","), tokenStr));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -29,7 +29,7 @@ zlt:
|
|||||||
# 忽略验证码的应用编号
|
# 忽略验证码的应用编号
|
||||||
ignoreClientCode: app
|
ignoreClientCode: app
|
||||||
auth:
|
auth:
|
||||||
logoutNotify: true
|
unifiedLogout: true
|
||||||
#多租户配置
|
#多租户配置
|
||||||
tenant:
|
tenant:
|
||||||
enable: true
|
enable: true
|
||||||
|
|||||||
Reference in New Issue
Block a user