JSP登录页面会话超时

  发布于 2023-02-12 19:29

我已经为酒店管理员的模拟创建了一个登录页面.现在我想为它添加会话时间功能.换句话说,假设用户离开计算机(他仍然登录管理网页)大约10分钟左右.然后,当他回来时,我想结束当前会话,然后重定向到登录页面(这更安全,他的个人信息永远不会丢失!).

我该如何做到这一点?

public class LoginServlet extends SpringInjectedServlet {
@Autowired
private LoginService loginService;


@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    String id = req.getParameter("id");
    String password = req.getParameter("password");

    String error = null;

    //code for checking correct input
}

// mock
private boolean check(String id, String password) {
    return loginService.authenticate(id, password);
}

@Override
public void init() throws ServletException {
    System.out.println("LoginServlet");
}
}

user2779544.. 5

使用身份验证筛选器在检查会话的每个请求

 HttpSession session = request.getSession();
 if (session == null || session.getAttribute("username") == null) {
        // Forward the control to login.jsp if authentication fails or session expires
        request.getRequestDispatcher("/login.jsp").forward(request,
            response);
 }

这将检查每个请求的会话登录用户名,如果它的null或会话已过期,它将重定向到登录页面.

在web.xml中添加它


  5

检查它在这里.

1 个回答
  • 使用身份验证筛选器在检查会话的每个请求

     HttpSession session = request.getSession();
     if (session == null || session.getAttribute("username") == null) {
            // Forward the control to login.jsp if authentication fails or session expires
            request.getRequestDispatcher("/login.jsp").forward(request,
                response);
     }
    

    这将检查每个请求的会话登录用户名,如果它的null或会话已过期,它将重定向到登录页面.

    在web.xml中添加它

    <session-config>
      <session-timeout>5</session-timeout>
    </session-config>
    

    检查它在这里.

    2023-02-12 19:30 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有