Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中使用HttpSession跟踪登录尝试?_Java_Jsp_Servlets - Fatal编程技术网

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

如何在Java中使用HttpSession跟踪登录尝试?,java,jsp,servlets,Java,Jsp,Servlets,我有一个没有框架的web应用程序。我需要实现一种简单的方法来检查不成功的登录,使用会话。如果用户尝试使用不正确的用户名/密码组合登录3次,则会给他们20分钟的超时时间,然后再尝试登录 目前,我仅在用户成功登录到系统时设置用户会话。然而,似乎我也应该在登录失败的情况下获得一个会话,并以某种方式计算登录尝试次数 Login.jsp(简化版): 在进行研究时,我发现各种Java框架都有很多内置的安全特性。我还发现,使用会话并不是跟踪登录尝试的最佳方式,因为用户可以使用不同的浏览器登录。但是,我正在为一

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

目前,我仅在用户成功登录到系统时设置用户会话。然而,似乎我也应该在登录失败的情况下获得一个会话,并以某种方式计算登录尝试次数

Login.jsp(简化版):

在进行研究时,我发现各种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);
//查看客户是否为有效用户
String selectQuery=“选择firstName、lastName、来自客户的电子邮件,其中用户名='“+userName+”,密码='“+password+”;
selectResult=statement.executeQuery(selectQuery);
if(选择result.next())
{
//我们有一个有效的用户,让我们登录他们
客户=新客户();
customer.setFirstName(selectResult.getString(“firstName”);
customer.setLastName(selectResult.getString(“lastName”);
customer.setEmail(selectResult.getString(“email”);
customer.setUserName(用户名);
customer.setPassword(密码);
//建立用户会话
session.setAttribute(“客户”,客户);
setAttribute(“firstName”,customer.getFristName());
url=“/index.jsp”;
选择result.close();
}
其他的
{
int LOGINATTEST;
if(session.getAttribute(“loginCount”)==null)
{
session.setAttribute(“loginCount”,0);
loginattent=0;
}
其他的
{
LoginAttest=(整数)session.getAttribute(“loginCount”);
}
//这是从0,1,2开始的3次尝试计数
如果(登录尝试>=2)
{        
long lastAccessedTime=会话。getLastAccessedTime();
日期=新日期();
long currentTime=date.getTime();
long-timeDiff=currentTime-lastAccessedTime;
//20分钟(毫秒)
如果(时间差>=1200000)
{
//使用户会话无效,以便他们可以重试
session.invalidate();
}
其他的
{
//错误消息
session.setAttribute(“消息”,“您已超过3次失败的登录尝试。请在20分钟内尝试登录,或致电1-800 555-1212联系我们的客户服务中心。”);
}  
}
其他的
{
LoginAttest++;
int allowLogin=3-LOGINATTEST;
session.setAttribute(“message”、“LoginAttest=“+LoginAttest+”。用户名或密码无效。您还有“+allowLogin+”尝试。请重试!
不是注册客户?请!”); } session.setAttribute(“loginCount”,loginattest); url=“/login.jsp”; } RequestDispatcher=getServletContext().getRequestDispatcher(url); 转发(请求、响应);
您可以尝试以下代码

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

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

谢谢你对编辑的帮助,巴维克。我还是新来的,正在学习这个系统。这是一个很好的开始,但还没有完全发挥作用。LoginAttent不能分配给int,它是一个对象。是否应将其转换为int?另外,如何为用户设置超时,以便允许他/她在20分钟内再次尝试?我是否使用session.setMaxInactiveInterval?我在这里说过,如果我想计算用户尝试登录请求的次数,该怎么办?您可以使用
session.getLastAccessedTime()
并将该时间与间隔验证的当前系统时间进行比较很好,我会这样做。非常感谢。这种“解决方案”不够稳健。黑客仍然可以通过不发送会话cookie来规避这一问题,因为会话cookie每次都会在服务器端显示为一个新的会话。
// 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++);
}