針對前臺註冊登入的MD5加密方式,不影響其他後臺等hash加密方式。
具體步驟如下
1.在app目錄下,新建一個檔案Libraries,在Libraries目錄下新建一個MD5.php檔案,裡面程式碼如下:
<?php
namespace App\Libraries;
use Illuminate\Contracts\Hashing\Hasher;
class MD5 implements Hasher
{
/**
* Hash the given value.
*
* @param string $value
*
* @return array $options
* @return string
*/
public function make($value, array $options = [])
{
return 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 = [])
{
if(empty($hashedValue)){
return true;
}
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 = [])
{
return false;
}
}
2.在Providers檔案下面新建一個檔案 MD5ServiceProvider.php,裡面程式碼如下:
<?php
namespace App\Providers;
use Illuminate\Auth\EloquentUserProvider;
class MD5ServiceProvider extends EloquentUserProvider
{
//繼承EloquentUserProvider類,呼叫父類的建構函式
public function __construct($hasher, $model)
{
parent::__construct($hasher, $model);
}
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
3.在AuthServiceProvider.php檔案裡boot方法裡新增如下程式碼
Auth::provider('MD5', function ($app) {
$model = config('auth.providers.users.model');
return new MD5ServiceProvider(new MD5, $model);
});
頭部需要引用命名
use App\Libraries\MD5;
use Auth;
4.修改config/auth.php裡的providers,修改程式碼如下。
'providers' => [
'users' => [
'driver' => 'MD5',//'driver' => 'eloquent',//eloquent預設加密碼方式
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
5.修改註冊create
修改app/Http/Controllers/Auth/RegisterController.php裡的create,修改程式碼如下
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => md5($data['password']),
]);
}
到此就修改完成了,這樣註冊登入都可以使用md5去加密驗證。
轉載,做了些優化處理增加了註冊時加入MD5
本作品採用《CC 協議》,轉載必須註明作者和本文連結