【问题标题】:How to hash password with md5 instead of bcrypt in laravel 8?如何在 laravel 8 中使用 md5 而不是 bcrypt 对密码进行哈希处理?
【发布时间】:2021-05-23 07:58:29
【问题描述】:

我想在 laravel 8 中使用 md5 而不是 bcrypt 对密码进行哈希处理。 在我的登录控制器 LoginController.php 中:

protected function credentials(Request $request)
        {
            return ['user_name' => $request->{$this->username()}, 'password' => md5($request->password), 'actif' => 'Y'];
        }

在我的 UserController 我有:

public function store(Request $request)
    {        
        $this->validationRules($request);        
        $user = new User();       
        $user->password = md5("00000000");
        $user->actif = 'Y';
        $user->user_name = $request->input('user_name');
        $user->save();

        return redirect('/users');
    }

新用户使用 md5 保存在数据库中,但是当我尝试登录时,它给了我这个错误:

These credentials do not match with our records

【问题讨论】:

  • 我想知道,既然MD5现在已经相当破碎和不安全,你为什么还要坚持使用它?

标签: laravel eloquent laravel-7 laravel-8 laravel-datatables


【解决方案1】:

阅读资源代码后,您会发现 Laravel 在vendor/src/Illuminate/src/Auth/EloquentUserProvider 中验证用户的凭据

public function validateCredentials(UserContract $user, array $credentials)
{
    $plain = $credentials['password'];
    return $this->hasher->check($plain, $user->getAuthPassword());
}
  1. 所以,首先在你的Models/User.php 中添加一个getAuthPassword 函数
class User extends Authenticatable
{
    public function getAuthPassword()
    {
        return ['password' => $this->attributes['password']];
    }
} 
  1. 然后,添加自定义 SelfEloquentUserProvider 扩展自 vendor/src/Illuminate/src/Auth/EloquentUserProvider.php
namespace App\Libs;

use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Str;

class SelfEloquentUserProvider extends EloquentUserProvider
{
    /**
     * Validate a user against the given credentials.
     *
     * @param \Illuminate\Contracts\Auth\Authenticatable $user
     * @param array $credentials
     */
    public function validateCredentials(Authenticatable $user, array $credentials)
    {
        $plain = $credentials['password'];
        $authPassword = $user->getAuthPassword();
 
        return hash_equals(md5($plain), $authPassword['password']);
    }
}
  1. 然后,将您的SelfEloquentUserProvider 注册到App/Providers/AppServiceProvider
class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        \Auth::provider('self-eloquent', function ($app, $config) {
            return New \App\Libs\SelfEloquentUserProvider($app['hash'], $config['model']);
        });
    }
    ......
}
  1. 最后,在你的config/auth.php
'providers' => [
    'users' => [
        'driver' => 'self-eloquent',
        'model' => \App\User::class,
    ]
]

您可以通过这种方式轻松自定义自己的身份验证规则。

【讨论】:

  • 好的,我试试这个
  • 当我在 EloquentUserProvider 和 SelfEloquentUserProvider 的 validateCredentials() 中 dd('message') 时它不起作用,它仍然转到 EloquentUserProvider 而不是 SelfEloquentUserProvider 的 validateCredentials()。
【解决方案2】:

您需要编写 LOGIN 控制器。

在带有 AUTH 包的 Laravel 7 (Illuminate\Foundation\Auth\AuthenticatesUsers) 中,您可以访问:

  • Http > Controllers > Auth > LoginController.php

你必须用下面的方法重写 AuthenticatesUsers trait:

protected function attemptLogin(Request $request)
{
    // Your login logic
}

如果您使用其他包,方法会有所不同,如果是这种情况,请查看您的包的文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    • 1970-01-01
    • 2012-05-06
    • 2011-03-04
    相关资源
    最近更新 更多