我已经在使用 Spring Security 的地方创建了 webapp,并且我已经将 2 个自定义过滤器添加到如下所示的 spring-security.xml 文件中。
<security:custom-filter ref="authenticationFilter" position="FORM_LOGIN_FILTER"/>
<security:custom-filter ref="concurrencyFilter" position="CONCURRENT_SESSION_FILTER"/>
它适用于非 ajax 请求。当我尝试在 session 已过期时发送 AJAX 请求时,操作会以 html 形式返回登录页面,作为响应加载到 div 元素中。 我已经搜索了解决方案并找到了这个链接 Session Timeout handling for Ajax calls其中定义的功能返回状态代码,此代码在 javascript 端使用,以便将用户导航到登录页面。
我的问题是:
必须在 spring-security.xml 文件中定义 authenticationFilter、concurrencyFilter 和 ajaxTimeoutRedirectFilter 的顺序正确处理请求管道?
最佳答案
查看3) 过滤器配置:
The idea is to add the above custom filter in the Spring Security filter chain. The order in the filter chain is crucial. Our filter should intercept the session timeout for Ajax calls before the vanilla ExceptionTranslationFilter in order to send the custom HTTP error code.
在xml配置中添加:
<custom-filter ref="ajaxTimeoutRedirectFilter" after="EXCEPTION_TRANSLATION_FILTER"/>
在java配置中添加:
@Bean
public Filter ajaxTimeOutRedirectFilter() {
AjaxTimeOutRedirectFilter f = new AjaxTimeOutRedirectFilter();
//f.setCustomSessionExpiredErrorCode(901);
return f;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.addFilterAfter(ajaxTimeOutRedirectFilter(), ExceptionTranslationFilter.class)
...
...
}
它对我有用,感谢 DuyHai's Java Blog和 Demo application for the article
关于java - Ajax 调用的 Spring security session 超时处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24730917/