フレームワークのないWebアプリケーションがあります。セッションを使用して、失敗したログインをチェックする簡単な方法を実装する必要があります。ユーザーが誤ったユーザー名とパスワードの組み合わせを使用して3回ログインしようとすると、再度ログインを試みる前に20分のタイムアウトが与えられます。
現在、ユーザーがシステムに正常にログインした場合にのみ、ユーザーセッションを設定します。ただし、ログインに失敗した場合もセッションを取得し、なんとかしてログイン試行回数をカウントする必要があるようです。
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フレームワークに多くの組み込みのセキュリティ機能があることがわかりました。また、ユーザーはさまざまなブラウザーでログインできるため、セッションの使用はログイン試行を追跡するための最良の方法ではないこともわかりました。ただし、この機能は、実稼働環境に移行することのない単純なWebプロジェクト用に作成しています。JavaHTTPSessionオブジェクトを使用してこの機能を実装する方法を知りたいです。
さて、これが私が受け取ったフィードバックに基づいた私の完全な解決策です。同様の問題で他の人を助けるかもしれない場合に備えて、私はこれを投稿しています:
// 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);