且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Spring安全会话超时处理Ajax调用

更新时间:2021-10-07 00:46:56

查看 3)过滤器配置:

这个想法是在Spring Security过滤器链中添加上述自定义过滤器.筛选器链中的顺序至关重要.为了发送自定义HTTP错误代码,我们的过滤器应在香草ExceptionTranslationFilter 之前拦截Ajax调用的会话超时.

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配置中添加:

in java config add :

@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的Java博客

it works for me, thanks to DuyHai's Java Blog and Demo application for the article