dev*_*dev 5 php encryption laravel laravel-5 laravel-5.4
我试图将laravel 5.4中的auth集成到现有数据库中,其中用户和密码字段具有其他名称(memberid,passwordnew_enc).随着波纹管的变化和强制create功能RegisterController使用MD5我设法使注册工作.注册后它也会正常登录.但是实际的登录表单返回:
这些凭据与我们的记录不符.
到目前为止我已经改变了 User.php
public function getAuthPassword()
{
return $this->passwordnew_enc;
}
Run Code Online (Sandbox Code Playgroud)
和
public function setPasswordAttribute($value)
{
$this->attributes['password'] = md5($value);
}
Run Code Online (Sandbox Code Playgroud)
也在 LoginController.php
public function username()
{
return 'memberid';
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么 ?
我只需要更改两个列名称以适应密码加密从bcrypt到md5
upf*_*ful 18
我会制作自定义用户提供商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());
}
}
Run Code Online (Sandbox Code Playgroud)
这样,如果使用md5存在密码,它将允许它工作一次然后重新散列它.
您将注册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);
});
Run Code Online (Sandbox Code Playgroud)
编辑你的 config/auth.php
'providers' => [
'users' => [
'driver' => 'custom',
'model' => App\User::class,
],
],
Run Code Online (Sandbox Code Playgroud)
如前所述,您还需要添加以下内容...
app\Http\Controllers\Auth\LoginController.php
public function username()
{
return 'memberid';
}
Run Code Online (Sandbox Code Playgroud)
app\User.php
public function getAuthIdentifierName()
{
return 'memberid';
}
public function getAuthIdentifier()
{
return $this->memberid;
}
public function getAuthPassword()
{
return $this->passwordnew_enc;
}
Run Code Online (Sandbox Code Playgroud)