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]);
}
第一步:在vendorlaravelframeworksrcIlluminateHashing 新建Md5Hasher文件写入以下内容、
<?php
namespace IlluminateHashing;
use RuntimeException;
use IlluminateContractsHashingHasher 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 AppProviders;
use IlluminateHashingMd5Hasher;
use IlluminateSupportServiceProvider;
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...
将原来的 IlluminateHashingHashServiceProvider::class更改
*/
AppProvidersMD5HashServiceProvider::class,
参考:https://learnku.com/articles/5963/toggle-laravel-login-default-bcrypt-encryption-validation