更改 Laravel 5.4 密码加密和表列名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42704782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:31:44  来源:igfitidea点击:

Changing Laravel 5.4 password encryption and table column names

phplaravelencryptionlaravel-5laravel-5.4

提问by dev

I am trying to integrate the auth in laravel 5.4 within an existing database where the user and password fields have other names (memberid, passwordnew_enc). With the bellow changes and forcing the createfunction in RegisterControllerto use MD5 I managed to make the registration work. It also logins fine after registration. However the actual login form returns:

我正在尝试将 laravel 5.4 中的身份验证集成到现有数据库中,其中用户和密码字段具有其他名称 ( memberid, passwordnew_enc)。通过波纹管更改并强制create函数RegisterController使用 MD5,我设法使注册工作。注册后也能正常登录。但是实际的登录表单返回:

These credentials do not match our records.

这些凭据与我们的记录不符。

So far I have changed the User.php

到目前为止,我已经改变了 User.php

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

and

public function setPasswordAttribute($value)
{
    $this->attributes['password'] = md5($value);
}

Also on LoginController.php

也在 LoginController.php

public function username()
{
    return 'memberid';
}

Did I miss something ?

我错过了什么 ?

I only need to change the two column names to fit and the password encryption from bcrypt to md5

我只需要更改两列名称以适应并将密码加密从 bcrypt 更改为 md5

回答by upful

I would make custom user provider php artisan make:provider CustomUserProvider:

我会让自定义用户提供程序php artisan make:provider CustomUserProvider

<?php

namespace App\Providers;

use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;

class CustomUserProvider extends EloquentUserProvider {

    /**
    * Validate a user against the given credentials.
    *
    * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
    * @param  array  $credentials
    * @return bool
    */
    public function validateCredentials(UserContract $user, array $credentials)
    {
        $plain = $credentials['password']; // will depend on the name of the input on the login form
        $hashedValue = $user->getAuthPassword();

        if ($this->hasher->needsRehash($hashedValue) && $hashedValue === md5($plain)) {
            $user->passwordnew_enc = bcrypt($plain);
            $user->save();
        }

        return $this->hasher->check($plain, $user->getAuthPassword());
    }

}

This way if the password exists using md5 it will allow it to work once and then rehash it.

这样,如果使用 md5 存在密码,它将允许它工作一次,然后重新散列它。



You will register the CustomUserProviderin App\Providers\AuthServiceProvider boot()as follows:

您将注册CustomUserProviderApp\Providers\AuthServiceProvider boot()如下:

$this->app['auth']->provider('custom', function ($app, array $config) {
            $model = $app['config']['auth.providers.users.model'];
            return new CustomUserProvider($app['hash'], $model);
        });


Edit your config/auth.php

编辑你的 config/auth.php

'providers' => [
        'users' => [
            'driver' => 'custom',
            'model' => App\User::class,
        ],
],


You will also need to add the following as mentioned previously...

如前所述,您还需要添加以下内容...

app\Http\Controllers\Auth\LoginController.php

public function username()
{
    return 'memberid';
}


app\User.php

public function getAuthIdentifierName()
{
    return 'memberid';
}

public function getAuthIdentifier()
{
    return $this->memberid;
}

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

回答by dev

Alright I got it

好的,我知道了

app\User.php

app\User.php

public function setPasswordAttribute($value)
{
    $this->attributes['password'] = md5($value);
}

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

public function getAuthIdentifierName()
{
    return 'memberid';
}


app\Http\Controllers\Auth\LoginController.php

app\Http\Controllers\Auth\LoginController.php

public function username()
{
    return 'memb___id';
}


config\app.php

config\app.php

    // Illuminate\Hashing\HashServiceProvider::class,
    App\Providers\MD5HashServiceProvider::class,


app\Providers\MD5HashServiceProvider.php

app\Providers\MD5HashServiceProvider.php

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class MD5HashServiceProvider extends ServiceProvider
{
    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = true;
    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('hash', function () {
            return new \MD5Hasher;
        });
    }
    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return ['hash'];
    }
}


lib\MD5Hasher\MD5Hasher.php

lib\MD5Hasher\MD5Hasher.php

<?php
class MD5Hasher implements Illuminate\Contracts\Hashing\Hasher
{
    /**
     * Hash the given value.
     *
     * @param  string  $value
     * @return array   $options
     * @return string
     */
    public function make($value, array $options = array())
    {
        return md5($value); //hash('md5', $value);
    }
    /**
     * Check the given plain value against a hash.
     *
     * @param  string  $value
     * @param  string  $hashedValue
     * @param  array   $options
     * @return bool
     */
    public function check($value, $hashedValue, array $options = array())
    {
        return $this->make($value) === $hashedValue;
    }
    /**
     * Check if the given hash has been hashed using the given options.
     *
     * @param  string  $hashedValue
     * @param  array   $options
     * @return bool
     */
    public function needsRehash($hashedValue, array $options = array())
    {
        return false;
    }
}


composer.json

composer.json

...
"autoload": {
    "classmap": [
        ...
        "app/Lib"
    ],
 ...

回答by TimFelix

upful's code worked for me (in Laravel 5.4)

upful 的代码对我有用(在 Laravel 5.4 中)

But I needed to add:

但我需要补充:

use Illuminate\Contracts\Auth\Authenticatable as UserContract;

in the CustomUserProviderclass.

CustomUserProvider课堂上。