简体   繁体   English

Laravel 5.4更改密码加密方法

[英]Laravel 5.4 change password crypting method

How can I change the password crypting algorithm for the auth in Laravel. 如何更改Laravel中auth的密码加密算法。 I see that in the register controller there is this function: 我看到在寄存器控制器中有此功能:

protected function create(array $data)
    {
        return User::create([
            'username' => $data['username'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }

and I can change the bcrypt function here but how to make the login check the password with my custom hashing algorithm that I will make? 并且我可以在此处更改bcrypt函数,但是如何使用我将要使用的自定义哈希算法使登录名检查密码?

You need to create your own HashServiceProvider (its a service provider) so use artisan to create your own (you may call it same as original) 您需要创建自己的HashServiceProvider (它是服务提供商),所以请使用artisan创建自己的(您可以将其称为原始)

$ artisan make:provider HashServiceProvider

Now copy most of the body of stock HashServiceProvider (the stock one can be found in \\Illuminate\\Hashing\\HashServiceProvider.php ) and fix namespaces and imports. 现在,复制库存HashServiceProvider的大部分内容(可以在\\ Illuminate \\ Hashing \\ HashServiceProvider.php中找到该库存),并修复名称空间和导入。 Now go to config/app.php and find HashServiceProvider and change stock HashServiceProvider for your own service provider, given that you did exactly as I guided you should notice no change what so ever. 现在转到config / app.php并找到HashServiceProvider并为您自己的服务提供商更改库存HashServiceProvider,因为您完全按照我的指导进行了操作,因此您应该注意到没有任何变化。

Now you have to create (implement) your own hashing functions. 现在,您必须创建(实现)自己的哈希函数。 You have two options: 您有两种选择:

A. extend the original BcryptHasher A.扩展原始的BcryptHasher

<?php
namespace Your\Namespaced\Hasher;
use Illuminate\Hashing\BcryptHasher as OriginalHasher;
class BcryptHasher extends OriginalHasher
{...} // and change what you need

example of this method can be found in Kyslik/django-bcrypt repository 该方法的示例可以在Kyslik / django-bcrypt存储库中找到

B. create new class and implement Hasher interface B.创建新类并实现Hasher接口

<?php
namespace Your\Namespaced\Hasher;
use Illuminate\Contracts\Hashing\Hasher;
class MyHasher implements Hasher {...} // IDE should scream at you with methods you need to implement

Finally in your service provider (HashServiceProvider) change register() method to return your implementation of hashing. 最后,在服务提供者(HashServiceProvider)中,更改register()方法以返回哈希的实现。

Do a $ composer dump-autoload and you may enjoy your own shiny hashing. 进行$ composer dump-autoload ,您可能会喜欢自己的闪亮哈希。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM