【问题标题】:Hashed Password not matching - Laravel哈希密码不匹配 - Laravel
【发布时间】:2015-04-05 06:25:00
【问题描述】:

我使用的是 Laravel 4,我的数据库表/页面是使用旧版本的 Laravel 创建的。第 3 版。我相信。

我有一个用户登录系统,当将散列密码从视图传递到控制器时,它根本不匹配数据库的密码。

我的哈希码是:

数据库

$08$wqCWqMgG7SRIukdyNEbXX.kK5c.8BxqzGVJSaCC55eKndFjqrJqJG

表格

$2y$10$hJQsF7.KkuXw4GYb8vk1o.SZhdocP7e8SxcjvBWjtLzpJPBlX0f5q

我的 Laravel 控制器代码是:

public function postLogin()
    {

        $email = Input::get('email');
        $password = Hash::make(Input::get('Password'));

        dd($password);

        $credentials = array(
            'user_email' => Input::get('UserName'),
            'user_password' => Input::get('Password')
        );

        if(Auth::attempt($credentials))
        {
            return Redirect::to('dashboard')->with('message', 'You are now logged in!');
        }
        else
        {
            return Redirect::to('users/login')
                           ->with('message', 'Your username/password combination was incorrect')
                           ->withInput();
        }
    }

是不是因为旧版本的数据库不匹配?关于我可以检查/更改以匹配的任何建议。

干杯

【问题讨论】:

    标签: php laravel hash


    【解决方案1】:

    同一秘密的新哈希每次都会不同,因为在哈希时添加了随机盐。使用哈希检查秘密:

    Hash::check('secret', 'hash-of-secret');
    

    Auth::attempt 失败的原因是传递的凭据总是需要有一个password 密钥。 (即使您的数据库字段有不同的名称)

    $credentials = array(
        'user_email' => Input::get('UserName'),
        'password' => Input::get('Password')
    );
    

    然后确保你的User 模型实现了这个方法:

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

    【讨论】:

    • 太好了,我让“user_password”切换到“密码”完成了这项工作。谢谢!
    【解决方案2】:

    当您像这样比较它们时,哈希不会“匹配”。有一种盐会添加到所有哈希中。

    问题很可能是密码的数据库列名。如果是 user_passwords - 那么你必须在你的用户模型中设置它,否则它不会工作(Laravel 会假设它是 password 否则)

    所以您的 $credentials 必须使用 password 字段 - 而不是 user_password

    $credentials = array(
                'user_email' => Input::get('UserName'),
                'password' => Input::get('Password')
            );
    

    如果您的用户数据库在名为“密码”的列中有密码。然后你不需要做任何进一步的事情。但是,如果您的列名为“user_password” - 那么您必须修改您的 User 模型并向其中添加/修改以下函数:

    现在在用户模型(app/models/User.php)文件中你需要添加以下函数:

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

    【讨论】:

    • 哇,看起来我们同时写了几乎相同的答案;)
    猜你喜欢
    • 1970-01-01
    • 2013-05-19
    • 2012-07-05
    • 2016-08-27
    • 2015-05-11
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多