diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java index 135e253d3..f0efab4e2 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/config/WebSecurityConfigurer.java @@ -22,6 +22,7 @@ package com.pig4cloud.pigx.auth.config; import com.pig4cloud.pigx.common.security.handler.FormAuthenticationFailureHandler; import com.pig4cloud.pigx.common.security.handler.MobileLoginSuccessHandler; import com.pig4cloud.pigx.common.security.handler.SsoLogoutSuccessHandler; +import com.pig4cloud.pigx.common.security.handler.TenantSavedRequestAwareAuthenticationSuccessHandler; import com.pig4cloud.pigx.common.security.mobile.MobileSecurityConfigurer; import lombok.SneakyThrows; import org.springframework.context.annotation.Bean; @@ -51,6 +52,7 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { @SneakyThrows protected void configure(HttpSecurity http) { http.formLogin().loginPage("/token/login").loginProcessingUrl("/token/form") + .successHandler(tenantSavedRequestAwareAuthenticationSuccessHandler()) .failureHandler(authenticationFailureHandler()).and().logout() .logoutSuccessHandler(logoutSuccessHandler()).deleteCookies("JSESSIONID").invalidateHttpSession(true) .and().authorizeRequests().antMatchers("/token/**", "/actuator/**", "/mobile/**").permitAll() @@ -88,6 +90,15 @@ public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { return new MobileLoginSuccessHandler(); } + /** + * 具备租户传递能力 + * @return AuthenticationSuccessHandler + */ + @Bean + public AuthenticationSuccessHandler tenantSavedRequestAwareAuthenticationSuccessHandler() { + return new TenantSavedRequestAwareAuthenticationSuccessHandler(); + } + @Bean public MobileSecurityConfigurer mobileSecurityConfigurer() { return new MobileSecurityConfigurer(); diff --git a/pigx-common/pigx-common-gray/src/main/java/com/pig4cloud/pigx/common/gray/rule/GrayRoundRobinLoadBalancer.java b/pigx-common/pigx-common-gray/src/main/java/com/pig4cloud/pigx/common/gray/rule/GrayRoundRobinLoadBalancer.java index da3824af3..e0aa9fdb5 100755 --- a/pigx-common/pigx-common-gray/src/main/java/com/pig4cloud/pigx/common/gray/rule/GrayRoundRobinLoadBalancer.java +++ b/pigx-common/pigx-common-gray/src/main/java/com/pig4cloud/pigx/common/gray/rule/GrayRoundRobinLoadBalancer.java @@ -65,7 +65,7 @@ public class GrayRoundRobinLoadBalancer extends RoundRobinLoadBalancer { return new EmptyResponse(); } - if (request == null || request.getContext() == null){ + if (request == null || request.getContext() == null) { return super.choose(request).block(); } diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/SsoLogoutSuccessHandler.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/SsoLogoutSuccessHandler.java index 53f5602d7..139c76d0e 100644 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/SsoLogoutSuccessHandler.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/SsoLogoutSuccessHandler.java @@ -1,14 +1,15 @@ package com.pig4cloud.pigx.common.security.handler; +import java.io.IOException; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + import cn.hutool.core.util.StrUtil; import org.springframework.http.HttpHeaders; import org.springframework.security.core.Authentication; import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; - /** * @author lengleng * @date 2020/10/6 @@ -25,12 +26,13 @@ public class SsoLogoutSuccessHandler implements LogoutSuccessHandler { // 获取请求参数中是否包含 回调地址 String redirectUrl = request.getParameter(REDIRECT_URL); + String referer = request.getHeader(HttpHeaders.REFERER); + if (StrUtil.isNotBlank(redirectUrl)) { response.sendRedirect(redirectUrl); } - else { + else if (StrUtil.isNotBlank(referer)) { // 默认跳转referer 地址 - String referer = request.getHeader(HttpHeaders.REFERER); response.sendRedirect(referer); } } diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/TenantSavedRequestAwareAuthenticationSuccessHandler.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/TenantSavedRequestAwareAuthenticationSuccessHandler.java new file mode 100644 index 000000000..098bff18d --- /dev/null +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/TenantSavedRequestAwareAuthenticationSuccessHandler.java @@ -0,0 +1,63 @@ +package com.pig4cloud.pigx.common.security.handler; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import cn.hutool.core.util.StrUtil; +import com.pig4cloud.pigx.common.core.util.KeyStrResolver; +import com.pig4cloud.pigx.common.core.util.SpringContextHolder; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.security.core.Authentication; +import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; +import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; +import org.springframework.security.web.savedrequest.HttpSessionRequestCache; +import org.springframework.security.web.savedrequest.RequestCache; +import org.springframework.security.web.savedrequest.SavedRequest; + +/** + * @author lengleng + * @date 2021/2/7 + * + * 增强成功回调增加租户上下文避免极端情况下丢失问题 + * @see SavedRequestAwareAuthenticationSuccessHandler + */ +@Slf4j +public class TenantSavedRequestAwareAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler { + + protected final Log logger = LogFactory.getLog(this.getClass()); + + private RequestCache requestCache = new HttpSessionRequestCache(); + + public TenantSavedRequestAwareAuthenticationSuccessHandler() { + } + + @Override + public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, + Authentication authentication) throws ServletException, IOException { + SavedRequest savedRequest = this.requestCache.getRequest(request, response); + if (savedRequest == null) { + super.onAuthenticationSuccess(request, response, authentication); + } + + String targetUrlParameter = this.getTargetUrlParameter(); + if (isAlwaysUseDefaultTargetUrl() || StrUtil.isBlank(request.getParameter(targetUrlParameter))) { + this.requestCache.removeRequest(request, response); + super.onAuthenticationSuccess(request, response, authentication); + } + else { + this.clearAuthenticationAttributes(request); + // 增加租户信息 + assert savedRequest != null; + String targetUrl = String.format(savedRequest.getRedirectUrl() + "&TENANT-ID=%s", + SpringContextHolder.getBean(KeyStrResolver.class).key()); + + this.getRedirectStrategy().sendRedirect(request, response, targetUrl); + } + } + +}