繁体   English   中英

Laravel 4 Auth :: attempt在正确值上返回false

[英]Laravel 4 Auth::attempt returns false on correct values

我有登录代码,该代码应通过尝试使用Laravel的Auth::attempt()方法对用户进行身份验证来起作用。 该代码在我的另一个站点上有效,我将其更改为数据库中的passwdEncrypted ,而不是数据库中的Password 我不能更改它,因为该数据库也被另一个应用程序使用。

代码如下:

// check if in database
        $isInDb = User::where('ReferenceCode', $username)->first();
        if($isInDb) {
            // is in database
            // check if password is encrypted yet
            if($isInDb->passwdEncrypted) {
                // password is encrypted
                if(Auth::attempt(array('ReferenceCode' => $username, 'passwdEncrypted' => $password))) {
                    // authenticated
                    $array = array();
                    $array['verified'] = 'true';
                    $array['type'] = Auth::user()->UserType;
                    return Response::json($array);
                } else {
                    // not authenticated
                    $array = array();
                    $array['verified'] = 'false';
                    $array['type'] = $type;
                    return Response::json($array);
                }
            } else {
                // password is not encrypted
                // check if plain text password is correct
                if($isInDb->Password == $password) {
                    // plain text password is correct
                    $hashed = Hash::make($password);
                    $arr = array('passwdEncrypted' => $hashed);
                    $updated = User::where('rsmsOnlineUserID', $isInDb->rsmsOnlineUserID)->update($arr);
                    if($updated) {
                        $newUser = User::find($isInDb->rsmsOnlineUserID);
                        echo $newUser->passwdEncrypted;
                        if(Auth::attempt(array('ReferenceCode' => $username, 'passwdEncrypted' => $password))) {
                            echo 'logged in';
                        } else {
                            dd(DB::getQueryLog());
                            echo 'could not log in';
                        }
                    } else {
                        echo 'did not update';
                    }
                } else {
                    // plain text password is incorrect
                    $array = array();
                    $array['verified'] = 'false';
                    $array['type'] = $type;
                    return Response::json($array);
                }
            }
        } else {
            // not in database
            return Respone::json(array('success' => 'false'));
        }

发生了什么:我无法登录,数据库中的用户名和密码为1234 ,即使我对此进行了硬编码,也无法正常工作。

它首先检查用户是否在数据库中,如果存在,则检查是否存在加密密码,如果没有,则将根据给​​定的密码创建一个与密码中的纯文本密码匹配的密码。然后将用户登录(我别无选择,只能将纯文本密码存储在数据库中,这就是他们想要的方式)。

但是它从not authenticated的代码部分返回{"verified":"false","type":"prospective_employee"} 因此, Auth::attempt()块都不起作用。

我是手动登录的,但是即使Auth::login()也不起作用。

我的User模型中有以下内容(包含主数据库表):

public function getAuthPassword()
{
    return $this->Password;
}

/**
 * Get the token value for the "remember me" session.
 *
 * @return string
 */
public function getRememberToken() {
    return $this->remember_token;
}


public function setRememberToken($value) {
    $this->remember_token = $value;
}


public function getRememberTokenName() {
    return 'remember_token';
}

/**
 * Get the e-mail address where password reminders are sent.
 *
 * @return string
 */
public function getReminderEmail()
{
    return $this->email;
}

请注意,表中有一个名为Password的字段,但这是纯文本密码,我需要针对passwdEncrypted字段进行身份验证。

出于充分的原因,您不能使用Laravel进行此操作,但这是可笑的不安全且危险的。

我别无选择,只能将纯文本密码存储在数据库中,这就是他们想要的方式

我不明白-为什么您要同时存储“未加密”和“已加密”密码? 无关紧要。 您应该只存储加密的密码。 不需要任何其他方式,您需要教育人们为什么。

该代码可在我的另一个站点上运行,我将其更改为数据库中的passwdEncrypted而不是数据库中的密码。 我不能更改它,因为该数据库也被另一个应用程序使用。

Laravel Auth代码经过硬编码以使用“密码”列。 您不能简单地将其更改为另一个列。 这就是您的代码失败的原因。

由于您没有使用密码列,也没有使用加密的密码,因此您最好只创建自己的不安全的登录系统,并根据需要进行定制。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM