laravel 由于很多数据是导入的,采用的是md5加密密码,但是laravel 默认的是使用bcrypt加密,所以需要更改,需要重写Auth中的attempt方法中的密码加密方法。
$request_params=$request->only(['username','password']); /*attempt默认使用bcrypt加密*/ if(Auth::guard('chatService')->attempt($request_params)) { return response()->json(['msg'=>'登陆成功','code'=>200]); }else { return response()->json(['msg'=>'登陆失败','code'=>400]); }
第一步:在vendor\laravel\framework\src\Illuminate\Hashing 新建Md5Hasher文件写入以下内容、
<?php namespace Illuminate\Hashing; use RuntimeException; use Illuminate\Contracts\Hashing\Hasher as HasherContract; /*增加MD5加密*/ class Md5Hasher implements HasherContract { public function check($value, $hashedValue, array $options = []) { return $this->make($value) === $hashedValue; } public function needsRehash($hashedValue, array $options = []) { return false; } public function make($value, array $options = []) { $value = env('SALT', '').$value; return md5($value); } }
第二步:
1)建立 MD5HashServiceProvider
php artisan make:provider MD5HashServiceProvider
2)写入以下内容:
<?php namespace App\Providers; use Illuminate\Hashing\Md5Hasher; use Illuminate\Support\ServiceProvider; class MD5HashServiceProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void */ public function boot() { $this->app->singleton('hash',function () { return new Md5Hasher(); }); } /** * Register the application services. * * @return void */ public function register() { } public function provides() { return ['hash']; // TODO: Change the autogenerated stub } }
3)更改config/app.php
'providers' => [ /* * Laravel Framework Service Providers... 将原来的 Illuminate\Hashing\HashServiceProvider::class更改 */ App\Providers\MD5HashServiceProvider::class,
参考:https://learnku.com/articles/5963/toggle-laravel-login-default-bcrypt-encryption-validation