我正在尝试将 laravel 5.4 中的身份验证集成到现有数据库中,其中用户和密码字段具有其他名称(memberid
、passwordnew_enc
)。通过以下更改并强制 RegisterController
中的 create
函数使用 MD5,我成功地完成了注册。注册后也可以正常登录。然而,实际的登录表单返回:
These credentials do not match our records.
到目前为止,我已经更改了 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
最佳答案
我会制作自定义用户提供程序 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());
}
}
这样,如果使用 md5 的密码存在,它将允许它工作一次,然后重新散列。
您将在 App\Providers\AuthServiceProvider boot()
中注册 CustomUserProvider
,如下所示:
$this->app['auth']->provider('custom', function ($app, array $config) {
$model = $app['config']['auth.providers.users.model'];
return new CustomUserProvider($app['hash'], $model);
});
编辑你的config/auth.php
'providers' => [
'users' => [
'driver' => 'custom',
'model' => App\User::class,
],
],
如前所述,您还需要添加以下内容...
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;
}
关于php - 更改 Laravel 5.4 密码加密和表列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42704782/