简体   繁体   English

会话结束时如何将Web应用程序流重定向到特定页面?

[英]How to redirect web application flow to specific page when session ends?

I have a web application developed on AngularJS / Javascript, back-end is Java / Spring. 我有一个基于AngularJS / Javascript开发的Web应用程序,后端是Java / Spring。 I have an issue related to session management for my web application. 我的Web应用程序存在与会话管理有关的问题。

I have set the session timeout using the web.xml session-timeout parameter in session-config like below, 我已在session-config中使用web.xml session-timeout参数设置了会话超时,如下所示,

<session-config>
    <session-timeout>1</session-timeout>
</session-config>

Now after 1 minute, the session times out. 现在,一分钟后,会话超时。 I have a scenario where I need to redirect my web application flow to login page after the session has timed out. 我有一种情况,会话超时后需要将Web应用程序流重定向到登录页面。

Scenario 1 : If I click on any link / button which needs to load a new page, it automatically detects that the session has timed out and it redirects the application to login page. 方案1:如果我单击任何需要加载新页面的链接/按钮,它将自动检测到会话已超时,并将应用程序重定向到登录页面。 THIS WORKS FINE. 这很不错。

Scenario 2 : BUT if the page contains any drop-downs that I select, the application doesn't redirect to login page. 方案2:但是,如果该页面包含我选择的任何下拉列表,则该应用程序不会重定向到登录页面。 THIS IS THE ISSUE. 这就是问题。

What happens generally is when I chose any value from drop-down, it makes a rest call to back-end and fetch the data needed to fill in the values on the page. 通常发生的情况是,当我从下拉列表中选择任何值时,它将对后端进行一次调用,并获取填充页面上的值所需的数据。 Now if the session has ended after a minute, and if I select the drop-down, it doesn't make any call to back-end as the session is over. 现在,如果会话在一分钟后结束,并且如果我选择了下拉菜单,则会话结束时不会对后端进行任何呼叫。

Now, in this scenario what should be done to make sure that even when I chose the drop-down and if the session is over, it will detect it somehow and redirect the application to login page. 现在,在这种情况下,应该采取什么措施来确保即使我选择了下拉列表并且会话结束后,会话也将以某种方式检测到该错误并将应用程序重定向到登录页面。 Is there a way using angular JS or javascript to check if the session is still alive when I chose the drop-down value and can redirect the flow as per need? 当我选择下拉值并可以根据需要重定向流时,是否可以使用angular JS或javascript检查会话是否仍然存在?

Considering the fact that when I chose the drop-down the flow doesn't go to back-end, I guess I might need to handle this redirection at client side only (front-end) instead of back-end. 考虑到当我选择下拉列表时流程不会流向后端的事实,我想我可能需要仅在客户端(前端)而不是后端处理此重定向。 Not sure how to do that though. 虽然不知道如何做到这一点。

Any help would be appreciated. 任何帮助,将不胜感激。

You can make use of http intercetor to help you here. 您可以在这里使用http拦截器来帮助您。 Basically, interceptor will be executed every time you make a request, make an error while making a request, when you response or when you get error in response. 基本上,拦截器将在您每次发出请求,在发出请求时出错,当您响应或收到错误响应时都执行。 Depending upon response status, you can redirect the control to an error page. 根据响应状态,您可以将控件重定向到错误页面。

app.factory('CustomHttpInterceptor',[ '$q' , '$location', function( $q , $location, TimeoutBroadCaster ){
        return{
            request: function(config) {
                return config;
            },

            requestError: function(config) {
                return config;
            },

            response: function(res) {
                return res;
            },

            responseError: function(res) {              
                if(res.status != 200){
                     $location.path( 'timed-out' );
                }
                return res;
            }
        }

    }

    ]);

And then in config method at application start up, you push this interceptor in queue with 然后在应用程序启动时的config方法中,将该拦截器与

$httpProvider.interceptors.push('CustomHttpInterceptor');

To make things easier, at server end, you can come up with some custom error status which can help you determine if session had timed out and write check specifically for that condition. 为了使事情变得更容易,在服务器端,您可以提出一些自定义错误状态,这些状态可以帮助您确定会话是否超时,并专门针对该情况进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM