虽然不推荐使用明文密码,但是我还是发一篇教程
使用的方法还是自定义Hash,只不过我们在处理中不进行hash运算即可,参考https://onlyke.com/html/829.html
首先,新建自定义的NonHasher
<?php namespace App\Libraries\Hashing; use Illuminate\Contracts\Hashing\Hasher as HasherContract; class NonHasher implements HasherContract{ /** * Hash the given value. * * @param string $value * @param array $options * @return string */ public function make($value, array $options = []){ return $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 (strlen($hashedValue) === 0) { return false; } return strcmp($value,$hashedValue) == 0; } /** * 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 = []){ if (strlen($hashedValue) === 0) { return false; } return $hashedValue; } }
然后,在AuthServiceProvider中注入
#app\Providers\AuthServiceProvider.php namespace App\Providers; use Auth; use Illuminate\Auth\EloquentUserProvider; use App\Libraries\Hashing\NonHasher; use Illuminate\Support\Facades\Gate; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; class AuthServiceProvider extends ServiceProvider { /** * The policy mappings for the application. * * @var array */ protected $policies = [ 'App\Model' => 'App\Policies\ModelPolicy', ]; /** * Register any authentication / authorization services. * * @return void */ public function boot() { $this->registerPolicies(); //使用自定义的Hash来处理密码 Auth::provider('non', function($app, array $config) { return new EloquentUserProvider(new NonHasher,$config['model']); }); } }
当然,最后别忘了设置config\auth.php下面的providers中driver为你上面在Auth::provider中声明的名字
/* |-------------------------------------------------------------------------- | User Providers |-------------------------------------------------------------------------- | | All authentication drivers have a user provider. This defines how the | users are actually retrieved out of your database or other storage | mechanisms used by this application to persist your user's data. | | If you have multiple user tables or models you may configure multiple | sources which represent each model / table. These sources may then | be assigned to any extra authentication guards you have defined. | | Supported: "database", "eloquent" | */ 'providers' => [ 'users' => [ 'driver' => 'non', 'model' => App\User::class, ], // 'users' => [ // 'driver' => 'database', // 'table' => 'users', // ], ],