我有登录代码,应该通过尝试使用Laravel的Auth::attempt()
方法验证用户来工作。这段代码在我的另一个网站上工作,我已经改变了它,而不是数据库中的Password
,它被存储为passwdEncrypted
。我不能更改它,因为数据库正在被另一个应用程序使用。
代码如下:
// 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中这样做,并且有很好的理由,但它非常不安全且危险。
我别无选择,只能将纯文本密码存储在数据库,这就是他们想要的
我不明白-为什么你存储一个"未加密"answers"加密"的密码?没有意义。你应该只存储加密的密码。没有必要采取任何其他方法,你需要教育人们为什么。
这段代码适用于我的另一个站点,我将其改为密码在数据库中的存储形式为passwdEncrypted。我由于数据库正在被其他应用程序使用,因此无法更改它好。
Laravel认证码是硬编码的,以使用"password"列。您不能简单地将其更改为另一列。这就是你的代码失败的原因。
既然您没有使用密码列,并且由于您没有使用加密密码,那么您不妨创建自己的不安全登录系统,根据您的要求进行定制。