繁体   English   中英

将数据库中的普通密码转换为 Laravel 加密密码

[英]Converting plain password in database to Laravel encrypted password

我有一个名为“users”的表,其中包含来自用户的用户名和密码。

密码为纯文本。 现在我用 Laravel 6.0 和 Auth 创建了一个新站点。

因此,如果用户想登录我的网站,我需要将我的密码纯文本转换为加密的新密码。

如何从我的身份验证中获取“盐”以及从我的普通密码和“盐”中获取加密密码的工具。 原因是因为我在用户表中创建了一个新列,所以我想将使用查询加密的密码放在那里。

运行 change_command

在此处输入图片说明

您必须首先创建一个函数来将您的数据库密码更新为加密密码。

在 web.php 上有类似的东西,并在浏览器上访问 /password-updator

Route::get('/password_updator', function() {
 $allusers = \DB::table('users')->get();
 foreach($users as $user) {
  $user->password = bcrypt($user->password);
  $user->save();
}
});

确保在继续之前备份您的数据库!

或者,您首先在用户表上创建一个名为 password_hashed 的新列并将其更新以进行实验。

https://laravel.com/docs/5.4/helpers#method-bcrypt

Laravel Hash门面提供安全的 Bcrypt 和 Argon2 散列来存储用户密码。

$password = Hash::make('plain-text-password');

bcrypt函数使用 Bcrypt 散列给定的值。 你可以用它来替代Hash门面:

$password = bcrypt('plain-text-password');

如何从我的身份验证中获取“盐”以及从我的普通密码和“盐”中获取加密密码的工具。

根据散列验证密码

check方法允许您验证给定的纯文本字符串是否对应于给定的散列。

if (Hash::check('plain-text-password', $hashedPassword)) {
    // The passwords match...
}

更新

您可以使用命令或创建路由来更改现有客户的“纯文本”密码。

创建命令app/Console/Commands/ChangePassword.php

<?php

namespace App\Console\Commands;

use App\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;

class ChangePassword extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'change-password';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Plain-text password changer';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $users = User::get();

        foreach ($users as $user) {
            if (Hash::needsRehash($user->password)) {
                $user->password = Hash::make($user->password);
                $user->save();
            }
        }

        $this->info('Done..');
    }
}
用法 :
 php artisan change-password

运行命令后,您可以尝试通过Auth::routes()路由登录。


或手动验证用户

if (Auth::attempt($credentials)) { // Authentication passed... }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM