这似乎是一个非常普遍的问题。但是我找不到任何可行的解决方案。我们正在使用Richafaces 4,Myfaces 2.0.5和Spring security3.0.X。
在ajax / non ajax请求的会话时间上,应该将用户重定向到登录页面。登录回去后,应该向他显示先前执行的ajax / non ajax操作。
非ajax请求不会遇到任何问题。但是对于ajax请求,不会将用户重定向到登录页面。
我已实现了servlet方法。该解决方案只能在Firefox中使用,而不能在IE 8中使用。
即使在会话超时时正确重定向到登录页面,也可能会有另一个问题。我期望先前登录的ajax请求成功登录后会出现ViewExpiredException。
我想带来ViewExpiredException,因为这两个问题可能相互关联。
任何解决方案/线索,将不胜感激。
由于你使用的是Spring Security 3.0.x,因此可以按此处所述使用自定义sessionManagementFilter
com.icesoft.spring.security.JsfRedirectStrategy类在此处可用
如果你使用的是Spring Security 3.1.x,请进行以下更改
<beans:bean id="sessionManagementFilter" class="org.springframework.security.web.session.SessionManagementFilter"> <beans:constructor-arg name="securityContextRepository" ref="httpSessionSecurityContextRepository" /> <!-- this permits redirection to session timeout page from javascript/ajax or http --> <beans:property name="invalidSessionStrategy" ref="jsfRedirectStrategy" /> </beans:bean> <beans:bean id="jsfRedirectStrategy" class="com.icesoft.spring.security.JsfRedirectStrategy"> <beans:constructor-arg name="invalidSessionUrl" value="/general/logins/sessionExpired.jsf" /> </beans:bean> <beans:bean id="httpSessionSecurityContextRepository" class="org.springframework.security.web.context.HttpSessionSecurityContextRepository"/>
对JSFRedirectStrategy类的唯一更改是前几行:
public class JsfRedirectStrategy implements InvalidSessionStrategy { protected final Log logger = LogFactory.getLog(getClass()); private String invalidSessionUrl; private boolean contextRelative; public JsfRedirectStrategy(String invalidSessionUrl){ this.invalidSessionUrl=invalidSessionUrl; } @Override public void onInvalidSessionDetected(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String redirectUrl = calculateRedirectUrl(request.getContextPath(), invalidSessionUrl);
这也适用于IE8。如果你有兴趣,也可以查看此博客,但是我从未尝试过这样做,因为上面的操作要容易得多。
仅供参考:如果你不使用Spring,则有很多方法可以执行此操作:Primefaces在其站点上执行此操作。 或者通过导入Omnifaces jar 链接更简单