gpt4 book ai didi

java - 如何在 Java 中使用 HttpSession 跟踪登录尝试?

转载 作者:行者123 更新时间:2023-11-30 09:29:13 24 4
gpt4 key购买 nike

我有一个无框架网络应用程序。我需要使用 session 实现一种检查不成功登录的简单方法。如果用户尝试使用不正确的用户名/密码组合登录 3 次,他们将获得 20 分钟的超时时间,然后才能再次尝试登录。

目前我只在用户成功登录系统时设置一个用户 session 。但是,似乎我也应该在登录不成功的情况下获得一个 session ,并以某种方式计算登录尝试次数。

Login.jsp(简化版):

<form name="loginForm" method="post" action="CustomerData">
User name:<input type="text" name="userName"/>
Password:<input type="password" name="password"/>
<input type="button" value="submit">

CustomerData.java(简化版):

// See if customer is a valid user
String selectQuery = "Select firstName,lastName,email from customer where userName='"+userName+"' and password='"+password+"'";
selectResult = statement.executeQuery(selectQuery);

if(selectResult.next())
{
// We got a valid user, let's log them in
....
HttpSession session = request.getSession(true);
session.setAttribute("customer", customer);
}
else
{
// this is where I need to get the session id (??),
// count the unsuccessful login attempts somehow,
//and give them a 20 minutes timeout before they can try logging in again.

request.setAttribute("message","Invalid username or password. Please try again!");

}

在做研究时,我发现各种 Java 框架都有很多内置的安全功能。我还发现使用 session 并不是跟踪登录尝试的最佳方式,因为用户可以使用不同的浏览器登录。但是,我正在为一个永远不会进入任何生产环境的简单 Web 项目创建此功能。我想知道如何使用 Java HTTPSession 对象实现此功能。

好的,这是我根据收到的反馈提出的完整解决方案。我发布这个以防它可以帮助其他有类似问题的人:

// See if customer is a valid user
String selectQuery = "Select firstName,lastName,email from customer where userName='"+userName+"' and password='"+password+"'";
selectResult = statement.executeQuery(selectQuery);

if(selectResult.next())
{
// We got a valid user, let's log them in
Customer customer = new Customer();
customer.setFirstName(selectResult.getString("firstName"));
customer.setLastName(selectResult.getString("lastName"));
customer.setEmail(selectResult.getString("email"));
customer.setUserName(userName);
customer.setPassword(password);

// establish a user session
session.setAttribute("customer", customer);
session.setAttribute("firstName", customer.getFristName());
url = "/index.jsp";
selectResult.close();

}
else
{
int loginAttempt;
if (session.getAttribute("loginCount") == null)
{
session.setAttribute("loginCount", 0);
loginAttempt = 0;
}
else
{
loginAttempt = (Integer) session.getAttribute("loginCount");
}

//this is 3 attempt counting from 0,1,2
if (loginAttempt >= 2 )
{
long lastAccessedTime = session.getLastAccessedTime();
date = new Date();
long currentTime = date.getTime();
long timeDiff = currentTime - lastAccessedTime;
// 20 minutes in milliseconds
if (timeDiff >= 1200000)
{
//invalidate user session, so they can try again
session.invalidate();
}
else
{
// Error message
session.setAttribute("message","You have exceeded the 3 failed login attempt. Please try loggin in in 20 minutes, or call our customer service center at 1-800 555-1212.");
}

}
else
{
loginAttempt++;
int allowLogin = 3-loginAttempt;
session.setAttribute("message","loginAttempt= "+loginAttempt+". Invalid username or password. You have "+allowLogin+" attempts remaining. Please try again! <br>Not a registered cusomer? Please <a href=\"register.jsp\">register</a>!");
}
session.setAttribute("loginCount",loginAttempt);
url = "/login.jsp";

}

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);

最佳答案

你可以试试下面的代码

int loginAttempt = (Integer)session.getAttribute("loginCount");

if (loginAttempt > 3 ){
// Error message/page redirection
}else{
session.setAttribute("loginCount",loginAttempt++);
}

关于java - 如何在 Java 中使用 HttpSession 跟踪登录尝试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13694239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com