php - 如何更改/自定义 Laravel 4 和 Laravel 5 用户身份验证的密码字段名称

标签 php authentication laravel laravel-4 laravel-5

我想在使用 Laravel 身份验证时更改数据库中的密码字段。我想要我的专栏 users表的名称为 passwd而不是password 。我尝试运行这样的东西:

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

但它不起作用。

我还尝试添加User对以下函数建模:

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

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

最佳答案

信息

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

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

默认情况下,如果将数组传递给 attempt (可能还有其他身份验证功能,例如 validateonce )如果您这样做:

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

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

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

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

但是如果你只是使用:

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 model ( 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. 现在您可以运行 /db网址例如 http://localhost/yourprojectname/db只是为了创建用户表。
  3. 现在您可以运行 /网址例如 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文件

关于php - 如何更改/自定义 Laravel 4 和 Laravel 5 用户身份验证的密码字段名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26073309/

相关文章:

php - Laravel 路由不更新

laravel addSelectRaw() - 如何在 addSelect() 中绑定(bind)变量?

php - Composer 无法识别 PHP 7

php - 如何在 laravel 5 中为谷歌分析添加谷歌 PHP API

php - 不含 FOSUserBundle 的 HWIOAuthBundle

asp.net - 托管 WCF 应用程序 IIS 身份验证

php - 使用 PDO 设置 LIMIT 时忽略数据库重复项

php - 带有下拉列表和预选选项的购物车

c# - 如何验证客户端应用程序以信任从其发送的消息

laravel - 在 Laravel-Excel 3.0 中设置事件表