简体   繁体   English

一段时间后自动重定向到登录页面

[英]Redirect to login page automatically after some time

How can we redirect to login page automatically after some time? 一段时间后,我们如何自动重定向到登录页面?

I have a requirement to redirect to login page if the current page is idle for 10 minutes in Java/JSP. 如果当前页面在Java / JSP中空闲了10分钟,我需要重定向到登录页面。

I tried to use <meta http-equiv="refresh" content="120;url=./login.html"> tag. 我尝试使用<meta http-equiv="refresh" content="120;url=./login.html">标记。 This works only when I click on any link but not automatically after 2 mins(120secs). 仅当我单击任何链接时此方法有效,但在2分钟(120秒)后自动无效。

Can anyone tell me how to redirect to login page automatically? 谁能告诉我如何自动重定向到登录页面?

Is it Java? 是Java吗? No... But you must use PHP or JavaScript 否...但是您必须使用PHP或JavaScript

JavaScript: JavaScript:

<script>
 //   setTimeout("window.location.href='login.html';",time);
 //example:

    setTimeout("window.location.href='login.html';",120); // after 2 minutes
</script>

Add this code in HTML.head (<html><head>...here...</head>...) 在HTML.head(<html> <head> ... here ... </ head> ...)中添加此代码

Using servlet, you can try this : 使用servlet,您可以尝试以下操作:

response.setHeader("Refresh","120;url=./login.html");

But I do not think that's what you want. 但是我认为这不是您想要的。 In fact, for your needs, you need more than just redirect, you need to : 实际上,对于您的需求,您不仅需要重定向,还需要:

  • Invalidate the session OR make a session-timeout ; 使会话无效或进行会话超时;
  • Filter your ressources so they'll not be accessed unless login is successful. 过滤您的资源,以便除非登录成功,否则将无法访问它们。

As for invalidation from servlet : 至于来自servlet的无效

public LogoutServlet extends HttpServlet {
    @Override
    public void doGet(...) {
       request.getSession().invalidate();
    }
}

A code that I shamelessly copied from here , look into the thread, few good infos there. 我从这里毫不客气地复制了一个代码,调查了线程,那里的信息很少。


As for the session-timeout (check this ): 至于会话超时 (请检查 ):

HttpSession session = request.getSession();
session.setMaxInactiveInterval(2*60);

Which you may combime with a : 您可以将其与组合:

response.sendRedirect("./login.html");

The other way of doing things is a filter, filtering is a technology that protects your ressources ; 另一种处理方式是过滤器, 过滤是一种保护资源的技术。 it's just a servlet of which you override the doFilter methode. 它只是您覆盖其doFilter方法的servlet。 So, inside that doFilter , you can set the session-timeout and redirect, invalidate the session, etc. check this tuto , it does have a " Servlet and JSP Filters " section you could enjoy. 因此,在该doFilter ,您可以设置会话超时和重定向,使会话无效等。检查此tuto ,它确实具有您可以享受的“ Servlet和JSP筛选器 ”部分。

Best of luck. 祝你好运。

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

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