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

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

当前,只有在用户成功登录系统后,我才设置用户会话。但是,似乎我也应该在未成功登录的情况下进行会话,并以某种方式计算登录尝试次数。

Login.jsp(简体版):

User name:

Password:

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框架都有很多内置的安全功能。我还发现使用会话不是跟踪登录尝试的最佳方法,因为用户可以使用不同的浏览器登录。但是,我正在为一个永远不会进入任何生产环境的简单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!
Not a registered cusomer? Please register!");

}

session.setAttribute("loginCount",loginAttempt);

url = "/login.jsp";

}

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);

dispatcher.forward(request, response);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的JavaWeb登录功能实现代码示例: ```java // 导入必要的包 import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { // 设置响应内容类型 response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html><head><title>Login</title></head><body>"); out.println("<h1>JavaWeb登录功能实现</h1>"); // 获取表单参数 String username = request.getParameter("username"); String password = request.getParameter("password"); // 检查登录信息是否正确 if (username.equals("admin") && password.equals("admin")) { // 创建会话并将用户名存储在会话 HttpSession session = request.getSession(true); session.setAttribute("username", username); // 重定向到成功页面 response.sendRedirect("success.jsp"); } else { // 登录失败 out.println("<p>用户名或密码错误,请重新输入!</p>"); out.println("<a href='login.jsp'>返回登录页面</a>"); } out.println("</body></html>"); out.close(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { doGet(request, response); } } ``` 在上述代码,我们使用了 `doGet` 方法来处理 GET 请求和 `doPost` 方法来处理 POST 请求。当用户提交登录表单时,我们获取表单的用户名和密码,并在后台验证。如果登录信息正确,我们创建一个会话并将用户名存储在会话,然后重定向到成功页面。如果登录信息不正确,则返回登录页面并显示错误消息。 这只是一个简单的示例,实际的登录功能可能需要更多的验证和安全措施。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值