프레임 워크가없는 웹 응용 프로그램이 있습니다. 세션을 사용하여 실패한 로그인을 확인하는 간단한 방법을 구현해야합니다. 사용자가 잘못된 사용자 이름/암호 조합을 사용하여 3 번 로그인하려고하면 다시 로그인을 시도하기 전에 20 분의 시간 제한이 부여됩니다.Java에서 HttpSession을 사용하여 로그인 시도를 추적하는 방법은 무엇입니까?
현재 사용자가 시스템에 성공적으로 로그인 한 경우에만 사용자 세션을 설정합니다. 그러나 로그인에 실패 할 경우 세션을 가져와 로그인 시도를 어떻게 든 계산해야합니다.
의 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 HTTPSession Object를 사용하여 구현하는 방법을 알고 싶습니다.
좋아, 내가받은 피드백을 바탕으로 내 전체 솔루션입니다. 나는이 경우에 비슷한 문제로 다른 사람을 도울 수 게시하도록하겠습니다 :
// 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);
가 편집, Bhavik에 협조 해 주셔서 감사합니다 아래의 코드를 시도 할 수 있습니다. 나는 아직도 여기에서 새로운 시스템을 배우고있다. – Marta