Grails,在AJAX调用中处理会话超时

时间:2017-07-24 12:24:30

标签: ajax grails spring-security session-timeout

我有一个基于Spring-Security的grails-app,当会话超时时我想重定向到起始页面。

这似乎很难解决,所以至少我想要一个解决方案,当你点击连接到Ajax函数的链接时,该解决方案会重定向到起始页面。 这是因为当你点击链接时很烦人,因为你的会话超时了,没有任何反应。 希望有人能告诉我一个简单的解决方案或至少是一个解决方案。

1 个答案:

答案 0 :(得分:0)

一种选择是传递标题" nopage"有价值"真"使用ajax调用,spring安全插件的AjaxAwareAuthenticationEntryPoint将返回http状态代码401

另一种选择是创建自定义身份验证入口点。并在commence中检查它是否是ajax请求,只返回SC_UNAUTHORIZED响应代码。在您的javascript中检查状态代码是否为SC_UNAUTHORIZED 401,请正确处理。

class CustomAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {

    public CustomAuthenticationEntryPoint(String loginFormUrl) {
        super(loginFormUrl)
    }

    @Override
    void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, javax.servlet.ServletException {
        if (SpringSecurityUtils.isAjax(request)) {
            response.sendError(HttpServletResponse.SC_UNAUTHORIZED)
            return
        }
        super.commence(request, response, authException)
    }

}

将bean注册为名为authenticationEntryPoint的spring bean(请参阅SpringSecurityCoreGrailsPlugin并查找名为authenticationEntryPoint的bean,您需要覆盖该bean。

如果您使用的是jquery,我认为您可以使用全局ajaxError callback

相关问题