【问题标题】:Laravel authentication failsLaravel 身份验证失败
【发布时间】: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 passwordplain password 进行哈希处理吗?

标签: php authentication laravel laravel-4


【解决方案1】:

我认为问题在于您使用的是默认的 Eloquent 用户设置 (UserTrait),该设置假定您的密码字段是“密码”(请参阅​​here),但您使用的是“wachtwoord”。因此,由于密码列中缺少用于测试密码的值(由于完全缺少该列),登录失败。

如果您在想“但我在身份验证配置中明确指定了密码列!”不幸的是,这有点像红鲱鱼 - 如果您使用标准 DB 身份验证,配置的那部分是针对,而您使用的是 Eloquent 身份验证(请注意,在您的配置中,您如何将 driver 设置为 eloquent?) ,因此您需要根据 interface 在模型中指定这些字段。

幸运的是,类自己的实现会覆盖特征的实现,因此理论上您可以在模型上保留特征,而只需覆盖 getAuthPassword 方法。

也就是说,该特征(以及值得提醒的特征)确实假设您在所有地方都使用英文名称,因此您可能值得完全删除这两个特征(UserTraitRemindableTrait)并编写自己的接口方法的版本,因为如果/当您使用“记住我”或“忘记密码”时,您可能会看到同样的事情发生。


编辑:初步建议:

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;
    }
}

但是,正如我所说,如果您继续使用荷兰语列名,那么您最好完全放弃特征并自己实现方法:

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface
{
    /**
     * 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');

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->wachtwoord;
    }

    /**
     * Get the token value for the "remember me" session.
     *
     * @return string
     */
    public function getRememberToken()
    {
        return $this->aandenken_onthouden; // okay I made this up based on Google translate and some guesswork, use your own version!
    }

    /**
     * Set the token value for the "remember me" session.
     *
     * @param  string  $value
     * @return void
     */
    public function setRememberToken($value)
    {
        $this->aandenken_onthouden = $value;
    }

    /**
     * Get the column name for the "remember me" token.
     *
     * @return string
     */
    public function getRememberTokenName()
    {
        return 'aandenken_onthouden';
    }

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

【讨论】:

  • 感谢您的回答。我已将 getAuthPassword 添加到我的模型中,但没有成功。对于这个解决方案,我需要在我的代码中进行哪些更改?
  • @JelleP 我已将我的想法添加到答案中。请原谅我完全缺乏荷兰语知识,而是选择编造文字!你好!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-17
  • 2014-05-29
  • 2014-07-05
  • 2016-04-03
  • 2018-01-21
相关资源
最近更新 更多