【问题标题】:Grails, Handling session time out in AJAX-callGrails,在 AJAX 调用中处理会话超时
【发布时间】:2017-12-30 00:56:36
【问题描述】:

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

这似乎很难解决,所以至少我想要一个解决方案,当您单击连接到 Ajax 功能的链接时重定向到起始页面。 那是因为当您单击链接时很烦人,并且由于会话超时而没有任何反应。 希望有人能给我一个简单的解决方案,或者至少是一个解决方案。

【问题讨论】:

  • 我认为如果您的 ajax 在 sesson 到期后调用具有安全注释的控制器操作 spring-security-core 插件会自动将用户重定向到登录页面。如果您使用的是 spring-security-rest 插件然后您可以检查 401 (UnAuthorized) 响应状态并重定向到登录页面。

标签: ajax grails spring-security session-timeout


【解决方案1】:

一种选择是在您的 ajax 调用中传递值为“true”的标头“nopage”,spring 安全插件的AjaxAwareAuthenticationEntryPoint 将返回 http 状态代码 401

另一种选择是创建自定义身份验证入口点。在commence 检查它是否是一个ajax 请求,然后只返回SC_UNAUTHORIZED 响应代码。在你的javascript中检查状态码是否为SC_UNAUTHORIZED401,然后适当地处理它。

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

【讨论】:

    猜你喜欢
    • 2011-07-11
    • 1970-01-01
    • 2013-01-20
    • 2014-03-22
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    相关资源
    最近更新 更多