我刚刚开始使用Laravel,但我的身份验证仍然存在问题。我需要在现有的数据库结构上构建我的系统。用户表密码使用md5,我喜欢将其转换为Laravel Hash。但是在将md5密码转换为hash之后,登录失败并使用此新哈希。我找不到解决这个问题的方法。
表格
用户模型
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'gebruikers';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('wachtwoord');
}
登录控制器
class LoginController extends BaseController {
function starten(){
$rules = array(
'gebruikersnaam' => 'required', // make sure the email is an actual email
'wachtwoord' => 'required' // password can only be alphanumeric and has to be greater than 3 characters
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::to('/')
->withErrors($validator)
->withInput(Input::except('password'));
} else {
$user = User::where('gebruikersnaam','=',Input::get('gebruikersnaam'))
->first();
if(isset($user)) {
if($user->wachtwoord == md5(Input::get('wachtwoord'))) {
$user->wachtwoord = Hash::make(Input::get('wachtwoord'));
$user->save();
}
}
$userdata = array(
'gebruikersnaam' => Input::get('gebruikersnaam'),
'wachtwoord' => Input::get('wachtwoord')
);
if (Auth::attempt($userdata)) {
return Redirect::to('users/dashboard')
->with('message', 'You are now logged in!');
} else {
return Redirect::to('/')
->with('message', 'Deze combinatie van gebruikersnaam en wachtwoord lijkt helaas niet te kloppen')
->withInput();
}
}
}
}
验证
return array(
'driver' => 'eloquent',
'model' => 'User',
'table' => 'gebruikers',
'reminder' => array(
'email' => 'emails.auth.reminder',
'table' => 'password_reminders',
'expire' => 60,
),
'username' => 'gebruikersnaam',
'password' => 'wachtwoord',
);
注意:输入:: get('wachtwoord')和Input :: get('gebruikersnaam')在控制器中通过post正确填充。 md5在我的数据库中被正确地更改为Laravel哈希,所以我无法找到我处理的问题。
注意2:“gebruikersnaam”是用户名的荷兰语,“wachtwoord”是荷兰语用于密码
注3:我使用Laravel 4
**编辑**
输出$ user
[ “属性”:保护] => array(16){[“id”] => int(8681)[“gebruikersnaam”] => string(22)“---”[“wachtwoord”] => string(60)“$ 2y $ 10 $ i3bemDK9NzNf / E0jmliv / eBPrqhq / 3s3WGPTX3h6WNCMlXcS5W51i”[“email”] => string(22)“---”[“voornaam”] => string(5)“Jelle”[“tussenvoegsel”] => NULL [“achternaam”] => string(4)“Pals”[“school_id”] => int(110)[“telefoonnummer”] => string(10)“0655684308”[“geslacht”] => string(3)“man”[“geboortedatum”] => string(19)“1990-03-22 09:00:00”[“groep_id”] => int(8811)[“status”] => string(1)“1”[“updated_at”] => string(19)“2014-06-25 14:53:43”[“created_at”] => string(19)“0000-00-00 00:00:00”[“remember_token”] => string(0)“”}
**在updated_at **中找到奇怪的000000
答案 0 :(得分:3)
我认为问题在于您使用默认的Eloquent用户设置(UserTrait),假设您的密码字段为“密码”(请参阅here),但您使用的是“wachtwoord”。因此,登录失败是由于密码列中缺少值(由于缺少完整的列)来测试密码。
如果您正在考虑“但我在auth配置中明确指定了密码列!”不幸的是,这是一个红色的鲱鱼 - 如果你使用标准的数据库身份验证,那么配置的部分是你的,而你正在使用Eloquent身份验证(请注意,在你的配置中,driver
设置为{{1}因为你需要根据interface在模型中指定这些字段。
幸运的是,一个类自己的实现会覆盖一个特性,所以理论上你可以在你的模型上保留特征,然后覆盖eloquent
方法。
那就是说,这个特质(以及可提醒者)非常假设你在任何地方都使用英文名字,所以你可能值得完全删除这两个特征(getAuthPassword
和UserTrait
)并编写自己的界面方法版本,因为如果/当您使用“记住我”或“忘记密码”时,您可能会看到同样的事情发生。
编辑:初步建议:
RemindableTrait
但是,正如我所说,如果你继续使用荷兰语专栏名称,你最终会完全放弃这些特征并自己实施这些方法:
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface
{
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'gebruikers';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('wachtwoord');
/**
* Override UserTrait#getAuthPassword
*/
public function getAuthPassword()
{
return $this->wachtwoord;
}
}