哪个Hash函数laravel的Auth正在使用?

时间:2014-07-31 13:18:58

标签: authentication hash laravel-4

我正在尝试使用Laravel的内置身份验证,但它无法正常工作。如果我在注册过程中散列密码,则它与登录过程中的散列不匹配,因为Laravel的auth生成的散列与Hash:make()完全不同。

我做了一个测试路线和功能,让一切都清楚 (我尝试使用Auth :: attempt()函数和Hash :: make(),而不使用Hash :: make(),如果我传递Hashed密码,它会生成一个完全不同的密码,如果我传递原始密码Auth甚至懒得去哈希):

Route::get('/test', function() {
    $email = rand(1, 1000) . "test@mail.com";
    $password = $email;
    $now = new DateTime();
    Felhasznalo::create(array(
        'teljesnev' => 'valami',
        'email' => $email,
        'jelszo' => Hash::make($password),
        'FelFelhasznaloiSzint_id' => 2,
        'created_at' => $now->getTimestamp(),
        'aktiv' => 1
    ));

    if (Auth::attempt(array(
                'email' => $email,
                'jelszo' => Hash:make($password)
            ))) {
        echo 'Working';
    } else {
        echo 'Not working';
    }
});

第一部分将此行生成到我的mysql服务器:

39test@mail.com, $2y$10$gkzQ2BEuDWN05RQ/OyBH8u4aKRqdL5zSIthUO4BUyEKcscgzRwZzG, valami ....etc

我故意错误地将密码字段的名称输入到jelszo1(在英语中表示password1),在尝试登录时,会出现sql错误。

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'jelszo1' in 'where clause' (SQL: select * from `Felhasznalo` where `email` = 39test@mail.com and `jelszo1` = y$XAnnnGNKrOQOxBiX2BnEPOq86Y9mh5h./xwUlfCTPpzW.jzzt3YiO limit 1) 

第一部分(在mysql中):

$2y$10$gkzQ2BEuDWN05RQ/OyBH8u4aKRqdL5zSIthUO4BUyEKcscgzRwZzG

第二部分(尝试登录时):

y$XAnnnGNKrOQOxBiX2BnEPOq86Y9mh5h./xwUlfCTPpzW.jzzt3YiO

和用户模型(名为Felhasznalo)

<?php

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

class Felhasznalo extends \Eloquent implements UserInterface, RemindableInterface {

    use UserTrait,
        RemindableTrait;

    protected $fillable = [
        'id', 'email', 'jelszo', 'teljesnev', 'jelszoreset',
        'hash', 'aktiv', 'ban', 'FelFelhasznaloiSzint_id',
        'remember_token'
    ];
    protected $guarded = [
    ];
    protected $hidden = [
        'jelszo', 'jelszoreset', 'hash'
    ];
    protected $table = 'Felhasznalo';
    public $timestamps = true;
    protected $softDelete = true;

    //----------------------------
    //----------------------------
    // RELATIONSHIP FUNCTIONS
    //----------------------------
    //----------------------------

    /**
     * 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->jelszo;
    }

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

    public function getRememberToken() {
        return $this->remember_token;
    }

    public function setRememberToken($value) {
        $this->remember_token = $value;
    }

    public function getRememberTokenName() {
        return 'remember_token';
    }

}

auth config。:

'model' => 'Felhasznalo',
'table' => 'Felhasznalo',

在attempt()中使用原始密码时出现SQL错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'jelszo1' in 'where clause' (SQL: select * from `Felhasznalo` where `email` = 832test@mail.com and `jelszo1` = 832test@mail.com limit 1) 

2 个答案:

答案 0 :(得分:0)

我刚检查过Auth代码,您的密码列必须被称为password。您无法使用jelszo

此外 - 您的登录代码不正确。你这样做:

if (Auth::attempt(array(
                'email' => $email,
                'password' => $password
            ))) {
        echo 'Working';
    } else {
        echo 'Not working';
    }

Auth :: attempt()将为您进行散列。

答案 1 :(得分:0)

可以在mnshankar.wordpress.com - Laravel Hash::make() explained上找到帖子标题中的问题答案(哪个Hash函数laravel的Auth正在使用?)。

以下是链接博客帖子的“如何?”部分中的第一句话:

  

在内部,Hash :: make()使用 bcrypt 函数和 Blowfish 算法加密。

此外,您可以在Laravel 5.4 docs - Hashing page上看到他们说:

  

Laravel Hash外观提供用于存储用户密码的安全 Bcrypt 哈希。

相关问题