laravel7修改attempt中的默认的加密算法

laravel中默认的加密算法是bcrypt ,为了满足项目需求,将其改为SHA1加密算法

首先要分析,其默认算法

Auth::attempt(['email'=>$request->email, 'password'=>$request->password]);

跟踪查找attempt,在_ide_helper.php中

public static function attempt($credentials = [], $remember = false)
{
        /** @var \Illuminate\Auth\SessionGuard $instance */
        return $instance->attempt($credentials, $remember);
}

继续跟踪:在Session_Guard.php中发现其调用位置

public function attempt(array $credentials = [], $remember = false)
    {
        $this->fireAttemptEvent($credentials, $remember);

        $this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials);

        // If an implementation of UserInterface was returned, we'll ask the provider
        // to validate the user against the given credentials, and if they are in
        // fact valid we'll log the users into the application and return true.
        if ($this->hasValidCredentials($user, $credentials)) {
            $this->login($user, $remember);

            return true;
        }

        // If the authentication attempt fails we will fire an event so that the user
        // may be notified of any suspicious attempts to access their account from
        // an unrecognized user. A developer may listen to this event as needed.
        $this->fireFailedEvent($user, $credentials);

        return false;
    }

接下来看在laravel中如何注册并使用这个attempt方法

从app\config\app.php中发现 

Illuminate\Hashing\HashServiceProvider::class,

这看起来就是加密算法  HashServiceProvider::class   哈希服务提供者

根据服务提供者进一步发现     HashManager

 

public function register()
    {
        $this->app->singleton('hash', function ($app) {
            return new HashManager($app);
        });

        $this->app->singleton('hash.driver', function ($app) {
            return $app['hash']->driver();
        });
    }

进入HashManager中直接看到他的加密算法为 Bcrypt

 

修改算法

(1)在APP\Helpers\Hashers中创建 SHA1Hasher.php,并将如下内容复制粘贴进

driver('sha1')->info($hashedValue);
    }

    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 = [])
    {

        return sha1($value);
    }

}

(2)新增SHA1算法服务提供者使用artisan命令

php artisan make:provider SHA1HashServiceProvider

将如下内容输入:

app->singleton('hash', function () {
            return new SHA1Hasher;
        });
    }

    public function provides()
    {
        return ['hash'];
    }
}

(3)在config\app.php中将

Illuminate\Hashing\HashServiceProvider::class,

替换为

App\Providers\SHA1HashServiceProvider::class,

就可以啦

你可能感兴趣的:(Laravel)