Laravel修改默认的auth模块为md5(password+salt)验证
首先声明:这里只是作为一个记录,实行拿来主义,懒得去记录那些分析源码的过程,不喜勿喷,可直接划走。
第一步:创建文件夹:app/Helpers/Hasher;
第二步:创建文件:
app/Helpers/Hasher/MD5Hash.php; app/Providers/MD5HashServiceProvider.php
第三步:修改文件:
1.MD5Hash.php:
namespace App\Helpers\Hasher;
use Illuminate\Contracts\Hashing\Hasher;
class MD5Hash implements Hasher
{
public function check($value, $hashedValue, array $options = []){
return $this->make($value.$hashedValue['salt']) == $hashedValue['password'];
}
public function needsRehash($hashedValue, array $options = [])
{
return false;
}
public function make($value, array $options = [])
{
return md5($value);
}
/**
* @param string $hashedValue
* @return array
*/
public function info($hashedValue)
{
// TODO: Implement info() method.
}
}
说明:check方法中的$hashvalue主要是根据自己的App/User.php中的getAuthPassword方法返回的内容。构造加密算法的主要是在make方法中实现。
2.MD5HashServiceProvider.php:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Helpers\Hasher\MD5Hash;
class MD5HashServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
$this->app->singleton('hash', function () {
return new MD5Hash;
});
}
public function provides()
{
// return parent::provides(); // TODO: Change the autogenerated stub
// return ['hash'];
}
}
说明:这个文件照搬就可以
3.app/User.php:
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'username', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
protected $table = 'rk_admin';
public function getAuthPassword()
{
return ['password'=>$this->attributes['password'], 'salt'=>$this->attributes['salt']];
}
}
说明:这里的getAuthPassword方法是返回需要的字段。需要添加自己的admin表。
4.config/app.php
// Illuminate\Hashing\HashServiceProvider::class, //修改为自己的md5
\App\Providers\MD5HashServiceProvider::class, //这里修改为自己的服务提供者
同理,其他类型的加密算法可以在md5hash.php中自己去自由发挥。