- <?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' => [
- //会员MD5登录加密
- 'users' => [
- 'driver' => 'MD5',
- 'model' => App\User::class,
- ],
- // 'users' => [
- // 'driver' => 'eloquent',
- // 'model' => App\User::class,
- // ],
- // 'users' => [
- // 'driver' => 'database',
- // 'table' => 'users',
- // ],
- ],
到此就修改完成了,这样注册登录都可以使用md5去加密验证,如有不懂可以联系站长Q Q:634381967