解決使用gateway后靜態(tài)資源失效的問題
關于使用gateway后靜態(tài)資源失效問題
配置文件方式對應服務配置文件目錄提供參考

F12可以看到靜態(tài)資源路徑全部都是加載失敗。這是因為我們沒有對靜態(tài)文件進行路由導致。
配置文件方式
貼出主要配置:/static/**表示對靜態(tài)資源的路由
routes:
- id: home-service
uri: lb://home-service #lb表示從注冊中心找到服務
predicates: #路由規(guī)則
- Path=/home-service/**, /static/**
- id: user-service
uri: lb://user-service
predicates:
- Path=/user-service/**, /static/**
對應服務配置文件
spring:
resources:
static-locations: 靜態(tài)資源路徑
目錄提供參考

記錄一次SSO gateway=true 失效的問題
問題發(fā)生場景:
當用戶在門戶登錄后(門戶集成sso(4.0 版本)單點登錄,門戶使用自己的登錄界面,使用sso中的gateway=true特性實現),經過一段時間后,用戶刷新門戶,如果門戶會話狀態(tài)和全局sso失效,按照設想是會回到門戶登錄界面,而不是sso的登錄界面,但是有時卻回到sso的登錄界面,并且瀏覽器地址欄帶有“gateway=true”查詢參數。
用戶在刷新一次瀏覽器才能回到門戶登錄界面。
一開始想到是sso中的gateway=true參數失效,在login-webflow.xml文件中增加一個action-state用來再次檢查gateway參數,并在decision-state id 為“gatewayRequestCheck”中使用新增加的action-state 對gateway再一次檢查。
配置以及代碼如下:
xml配置:
login-webflow.xml
<decision-state id="gatewayRequestCheck"> <if test="requestParameters.gateway != '' and requestParameters.gateway != null and flowScope.service != null" then="gatewayServicesManagementCheck" else="gatewayParameterCheck" /> </decision-state> <!-- 增加再次對gateway參數檢查 --> <action-state id="gatewayParameterCheck"> <evaluate expression="gatewayParameterCheck"/> <transition on="yes" to="gatewayServicesManagementCheck" /> <transition on="no" to="serviceAuthorizationCheck" /> </action-state> <!-- 增加再次對gateway參數檢查 #-->
cas-servlet.xml
<!-- 檢查gateway參數 -->
<bean id="gatewayParameterCheck" class="com.wisdragon.cas.web.flow.GatewayParameterCheck"
c:servicesManager-ref="servicesManager" />
<!-- 檢查gateway參數# -->
Java代碼:
public class GatewayParameterCheck extends AbstractAction {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@NotNull
private final ServicesManager servicesManager;
/**
* Initialize the component with an instance of the services manager.
* @param servicesManager the service registry instance.
*/
public GatewayParameterCheck(final ServicesManager servicesManager) {
this.servicesManager = servicesManager;
}
@Override
protected Event doExecute(final RequestContext context) throws Exception {
final Service service = WebUtils.getService(context);
final HttpServletRequest request = WebUtils.getHttpServletRequest(context);
String gateWayV = request.getParameter("gateway");
if (StringUtils.hasText(gateWayV) && service != null) {
if ("true".equals(gateWayV)) {
logger.info("gateway參數校驗,校驗信息:gateway={}, 請求服務信息:{}", gateWayV, service.toString());
return yes();
}
}
return no();
}
}
更新到生產環(huán)境后,經用戶測試發(fā)現此方案無效。此方案無效后,只能再次去回歸login-webflow的登錄流程,總結的流程圖如下:

上圖只是涉及到登錄部分簡單截圖,不涉及到TGT以及ST。
由上圖可以發(fā)現,在“ticketGrantingTicketCheck”節(jié)點之后,如果TGT不存在,則會去檢查gateway參數,如果gateway存在,則會去到節(jié)點“gatewayServicesManagementCheck”,如果不存在,則回到節(jié)點“serviceAuthorizationCheck”,到該節(jié)點一般也就會到sso的登錄界面了。
之前在節(jié)點“gatewayRequestCheck”后增加了gateway參數再次檢查的節(jié)點“gatewayParameterCheck”發(fā)現無效果。
因此可以排除走這條線的可能性,剩下的只有TGT存在無效到sso登錄界面這條線了。在這個action-state之中只是對tgt相關的cookie進行清除,并沒有對gateway參數進行檢查,因此有可能是問題的所在。
驗證猜想。
1. 將TGT的存活時間暫時設置為60秒(默認為2小時),方便測試。
修改配置文件:ticketExpirationPolicies.xml
<!-- TicketGrantingTicketExpirationPolicy: Default as of 3.5 -->
<!-- Provides both idle and hard timeouts, for instance 2 hour sliding window with an 8 hour max lifetime
"-->
<bean id="grantingTicketExpirationPolicy" class="org.jasig.cas.ticket.support.TicketGrantingTicketExpirationPolicy"
p:maxTimeToLiveInSeconds="${tgt.maxTimeToLiveInSeconds:28800}"
p:timeToKillInSeconds="${tgt.timeToKillInSeconds:60}"/>
2. 重新發(fā)布sso程序,從門戶認證成功后,等待60秒過后,再次刷新門戶系統,發(fā)現果然到sso登錄界面。重復實現多次發(fā)現都時一樣。由此可以得出,由于門戶使用的TGT失效,并沒有檢查gateway參數因此跳轉到sso的登錄界面。
3. 解決方案:只需在action-state 節(jié)點中增加對gateway參數的檢查邏輯,根據檢查的結果到不同的節(jié)點即可。新的流程圖如下:

在上圖中增加節(jié)點 “terminateSession”的 條件為“gateway”的transition,當請求中存在gateway參數時,讓流程到gatewayRequestCheck節(jié)點。
代碼:
final HttpServletResponse response = WebUtils.getHttpServletResponse(context);
this.ticketGrantingTicketCookieGenerator.removeCookie(response);
this.warnCookieGenerator.removeCookie(response);
/**
* 檢查gateway參數,如果為true,走gatewayRequestCheck
*/
final String hasGateWayParameter = WebUtils.getHttpServletRequest(context).getParameter("gateway");
if (!VTools.StringIsNullOrSpace(hasGateWayParameter) && "true".equals(hasGateWayParameter)) {
return new Event(this, "gateway");
}
return this.eventFactorySupport.success(this);
本地重新編譯測試后發(fā)現可正常跳轉回門戶登錄界面。將TGT的存活時間恢復默認值,發(fā)布到線上后,留意一段時間,發(fā)現沒有出現改問題,至此解決問題。
總結:
sso login-webflow 登錄流程較為復雜,出現問題時,應該根據登錄流程圖分析判斷問題出現在哪些節(jié)點上,然后修改相關參數重現問題,之后修改相關邏輯驗證以及解決問題。
相關文章
SpringBoot多環(huán)境開發(fā)與日志小結
這篇文章主要介紹了SpringBoot多環(huán)境開發(fā)與日志,下面給大家說一下如何基于多環(huán)境開發(fā)做配置獨立管理,務必掌握,需要的朋友可以參考下2022-08-08
springboot+redis+阿里云短信實現手機號登錄功能
這篇文章主要介紹了springboot+redis+阿里云短信實現手機號登錄功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-01-01

