【发布时间】:2014-08-16 03:54:12
【问题描述】:
我刚开始使用 Laravel,但我的身份验证仍然存在问题。我需要在现有的数据库结构上构建我的系统。用户表密码使用 md5,我喜欢将其转换为 Laravel 哈希。但是在将 md5 密码转换为散列后,登录失败并出现这个新的散列。我找不到这个问题的解决方案。
表格
- id (int11)
- gebruikersnaam (varchar 255)
- wachtwoord (varchar 255)
- updated_at(时间戳)
- created_at(时间戳)
- remember_token(时间戳)
用户模型
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',
);
注意:Input::get('wachtwoord') 和 Input::get('gebruikersnaam') 已通过控制器中的帖子正确填写。 md5 已正确更改为我的数据库中的 Laravel 哈希,所以我找不到我处理的问题。
注意2:“gebruikersnaam”是荷兰语的用户名,“wachtwoord”是荷兰语的密码
注意 3:我使用 Laravel 4
** 编辑 **
输出 $user
["attributes":protected]=> array(16) { ["id"]=> int(8681) ["gebruikersnaam"]=> string(22) "---" ["wachtwoord"]= > 字符串(60)“$2y$10$i3bemDK9NzNf/E0jmliv/eBPrqhq/3s3WGPTX3h6WNCMlXcS5W51i”[“电子邮件”]=>字符串(22)“---”[“voornaam”]=>字符串(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"]=>字符串(1)“1”[“updated_at”]=>字符串(19)“2014-06-25 14:53:43”[“created_at”]=>字符串(19)“0000-00-00 00:00 :00" ["remember_token"]=> 字符串(0) "" }
**在 updated_at 中发现奇怪的 000000 **
【问题讨论】:
-
我仔细研究了代码,但找不到任何缺陷。您介意创建一个新用户并发布哈希后将在数据库中的密码哈希吗?
-
感谢您的评论。我已经散列了“测试”->“$2y$10$bFqhL0J33g11iWQ9wa5Xv.AjhdaNp1nbTDsrvg9MRs6svTxVDZfo。”
-
好的,哈希值应该是 60 个字符。这似乎很奇怪。保存模型后尝试dump,对象是否包含密码属性?
-
我添加了一些额外的信息
-
需要澄清一下您的措辞...将 md5 密码转换为哈希:您实际上是指对
md5 password或plain password进行哈希处理吗?
标签: php authentication laravel laravel-4