Laravel 4失败了Auth ::尝试使用Hash替换

时间:2013-07-14 14:27:45

标签: authentication hash laravel laravel-4

我正在尝试在Laravel4项目中进行身份验证,但它总是失败。

由于我只使用PHP 5.3.4,因此我下载了一个使用https://github.com/robclancy/laravel4-hashing中的SHA512的替换Hash包。

我已经有一个Users表(密码字段预先填充了SHA512哈希值,与另一个应用程序共享),我的登录身份验证方法是:

Route::post('login', function()
{
    $userdata = array(
                'UserName' => 'SteB', 
                'password' => '1234'
                );

    if (Auth::attempt($userdata, true))
    {
       return Redirect::action('HomeController@Profile');
    }                    
    else
    {
        return Redirect::to('/')->with('login_errors', true);
    }

});

我还使用http://hash.online-convert.com/sha512-generator

检查了SHA512哈希是否正确

这总是失败(默默地),任何想法为什么或如何调试它都将受到赞赏。

我的用户表是:

UserID int,Identity PK
UserName varchar(24)
FullName varchar(32)
EMail varchar(64)
密码varchar(256)

这是在现有的SQL Server 2000数据库中,我从数据库中获取未经身份验证的页面的用户信息,因此我知道数据库驱动程序和连接正常工作。

Laravel中的用户模型:

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 = 'Users';

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array('password');

//protected $fillable = array('UserID', 'UserName', 'FullName');

/**
 * 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()
{
            /*Now all lowercase to match $userdata as suggested on SO*/
    return $this->password;
}

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

更新

刚刚找到这篇文章:http://laravel.io/topic/20/hashing-in-laravel 看起来它没有按照我的预期做事,我认为是带有随机盐的base64编码哈希。

3 个答案:

答案 0 :(得分:2)

因此,您需要更改模型中的某些内容以及您尝试的密钥

class User extends Eloquent implements UserInterface
{
    public function getAuthPassword()
    {
        return $this->Password;
    }

    // leave other code the same
 }

在你的数组中你传递的更改如下。

$userdata = array( 'UserName' => 'SteB', 'password' => '1234');

getAuthPassword()告诉Auth库您的实际字段名,并将userdata中的键设置为全部小写password告诉Auth库散列该字段。

答案 1 :(得分:0)

我不知道您的数据库结构是什么样的,但是您的用户名密钥可能与数据库列不同吗?

也许这会奏效:

$userdata = array(
    'username' => 'SteB', 
    'password' => '1234'
);

干杯。

答案 2 :(得分:0)

编辑:你可以尝试所有这些,但我认为我在写完这篇文章之后就证明自己是错的。

这个问题与您的数据库列命名和Laravel中的限制有关。查看Laravel的auth组件,我找到了一些特别需要password或至少包含该确切短语的字符串的区域。因此,您的数据库需要使用password列,或者您需要扩展laravel以更改某些内容。

老实说,你应该只使用带有laravel的驼峰盒或蛇盒。当你使用它永远不会想到的东西时会出现像这样的问题,因为没有合理的理由可以使用其他任何东西。

如此简单的修复,使用更好的列名。

更难修复是将Auth组件扩展为允许Password字段。