gpt4 book ai didi

java - 在基于 Spring 的 Web 应用程序中处理 session 过期事件

转载 作者:行者123 更新时间:2023-11-30 07:24:50 38 4
gpt4 key购买 nike

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

<session-management session-authentication-strategy-ref="example" /> 

没有无效 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 <http> element :

<http entry-point-ref="authenticationEntryPoint">
<!-- more configuration here -->
</http>

<bean id="authenticationEntryPoint"
class="com.example.spring.security.AjaxAwareAuthenticationEntryPoint">
<constructor-arg value="/login.jsp"/>
</bean>

关于java - 在基于 Spring 的 Web 应用程序中处理 session 过期事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11242174/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com