更改Laravel 5.4密码加密和表列名称 [英] Changing Laravel 5.4 password encryption and table column names
问题描述
我正在尝试在现有数据库中的laravel 5.4中集成auth,其中用户和密码字段有其他名称( memberid
, passwordnew_enc
)。使用波纹管更改并强制创建
函数在 RegisterController
中使用MD5我设法使注册工作。注册后也登录。但是实际的登录表单返回:
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 create
function in RegisterController
to use MD5 I managed to make the registration work. It also logins fine after registration. However the actual login form returns:
这些凭据与我们的记录不符。
These credentials do not match our records.
到目前为止,我已经更改了 User.php
So far I have changed the User.php
public function getAuthPassword()
{
return $this->passwordnew_enc;
}
和
public function setPasswordAttribute($value)
{
$this->attributes['password'] = md5($value);
}
还在 LoginController.php
public function username()
{
return 'memberid';
}
我错过了什么?
我只需要更改两个列名称以适应从bcrypt到md5的密码加密
I only need to change the two column names to fit and the password encryption from bcrypt to md5
推荐答案
我会使自定义用户提供者 php artisan make:provider CustomUserProvider
:
I would make custom user provider php artisan make:provider CustomUserProvider
:
<?php
namespace App\Providers;
use Illuminate\Auth\EloquentUserProvider;
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());
}
}
md5它将允许它工作一次然后重新发布。
This way if the password exists using md5 it will allow it to work once and then rehash it.
您将注册 CustomUserProvider
在 App\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);
});
编辑您的 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;
}
这篇关于更改Laravel 5.4密码加密和表列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!