gpt4 book ai didi

php - Laravel 播种机中的密码字段没有散列

转载 作者:行者123 更新时间:2023-12-03 23:29:05 27 4
gpt4 key购买 nike

我最近从 Laravel 开发人员那里继承了一个项目来查看。不幸的是,当我迁移和播种用户表时,密码加密不起作用,如下所示:

public function run()
{
DB::table('users')->insert([
'email' => 'admin@site.co.uk',
'first_name' => 'Site',
'last_name' => 'Admin',
'username' => 'admin',
'password' => 'localhostPassword'
]);
}

当我跑 php artisan migrate --seed密码字段是上面的字符串文字,当我尝试登录时,它告诉我我的密码凭据不正确。

由于我不是 Artisan Laravel 开发人员,我不确定从哪里开始,但我希望密码字段像这样散列 $2y$10$u/FcKFPKsgRs8whJZ6ODAO90qllmGjqROnkmuQnxcpynG6WaIbX8e ,这是我在当前代码库中使用注册表时生成的内容。

最佳答案

您需要在存储之前对其进行散列:

use Illuminate\Support\Facades\Hash; // <-- import it at the top

//

public function run()
{
DB::table('users')->insert([
'email' => 'admin@site.co.uk',
'first_name' => 'Site',
'last_name' => 'Admin',
'username' => 'admin',
'password' => Hash::make('localhostPassword') // <---- check this
]);
}
注意:另一种方法是使用 bcrypt()助手而不是 Hash::make()方法。
the documentation关于这方面:

Basic Usage

You may hash a password by calling the make method on the Hashfacade:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Http\Controllers\Controller;

class UpdatePasswordController extends Controller
{
/**
* Update the password for the user.
*
* @param Request $request
* @return Response
*/
public function update(Request $request)
{
// Validate the new password length...

$request->user()->fill([
'password' => Hash::make($request->newPassword)
])->save();
}
}

关于php - Laravel 播种机中的密码字段没有散列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53959667/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com