解決使用gateway后靜態(tài)資源失效的問題
關(guān)于使用gateway后靜態(tài)資源失效問題
配置文件方式對應(yīng)服務(wù)配置文件目錄提供參考
F12可以看到靜態(tài)資源路徑全部都是加載失敗。這是因為我們沒有對靜態(tài)文件進行路由導(dǎo)致。
配置文件方式
貼出主要配置:/static/**表示對靜態(tài)資源的路由
routes: - id: home-service uri: lb://home-service #lb表示從注冊中心找到服務(wù) predicates: #路由規(guī)則 - Path=/home-service/**, /static/** - id: user-service uri: lb://user-service predicates: - Path=/user-service/**, /static/**
對應(yīng)服務(wù)配置文件
spring: resources: static-locations: 靜態(tài)資源路徑
目錄提供參考
記錄一次SSO gateway=true 失效的問題
問題發(fā)生場景:
當用戶在門戶登錄后(門戶集成sso(4.0 版本)單點登錄,門戶使用自己的登錄界面,使用sso中的gateway=true特性實現(xiàn)),經(jīng)過一段時間后,用戶刷新門戶,如果門戶會話狀態(tài)和全局sso失效,按照設(shè)想是會回到門戶登錄界面,而不是sso的登錄界面,但是有時卻回到sso的登錄界面,并且瀏覽器地址欄帶有“gateway=true”查詢參數(shù)。
用戶在刷新一次瀏覽器才能回到門戶登錄界面。
一開始想到是sso中的gateway=true參數(shù)失效,在login-webflow.xml文件中增加一個action-state用來再次檢查gateway參數(shù),并在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參數(shù)檢查 --> <action-state id="gatewayParameterCheck"> <evaluate expression="gatewayParameterCheck"/> <transition on="yes" to="gatewayServicesManagementCheck" /> <transition on="no" to="serviceAuthorizationCheck" /> </action-state> <!-- 增加再次對gateway參數(shù)檢查 #-->
cas-servlet.xml
<!-- 檢查gateway參數(shù) --> <bean id="gatewayParameterCheck" class="com.wisdragon.cas.web.flow.GatewayParameterCheck" c:servicesManager-ref="servicesManager" /> <!-- 檢查gateway參數(shù)# -->
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參數(shù)校驗,校驗信息:gateway={}, 請求服務(wù)信息:{}", gateWayV, service.toString()); return yes(); } } return no(); } }
更新到生產(chǎn)環(huán)境后,經(jīng)用戶測試發(fā)現(xiàn)此方案無效。此方案無效后,只能再次去回歸login-webflow的登錄流程,總結(jié)的流程圖如下:
上圖只是涉及到登錄部分簡單截圖,不涉及到TGT以及ST。
由上圖可以發(fā)現(xiàn),在“ticketGrantingTicketCheck”節(jié)點之后,如果TGT不存在,則會去檢查gateway參數(shù),如果gateway存在,則會去到節(jié)點“gatewayServicesManagementCheck”,如果不存在,則回到節(jié)點“serviceAuthorizationCheck”,到該節(jié)點一般也就會到sso的登錄界面了。
之前在節(jié)點“gatewayRequestCheck”后增加了gateway參數(shù)再次檢查的節(jié)點“gatewayParameterCheck”發(fā)現(xiàn)無效果。
因此可以排除走這條線的可能性,剩下的只有TGT存在無效到sso登錄界面這條線了。在這個action-state之中只是對tgt相關(guān)的cookie進行清除,并沒有對gateway參數(shù)進行檢查,因此有可能是問題的所在。
驗證猜想。
1. 將TGT的存活時間暫時設(shè)置為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秒過后,再次刷新門戶系統(tǒng),發(fā)現(xiàn)果然到sso登錄界面。重復(fù)實現(xiàn)多次發(fā)現(xiàn)都時一樣。由此可以得出,由于門戶使用的TGT失效,并沒有檢查gateway參數(shù)因此跳轉(zhuǎn)到sso的登錄界面。
3. 解決方案:只需在action-state 節(jié)點中增加對gateway參數(shù)的檢查邏輯,根據(jù)檢查的結(jié)果到不同的節(jié)點即可。新的流程圖如下:
在上圖中增加節(jié)點 “terminateSession”的 條件為“gateway”的transition,當請求中存在gateway參數(shù)時,讓流程到gatewayRequestCheck節(jié)點。
代碼:
final HttpServletResponse response = WebUtils.getHttpServletResponse(context); this.ticketGrantingTicketCookieGenerator.removeCookie(response); this.warnCookieGenerator.removeCookie(response); /** * 檢查gateway參數(shù),如果為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ā)現(xiàn)可正常跳轉(zhuǎn)回門戶登錄界面。將TGT的存活時間恢復(fù)默認值,發(fā)布到線上后,留意一段時間,發(fā)現(xiàn)沒有出現(xiàn)改問題,至此解決問題。
總結(jié):
sso login-webflow 登錄流程較為復(fù)雜,出現(xiàn)問題時,應(yīng)該根據(jù)登錄流程圖分析判斷問題出現(xiàn)在哪些節(jié)點上,然后修改相關(guān)參數(shù)重現(xiàn)問題,之后修改相關(guān)邏輯驗證以及解決問題。
相關(guān)文章
SpringBoot多環(huán)境開發(fā)與日志小結(jié)
這篇文章主要介紹了SpringBoot多環(huán)境開發(fā)與日志,下面給大家說一下如何基于多環(huán)境開發(fā)做配置獨立管理,務(wù)必掌握,需要的朋友可以參考下2022-08-08java數(shù)據(jù)庫數(shù)據(jù)分批讀取的實現(xiàn)示例
在處理大量數(shù)據(jù)時,直接從數(shù)據(jù)庫一次性讀取所有數(shù)據(jù)可能會導(dǎo)致內(nèi)存溢出或者性能下降,本文就來介紹一下java數(shù)據(jù)庫數(shù)據(jù)分批讀取的實現(xiàn)示例,感興趣的可以了解一下2024-01-01java實現(xiàn)求只出現(xiàn)一次的數(shù)字
本文主要介紹了java實現(xiàn)求只出現(xiàn)一次的數(shù)字,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02SpringBoot+mybatis實現(xiàn)多數(shù)據(jù)源支持操作
這篇文章主要介紹了SpringBoot+mybatis實現(xiàn)多數(shù)據(jù)源支持操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10springboot+redis+阿里云短信實現(xiàn)手機號登錄功能
這篇文章主要介紹了springboot+redis+阿里云短信實現(xiàn)手機號登錄功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-01-01