✨ Introducing new features.#2374 避免授权码模式下丢失租户上下文的问题
This commit is contained in:
1 parent
480d2f3801
commit
45d5458526
4 files changed
+83
-7
No files matched your search
@@ -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();
|
||||
|
||||
+1
-1
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
+8
-6
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+63
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user