在基于Spring的Web应用程序中处理会话过期事件

我在我的应用程序中使用了Spring安全function,但我发现当会话过期时,所有请求ajax都返回页面login.jsp(不重定向,在http响应中,它放入所有html内容)这是登录页面我的webapp。 我在我的应用程序中使用了很多ajax请求,目标是返回某些错误代码,如510而不是登录页面。

 

没有invalid-session-url我试图使invalid-session-url =“”无法正常工作。 非常感谢

使用自定义AuthenticationEntryPoint :

 package com.example.spring.security // imports here public class AjaxAwareAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint { public AjaxAwareAuthenticationEntryPoint(final String loginFormUrl) { super(loginFormUrl); } @Override public void commence(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException authException) throws IOException, ServletException { if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) { response.sendError(403, "Forbidden"); } else { super.commence(request, response, authException); } } } 

定义一个bean并将其用作元素中的 entry-point-ref