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
