增加统一登出通知功能
This commit is contained in:
+5
@@ -28,4 +28,9 @@ public class AuthProperties {
|
|||||||
* url权限配置
|
* url权限配置
|
||||||
*/
|
*/
|
||||||
private UrlPermissionProperties urlPermission = new UrlPermissionProperties();
|
private UrlPermissionProperties urlPermission = new UrlPermissionProperties();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否开启登出统一通知
|
||||||
|
*/
|
||||||
|
private Boolean logoutNotify = false;
|
||||||
}
|
}
|
||||||
|
|||||||
-1
@@ -31,7 +31,6 @@ public class RestTemplateAutoConfigure {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private RestTemplateProperties restTemplateProperties;
|
private RestTemplateProperties restTemplateProperties;
|
||||||
|
|
||||||
@LoadBalanced
|
|
||||||
@Bean
|
@Bean
|
||||||
public RestTemplate restTemplate(ClientHttpRequestFactory httpRequestFactory) {
|
public RestTemplate restTemplate(ClientHttpRequestFactory httpRequestFactory) {
|
||||||
RestTemplate restTemplate = new RestTemplate();
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import org.springframework.security.web.authentication.AuthenticationSuccessHand
|
|||||||
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
|
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
|
||||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
||||||
import org.springframework.security.web.authentication.logout.LogoutHandler;
|
import org.springframework.security.web.authentication.logout.LogoutHandler;
|
||||||
|
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
@@ -58,7 +59,10 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
private PasswordEncoder passwordEncoder;
|
private PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private LogoutHandler oauthLogoutHandler;
|
private LogoutHandler logoutHandler;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private LogoutSuccessHandler logoutSuccessHandler;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private OpenIdAuthenticationSecurityConfig openIdAuthenticationSecurityConfig;
|
private OpenIdAuthenticationSecurityConfig openIdAuthenticationSecurityConfig;
|
||||||
@@ -108,8 +112,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
.and()
|
.and()
|
||||||
.logout()
|
.logout()
|
||||||
.logoutUrl(SecurityConstants.LOGOUT_URL)
|
.logoutUrl(SecurityConstants.LOGOUT_URL)
|
||||||
.logoutSuccessHandler(new OauthLogoutSuccessHandler())
|
.logoutSuccessHandler(logoutSuccessHandler)
|
||||||
.addLogoutHandler(oauthLogoutHandler)
|
.addLogoutHandler(logoutHandler)
|
||||||
.clearAuthentication(true)
|
.clearAuthentication(true)
|
||||||
.and()
|
.and()
|
||||||
.apply(openIdAuthenticationSecurityConfig)
|
.apply(openIdAuthenticationSecurityConfig)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.central.oauth.config;
|
|||||||
|
|
||||||
import com.central.oauth.exception.ValidateCodeException;
|
import com.central.oauth.exception.ValidateCodeException;
|
||||||
import com.central.oauth.handler.OauthLogoutHandler;
|
import com.central.oauth.handler.OauthLogoutHandler;
|
||||||
|
import com.central.oauth.handler.OauthLogoutSuccessHandler;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
@@ -13,6 +14,8 @@ import org.springframework.security.oauth2.provider.error.DefaultWebResponseExce
|
|||||||
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
|
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
|
||||||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
||||||
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
|
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
|
||||||
|
import org.springframework.security.web.authentication.logout.LogoutHandler;
|
||||||
|
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
@@ -28,10 +31,15 @@ import java.io.IOException;
|
|||||||
@Configuration
|
@Configuration
|
||||||
public class SecurityHandlerConfig {
|
public class SecurityHandlerConfig {
|
||||||
@Bean
|
@Bean
|
||||||
public OauthLogoutHandler oauthLogoutHandler() {
|
public LogoutHandler logoutHandler() {
|
||||||
return new OauthLogoutHandler();
|
return new OauthLogoutHandler();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public LogoutSuccessHandler logoutSuccessHandler() {
|
||||||
|
return new OauthLogoutSuccessHandler();
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public WebResponseExceptionTranslator webResponseExceptionTranslator() {
|
public WebResponseExceptionTranslator webResponseExceptionTranslator() {
|
||||||
return new DefaultWebResponseExceptionTranslator() {
|
return new DefaultWebResponseExceptionTranslator() {
|
||||||
|
|||||||
@@ -1,28 +1,38 @@
|
|||||||
package com.central.oauth.handler;
|
package com.central.oauth.handler;
|
||||||
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.central.oauth.utils.UsernameHolder;
|
||||||
|
import com.central.oauth2.common.properties.SecurityProperties;
|
||||||
import com.central.oauth2.common.util.AuthUtils;
|
import com.central.oauth2.common.util.AuthUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
||||||
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
|
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
|
||||||
|
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||||
import org.springframework.security.oauth2.provider.token.TokenStore;
|
import org.springframework.security.oauth2.provider.token.TokenStore;
|
||||||
import org.springframework.security.web.authentication.logout.LogoutHandler;
|
import org.springframework.security.web.authentication.logout.LogoutHandler;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zlt
|
* @author zlt
|
||||||
* @date 2018/10/17
|
* @date 2018/10/17
|
||||||
|
* <p>
|
||||||
|
* Blog: https://zlt2000.gitee.io
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class OauthLogoutHandler implements LogoutHandler {
|
public class OauthLogoutHandler implements LogoutHandler {
|
||||||
@Autowired
|
@Resource
|
||||||
private TokenStore tokenStore;
|
private TokenStore tokenStore;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private SecurityProperties securityProperties;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
|
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
|
||||||
Assert.notNull(tokenStore, "tokenStore must be set");
|
Assert.notNull(tokenStore, "tokenStore must be set");
|
||||||
@@ -31,6 +41,11 @@ 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()) {
|
||||||
|
OAuth2Authentication oAuth2Authentication = tokenStore.readAuthentication(token);
|
||||||
|
UsernameHolder.setContext(oAuth2Authentication.getName());
|
||||||
|
}
|
||||||
|
|
||||||
OAuth2AccessToken existingAccessToken = tokenStore.readAccessToken(token);
|
OAuth2AccessToken existingAccessToken = tokenStore.readAccessToken(token);
|
||||||
OAuth2RefreshToken refreshToken;
|
OAuth2RefreshToken refreshToken;
|
||||||
if (existingAccessToken != null) {
|
if (existingAccessToken != null) {
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ 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.oauth2.common.properties.SecurityProperties;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
@@ -11,6 +13,7 @@ import org.springframework.security.web.DefaultRedirectStrategy;
|
|||||||
import org.springframework.security.web.RedirectStrategy;
|
import org.springframework.security.web.RedirectStrategy;
|
||||||
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
|
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -25,10 +28,20 @@ import java.io.PrintWriter;
|
|||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class OauthLogoutSuccessHandler implements LogoutSuccessHandler {
|
public class OauthLogoutSuccessHandler implements LogoutSuccessHandler {
|
||||||
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
|
private final RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private LogoutNotifyService logoutNotifyService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
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()) {
|
||||||
|
logoutNotifyService.notification();
|
||||||
|
}
|
||||||
|
|
||||||
String redirectUri = request.getParameter("redirect_uri");
|
String redirectUri = request.getParameter("redirect_uri");
|
||||||
if (StrUtil.isNotEmpty(redirectUri)) {
|
if (StrUtil.isNotEmpty(redirectUri)) {
|
||||||
//重定向指定的地址
|
//重定向指定的地址
|
||||||
|
|||||||
@@ -0,0 +1,104 @@
|
|||||||
|
package com.central.oauth.service.impl;
|
||||||
|
|
||||||
|
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.core.task.TaskExecutor;
|
||||||
|
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
||||||
|
import org.springframework.security.oauth2.provider.ClientDetails;
|
||||||
|
import org.springframework.security.oauth2.provider.token.TokenStore;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登出通知服务
|
||||||
|
*
|
||||||
|
* @author zlt
|
||||||
|
* @version 1.0
|
||||||
|
* @date 2021/10/21
|
||||||
|
* <p>
|
||||||
|
* Blog: https://zlt2000.gitee.io
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
public class LogoutNotifyService {
|
||||||
|
private final static String LOGOUT_NOTIFY_URL_KEY = "LOGOUT_NOTIFY_URL_LIST";
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private TaskExecutor taskExecutor;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private RedisClientDetailsService redisClientDetailsService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private TokenStore tokenStore;
|
||||||
|
|
||||||
|
public void notification() {
|
||||||
|
Set<String> urls = this.getLogoutNotifyUrl();
|
||||||
|
for (String url : urls) {
|
||||||
|
taskExecutor.execute(() -> {
|
||||||
|
try {
|
||||||
|
restTemplate.getForObject(url, Void.class);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn(e.getMessage());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取登出需要通知的地址集合
|
||||||
|
*/
|
||||||
|
private Set<String> getLogoutNotifyUrl() {
|
||||||
|
String username = UsernameHolder.getContext();
|
||||||
|
Set<String> logoutNotifyUrls = new HashSet<>();
|
||||||
|
try {
|
||||||
|
List<ClientDetails> clientDetails = redisClientDetailsService.listClientDetails();
|
||||||
|
Map<String, Object> informationMap;
|
||||||
|
for (ClientDetails client : clientDetails) {
|
||||||
|
informationMap = client.getAdditionalInformation();
|
||||||
|
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)) {
|
||||||
|
logoutNotifyUrls.addAll(generateNotifyUrls(urls.split(","), tokenStr));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
UsernameHolder.clearContext();
|
||||||
|
}
|
||||||
|
return logoutNotifyUrls;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getTokenValueStr(Collection<OAuth2AccessToken> tokens) {
|
||||||
|
if (CollUtil.isNotEmpty(tokens)) {
|
||||||
|
return StrUtil.join( ",", tokens);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Set<String> generateNotifyUrls(String[] urls, String tokenStr) {
|
||||||
|
Set<String> urlSet = new HashSet(urls.length);
|
||||||
|
for (String url : urls) {
|
||||||
|
StringBuilder urlBuilder = new StringBuilder(url);
|
||||||
|
if (url.contains("?")) {
|
||||||
|
urlBuilder.append("&");
|
||||||
|
} else {
|
||||||
|
urlBuilder.append("?");
|
||||||
|
}
|
||||||
|
urlBuilder.append("tokens=").append(tokenStr);
|
||||||
|
urlSet.add(urlBuilder.toString());
|
||||||
|
}
|
||||||
|
return urlSet;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package com.central.oauth.utils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author zlt
|
||||||
|
* @version 1.0
|
||||||
|
* @date 2021/10/22
|
||||||
|
* <p>
|
||||||
|
* Blog: https://zlt2000.gitee.io
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
public class UsernameHolder {
|
||||||
|
private static final ThreadLocal<String> contextHolder = new ThreadLocal();
|
||||||
|
|
||||||
|
public static String getContext() {
|
||||||
|
return contextHolder.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setContext(String username) {
|
||||||
|
contextHolder.set(username);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void clearContext() {
|
||||||
|
contextHolder.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,6 +28,8 @@ zlt:
|
|||||||
code:
|
code:
|
||||||
# 忽略验证码的应用编号
|
# 忽略验证码的应用编号
|
||||||
ignoreClientCode: app
|
ignoreClientCode: app
|
||||||
|
auth:
|
||||||
|
logoutNotify: true
|
||||||
#多租户配置
|
#多租户配置
|
||||||
tenant:
|
tenant:
|
||||||
enable: true
|
enable: true
|
||||||
|
|||||||
Reference in New Issue
Block a user