MYSQL存储过程实现用户登录

Posted 项希盛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MYSQL存储过程实现用户登录相关的知识,希望对你有一定的参考价值。

mysql存储过程实现用户登录

CREATE DEFINER=`root`@`%` PROCEDURE `uc_session_login`(
    IN `_email` VARCHAR(50),
    IN `_pwdmd5` CHAR(32)
)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ‘‘
BEGIN
    #密钥表
    DECLARE _keyid INT(10)UNSIGNED;
    DECLARE _secretkey CHAR(32);
    #登录表
    DECLARE _uid INT(10)UNSIGNED;
    DECLARE _username VARCHAR(50);
    DECLARE _password CHAR(32);
    DECLARE _salt CHAR(6);
    #地址记录表
    DECLARE _failedlogins INT(10)UNSIGNED;
    #与字段无关的输入
    DECLARE failedlogins_max INT(10)UNSIGNED DEFAULT 1;#密码错误次数上限
    DECLARE failedlogins_timeout INT(10)UNSIGNED DEFAULT 2;#时间范围内清零计数
    DECLARE failedlogins_unlocktime INT(10)UNSIGNED DEFAULT 10;#密码错误次数超限后锁定时间
    DECLARE secretkey2 VARCHAR(32)DEFAULT IFNULL(@secretkey2,‘‘);
    DECLARE useripaddress VARCHAR(39)DEFAULT@ipaddress;
    #与字段无关的输出
    DECLARE json JSON DEFAULT{};
    loop0:LOOP
        #首先判断IP地址是否已被禁止尝试密码
        DELETE FROM uc_session_ipaddress WHERE beforedel<CURRENT_TIMESTAMP();#删除较早的IP
        SELECT failedlogins INTO _failedlogins FROM uc_session_ipaddress WHERE ipaddress=useripaddress;
        IF _failedlogins>failedlogins_max THEN
            SELECT -11 AS errno,您尝试密码错误过多,请稍候再试 AS message;
            LEAVE loop0;
        END IF;
        SELECT uid,username,`password`,salt INTO _uid,_username,_password,_salt FROM uc_members WHERE email=_email;
        IF ISNULL(_uid) THEN SELECT -1 AS errno,您输入的E-Mail尚未注册 AS message;LEAVE loop0;END IF;
        IF MD5(CONCAT(_pwdmd5,_salt))<>_password THEN
            SELECT -2 AS errno,您输入的密码错误 AS message;
            INSERT INTO uc_session_log(uid,ipaddress,errno,error)VALUES(_uid,useripaddress,-2,登录密码错误);
            IF ISNULL(_failedlogins) THEN
                INSERT INTO uc_session_ipaddress(ipaddress,failedlogins,beforedel)VALUES(useripaddress,1,TIMESTAMPADD(SECOND,failedlogins_timeout,CURRENT_TIMESTAMP()));
            ELSE
                UPDATE uc_session_ipaddress SET failedlogins=_failedlogins+1,beforedel=TIMESTAMPADD(SECOND,failedlogins_unlocktime,CURRENT_TIMESTAMP()) WHERE ipaddress=useripaddress;
            END IF;
            LEAVE loop0;
        END IF;
        #登录校验已通过,开始取得secretkey并出hmac
        SELECT id,secretkey INTO _keyid,_secretkey FROM uc_session_key WHERE started<CURRENT_TIMESTAMP() AND expired>CURRENT_TIMESTAMP() ORDER BY expired DESC LIMIT 1;
        IF ISNULL(_keyid) OR ISNULL(_secretkey) THEN SELECT -3 AS errno,secretkey获取失败 AS message;LEAVE loop0;END IF;
        SET json=JSON_SET(json,$.logintime,UNIX_TIMESTAMP(CURRENT_TIMESTAMP(6)));
        SET json=JSON_SET(json,$.loginip,useripaddress);
        SET json=JSON_SET(json,$.uid,_uid);
        SET json=JSON_SET(json,$.email,_email);
        SET json=JSON_SET(json,$.username,_username);
        SET json=JSON_SET(json,$.password,_password);
        SET json=JSON_SET(json,$.salt,_salt);
        SELECT json,_keyid AS keyid,uc_session_hmacsha1(CONCAT(_secretkey,secretkey2),json)AS hmacsha1;
        INSERT INTO uc_session_log(uid,ipaddress,errno,error)VALUES(_uid,useripaddress,0,登录成功);
        LEAVE loop0;
    END LOOP;
END

 

以上是关于MYSQL存储过程实现用户登录的主要内容,如果未能解决你的问题,请参考以下文章

MySQL视图事务与存储过程

linux中mysql如何实现免密码登录

PHP+MySQL制作简单的用户注册登录界面(注释超详细~附源代码)

如何在片段活动中获取用户名?

mysql 存储过程执行异常,参数不是出参

mysql查看有哪些存储过程