这篇文章主要介绍了使用gateway后静态资源失效怎么办,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
配置文件方式对应服务配置文件目录提供参考
F12可以看到静态资源路径全部都是加载失败。这是因为我们没有对静态文件进行路由导致。
贴出主要配置:/static/**表示对静态资源的路由
routes:
- id: home-service
uri: lb://home-service #lb表示从注册中心找到服务
predicates: #路由规则
- Path=/home-service/**, /static/**
- id: user-service
uri: lb://user-service
predicates:
- Path=/user-service/**, /static/**
spring:
resources:
static-locations: 静态资源路径
当用户在门户登录后(门户集成sso(4.0 版本)单点登录,门户使用自己的登录界面,使用sso中的gateway=true特性实现),经过一段时间后,用户刷新门户,如果门户会话状态和全局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();
}
}
更新到生产环境后,经用户测试发现此方案无效。此方案无效后,只能再次去回归login-webflow的登录流程,总结的流程图如下:
上图只是涉及到登录部分简单截图,不涉及到TGT以及ST。
由上图可以发现,在“ticketGrantingTicketCheck”节点之后,如果TGT不存在,则会去检查gateway参数,如果gateway存在,则会去到节点“gatewayServicesManagementCheck”,如果不存在,则回到节点“serviceAuthorizationCheck”,到该节点一般也就会到sso的登录界面了。
之前在节点“gatewayRequestCheck”后增加了gateway参数再次检查的节点“gatewayParameterCheck”发现无效果。
因此可以排除走这条线的可能性,剩下的只有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. 重新发布sso程序,从门户认证成功后,等待60秒过后,再次刷新门户系统,发现果然到sso登录界面。重复实现多次发现都时一样。由此可以得出,由于门户使用的TGT失效,并没有检查gateway参数因此跳转到sso的登录界面。
3. 解决方案:只需在action-state 节点中增加对gateway参数的检查逻辑,根据检查的结果到不同的节点即可。新的流程图如下:
在上图中增加节点 “terminateSession”的 条件为“gateway”的transition,当请求中存在gateway参数时,让流程到gatewayRequestCheck节点。
代码:
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);
感谢你能够认真阅读完这篇文章,希望小编分享的“使用gateway后静态资源失效怎么办”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。