Tapestry 应用程序中的会话超时 AJAX 错误

发布于 2024-09-24 06:56:39 字数 616 浏览 7 评论 0原文

我正在使用 Tapestry 结合 Spring Security 和 Prototype 之外的 jQuery 库构建一个 Web 应用程序。当用户在会话超时后单击链接时,他会自动重定向到登录页面。当然,这不适用于触发 AJAX 请求的链接。

我知道,这是任何类型的 Web 应用程序的常见问题(例如 http://www .openjs.com/articles/ajax/session_timeout.php)。 Tapestry 5 是否有最佳实践解决方案?

编辑 以下解决方案(感谢 Henning)对我有用:

Ajax.Responders.register(
{
    onException: function()
    {
        window.location.reload();
    }
});

如果 AJAX 调用期间发生故障,则会触发页面重新加载,从而重定向到登录页面。它仍然需要一些调整(例如显示错误消息而不是重定向),但使用 Ajax.Responders 基本上似乎是一个很好的方法。

I'm building a webapp using Tapestry in combination with Spring Security and the jQuery-library besides Prototype. When a user clicks on a link after his session timed out, he is automatically redirected to the login page. This, of course, does not work for links, that trigger an AJAX-request.

I know, this is a common problem with any kind of web application (e.g. http://www.openjs.com/articles/ajax/session_timeout.php). Is there a best practice solution for Tapestry 5?

EDIT
The following solution (thanks to Henning) works for me:

Ajax.Responders.register(
{
    onException: function()
    {
        window.location.reload();
    }
});

In case of a failure during an AJAX-call a page reload is triggered, which in result redirects to the login-page. It still needs some tuning (e.g. display an error message instead of redirect), but using Ajax.Responders basically seems a good way to do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷了相思 2024-10-01 06:56:39

对于使用 Prototype 的 AJAX,您可以添加一个全局侦听器,使用 AJAX.Responders< /a>; jQuery 有一个类似的构造,名为 Ajax Events,您可以使用它。

两个事件处理程序都应该在出现 403 错误时重定向到登录页面。您可以创建一个具有此功能的 mixin 并将其添加到布局组件中。

我还使用了一种机制,可以在应用程序仍然在浏览器窗口中打开时防止会话超时,只需执行 AJAX 调用并每隔几分钟接收一个空响应,从而保持会话打开。很蠢,但是工作正常。

For the AJAX that uses Prototype, you could add a global listener that reacts to AJAX failures using AJAX.Responders; jQuery has a similar construct called Ajax Events that you could use.

Both event handlers should just redirect to the login page on a 403 error. You could create a mixin with this functionality and add it to your layout component.

I have also used a mechanism that prevents session timeouts while the app is still open in a browser window by just doing an AJAX call and receiving an empty response every couple of minutes, thus keeping the session open. Stupid, but works okay.

后知后觉 2024-10-01 06:56:39

贡献T5主调度程序


public class AjaxAccessController implements Dispatcher {

    @Override
    public boolean dispatch(Request request, Response response) throws IOException {

        // Si no hay session y la petición es ajax, recargar la página
        Session session = request.getSession(false);
        if (session == null && request.isXHR()) {
            OutputStream os = response.getOutputStream("application/json;charset=UTF-8");
            os.write("{\"script\":\"window.location.reload();\"}".getBytes());
            os.flush();
            return true;
        }

        return false;
    }
}

你可以在你的AppModule.java中


public static void bind(ServiceBinder binder) {
        // binder.bind(MyServiceInterface.class, MyServiceImpl.class);
        // Make bind() calls on the binder object to define most IoC services.
        // Use service builder methods (example below) when the implementation
        // is provided inline, or requires more initialization than simply
        // invoking the constructor.

        // Id de AjaxAccessController
        binder.bind(AjaxAccessController.class).withId("AjaxAccessController");
    }

public void contributeMasterDispatcher(
            OrderedConfiguration configuration,
            @InjectService("AjaxAccessController") Dispatcher accessController) {

        configuration.add("AjaxAccessController", accessController, "before:ComponentEvent");
    }

所以每个没有会话的ajax请求,页面将重新加载并重定向到你的索引页面

you can contribute the T5 master dispatcher


public class AjaxAccessController implements Dispatcher {

    @Override
    public boolean dispatch(Request request, Response response) throws IOException {

        // Si no hay session y la petición es ajax, recargar la página
        Session session = request.getSession(false);
        if (session == null && request.isXHR()) {
            OutputStream os = response.getOutputStream("application/json;charset=UTF-8");
            os.write("{\"script\":\"window.location.reload();\"}".getBytes());
            os.flush();
            return true;
        }

        return false;
    }
}

In your AppModule.java


public static void bind(ServiceBinder binder) {
        // binder.bind(MyServiceInterface.class, MyServiceImpl.class);
        // Make bind() calls on the binder object to define most IoC services.
        // Use service builder methods (example below) when the implementation
        // is provided inline, or requires more initialization than simply
        // invoking the constructor.

        // Id de AjaxAccessController
        binder.bind(AjaxAccessController.class).withId("AjaxAccessController");
    }

public void contributeMasterDispatcher(
            OrderedConfiguration configuration,
            @InjectService("AjaxAccessController") Dispatcher accessController) {

        configuration.add("AjaxAccessController", accessController, "before:ComponentEvent");
    }

So every ajax request without session, the page will reloads and it redirects to your index page

公布 2024-10-01 06:56:39

好吧,向服务器发出 Ajax 请求,它发送标头“HTTP_X_REQUESTED_WITH”,值为“XMLHttpRequest”。您可以在服务器端检查是否是具有上述标头的 ajax 请求以及登录和会话超时的条件,然后再在索引页面中继续操作。

如果您的条件匹配,则只需在函数中打印“window.top.location.href='登录页面'”即可。

在 PHP 中,我可以这样做,

<?php if($_SERVER['HTTP_X_REQUESTED_WITH'] === "XMLHttpRequest" && condition for session check){
    echo "<script>window.top.location.href='login.php'</script>";
    }

?>

您可以在框架中添加与它类似的条件。

Well, Ajax request is made to server it sends the header "HTTP_X_REQUESTED_WITH" with value "XMLHttpRequest". You can just check serverside that whether it is ajax request with above header and condition for login and session timeout before proceeding further in your index page.

If your criteria gets matched then simply print "window.top.location.href='login page'" in your function.

In PHP i can do this as ,

<?php if($_SERVER['HTTP_X_REQUESTED_WITH'] === "XMLHttpRequest" && condition for session check){
    echo "<script>window.top.location.href='login.php'</script>";
    }

?>

You can add the condition similar to it in your framework.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文