登录失败3次验证码校验

--开发背景: 当对客户端访问时,分机器访问和人工访问,为遏制机器登录攻击,建立验证码错误3次,针对IP地址一天之内登录失败3次,则对其需要增加验证码的校验。
-- 为不用每次都记录一条错误登录的时间记录,使用存储过程。

--submit 登录失败,更新存储过程

--页面刷新,查询错误次数,然后程序设置显示验证码。

--提示:虽然有效机器人登录,但是验证码的识别技术 早已经公开,防止攻击的 有效手段还需加强。

    --针对登录失败,记录针对IP地址的登录失败记录表
            create table LoginRecords
            (
               LoginId  uniqueidentifier primary key default(newid()), -- GUID 类型,自动生成
               IP_Address varchar(200),                                

   -- 客户端IP地址
               LoginTime datetime default(getdate()),                   -- 上次登

录失败的时间
               ErrorTimes int default(0)                               

-- 错误次数
            )


            ---更新错误次数存储过程 

            create proc [usp_GetErrorTimes]
            @ip_address varchar(200)
            as 
            declare @lastTime datetime
            select @lastTime=LoginTime from LoginRecords where IP_Address=@ip_address  
            if(@lastTime is null)
            begin
               insert into LoginRecords(IP_Address,ErrorTimes)values(@ip_address,1);
            end 
            else
            begin

                if(DATEDIFF(day,@lastTime,getdate())>1)  -- 上次登录错误的时间与当前登录错误的时

间 相减 > 1begin
                        update LoginRecords set ErrorTimes=1,LoginTime=GETDATE()  --

重新计数
                         where IP_Address=@ip_address
                    end
                else
                    begin
                        update LoginRecords set ErrorTimes=ErrorTimes

+1,LoginTime=GETDATE() --累加错误次数
                         where IP_Address=@ip_address                 
                    end
            end


        --  exec usp_GetErrorTimes '192.168.1.152'

 

posted @ 2013-04-26 12:01  TakeTry  阅读(1748)  评论(0编辑  收藏  举报