11

使用 Laravel 身份验证时,我想更改数据库中的密码字段。我希望表中的列users有 namepasswd而不是password. 我试图运行这样的东西:

Auth::attempt(array(
    'user_name' => 'admin',
    'passwd' => 'hardpass',
));

但它不起作用。

我还尝试在User模型中添加以下功能:

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

但它也没有任何改变。用户仍未通过身份验证。Laravel 是否可以更改数据库中的密码字段名称?

4

1 回答 1

56

信息

您可以轻松更改数据库中的所有其他字段并将它们用于身份验证。唯一的问题是password字段。

事实上password,字段在 Laravel 中以某种方式硬编码(但不是许多人认为的方式),因此您不能在传递问题时只传递数组。

默认情况下,如果您以这种方式将数组传递给attempt(可能还有其他 Auth 函数,如validateor once):

Auth::attempt(array(
    'user_name' => 'admin',
    'password' => 'hardpass',
));

默认的 Eloquent 驱动程序将运行以下查询:

select * from `users` where `user_name` = 'admin' limit 1;

从数据库中获取此数据后,它会将您提供的密码与创建的用户对象的密码属性进行比较。

但是,如果您只是使用:

Auth::attempt(array(
    'user_name' => 'admin',
    'passwd' => 'hardpass',
));

将运行以下查询:

select * from `users` where `user_name` = 'admin' and `passwd` = 'hardpass' limit 1;

并且不会在数据库中找到用户(在passwd您存储散列密码中)。这是因为 Eloquent 从查询中移除,password但使用任何其他数据来运行查询。此外,如果您尝试在这里使用'passwd' => Hash:make($data['password'])虽然会找到用户,但比较密码将不起作用。

解决方案

解决方法很简单。你需要Auth::attempt像这样运行:

Auth::attempt(array(
    'user_name' => 'admin',
    'password' => 'hardpass',
));

如您所见,您仍然password作为键传递(尽管此列不会在users表中退出),因为只有这样 Eloquent 驱动程序才会使用它来构建查询。

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

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

如您所见,您在这里使用了数据库中真正存在的列:passwd.

以这种方式使用它,您可以将带有密码的列命名为您想要的任何名称,并且您仍然可以使用默认的 Eloquent 驱动程序。

要测试的样本数据

我为它创建了非常简单的测试。

您只需将app/routes.php文件替换为以下内容:

Route::get('/', function () {

    if (Auth::check()) {
        echo "I'm logged in as " . Auth::user()->user_name . "<br />";
        echo "<a href='/logout'>Log out</a>";
    } else {
        echo "I'm NOT logged in<br />";


        Auth::attempt(array(
            'user_name' => 'admin',
            'password'  => 'hardpass',
        ));


        if (Auth::check()) {
            echo "Now I'm logged in as " . Auth::user()->user_name . "<br />";
            echo "<a href='/logout'>Log out</a>";
        } else {
            echo "I'm still NOT logged in<br />";
        }
    }


});

Route::get('/logout', function () {
    Auth::logout();
    return "You have been logged out";
});


Route::get('/db', function () {

    if (!Schema::hasTable('users')) {


        Schema::create('users', function ($table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->string('user_name', 60)->unique();
            $table->string('passwd', 256);
            $table->rememberToken();
            $table->timestamps();
        });

        DB::table('users')->insert(
            [
                [
                    'user_name' => 'admin',
                    'passwd'    => Hash::make('hardpass'),
                ]
            ]
        );
    }

    echo "Table users has been created";
});
  1. 创建空数据库并设置连接数据app/config/database.php
  2. 现在您可以运行/dburl 例如http://localhost/yourprojectname/db创建用户表。
  3. 现在您可以运行/url 例如http://localhost/yourprojectname/- 正如您看到的用户已登录,即使users在数据库中的表中您没有任何password列(用于身份验证的数据已作为字符串传递,没有任何形式,但当然在实际应用程序中您将添加它们) . 您可以再次运行此网址 - 正如您所看到的用户仍被记录,因此它按预期工作。
  4. 如果您点击Log out链接,您将被注销

Laravel 5 对上述内容的更改

该解决方案已在 Larave 4.2.9(如上)和 Laravel 5 中进行了测试。在 Laravel5 中,一切都一样,但您当然需要编辑不同路径中的文件:

  1. User模型在app/User.php文件中
  2. 路线在app/Http/routes.php文件中
  3. 数据库配置文件在config/database.php文件中
于 2014-09-27T09:41:23.890 回答