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


How to change / Custom password field name for Laravel 4 and Laravel 5 user authentication

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

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

但它不起作用。

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

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

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

信息

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

事实上,password字段在某种程度上是用Laravel硬编码的(但不是很多人认为的那样),所以您不能在问题中传递数组。

默认情况下,如果您将数组传递给attempt(可能还有其他Auth函数,如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模型(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列,用户也已登录(用于身份验证的数据已作为字符串传递,没有任何形式,但在实际应用程序中,您当然会添加它们)。您可以再运行一次这个url,因为您看到用户仍然被记录,所以它按预期工作
  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文件中