首页 文章

覆盖laravel 4的身份验证方法以使用自定义哈希函数

提问于
浏览
8

我的数据库中有一个用户表 . 他们的密码是使用我自己的自定义散列函数生成的 .

如何覆盖laravel 4中的Authentication方法以使用我自己的哈希类?

这就是我一直在尝试做的事情:

class CustomUserProvider implements Illuminate\Auth\UserProviderInterface {


    public function retrieveByID($identifier)
    {
        return $this->createModel()->newQuery()->find($identifier);
    }

    public function retrieveByCredentials(array $credentials)
    {
        // First we will add each credential element to the query as a where clause.
        // Then we can execute the query and, if we found a user, return it in a
        // Eloquent User "model" that will be utilized by the Guard instances.
        $query = $this->createModel()->newQuery();

        foreach ($credentials as $key => $value)
        {
            if ( ! str_contains($key, 'password')) $query->where($key, $value);
        }

        return $query->first();
    }

    public function validateCredentials(Illuminate\Auth\UserInterface $user, array $credentials)
    {
        $plain = $credentials['password'];

        return $this->hasher->check($plain, $user->getAuthPassword());
    }

}

class CodeIgniter extends Illuminate\Auth\Guard {


}

App::bind('Illuminate\Auth\UserProviderInterface', 'CustomUserProvider');



Auth::extend('codeigniter', function()
{
    return new CodeIgniter( App::make('CustomUserProvider'), App::make('session'));
});

当我运行Auth :: attempt方法时,我收到此错误:ErrorException:Warning:isset中的非法偏移类型或G:\ Dropbox \ Workspaces \ www \ video \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Application中为空.php第352行

3 回答

  • 15

    这就是最终解决问题的方法:

    libraries\CustomHasherServiceProvider.php

    use Illuminate\Support\ServiceProvider;
    
    class CustomHasherServiceProvider extends ServiceProvider {
    
        public function register()
        {
            $this->app->bind('hash', function()
            {
                return new CustomHasher;
            });
        }
    
    }
    

    libraries\CustomHasher.php

    class CustomHasher implements Illuminate\Hashing\HasherInterface {
    
    private $NUMBER_OF_ROUNDS = '$5$rounds=7331$';
    
    
    public function make($value, array $options = array())
    {
    
        $salt = uniqid();
        $hash = crypt($password, $this->NUMBER_OF_ROUNDS . $salt);
        return substr($hash, 15);
    }
    
    public function check($value, $hashedValue, array $options = array())
    {
        return $this->NUMBER_OF_ROUNDS . $hashedValue === crypt($value, $this->NUMBER_OF_ROUNDS . $hashedValue);
    }
    
    }
    

    然后我在app / config / app.php的providers数组中用'CustomHasherServiceProvider'替换'Illuminate \ Hashing \ HashServiceProvider'

    并在composer.json中将“app / libraries”添加到autoload类映射中

  • 4

    @vFragosop在正确的道路上延伸 Auth .

    有几种方法可以让猫皮肤变好,这就是我如何在不更换默认的 Hasher 类的情况下做到这一点:

    包含在_1167709中或任何地方:

    use Illuminate\Auth\Guard;
    Auth::extend("eloquent", function() {
        return new Guard(
            new \Illuminate\Auth\EloquentUserProvider(new CustomHasher(), "User"),  
            App::make('session.store')
        );
    });
    

    创建并自动加载 CustomHasher 类(即 app/libraries/CustomHasher.php ):

    class CustomHasher extends Illuminate\Hashing\BcryptHasher {
        public function make($value, array $options = array())
        {
            ...
        }
        public function check($value, $hashedValue, array $options = array())
        {
            ...
        }
    }
    

    而已 .

  • 1

    Warning: 我无法确保这是开箱即用的工具,可能会有一些问题在这里和那里 . 请记住,Laravel 4仍处于开发阶段 . 希望我能提供更准确的答案,但代码库仍然经历了许多变化,而不是所有内容都被正确记录 . 无论如何,你正在寻找这样的东西:

    // on config/auth.php
    'driver' => 'custom'
    
    // on start/global.php
    Auth::extend('custom', function() {
        // CustomUserProvider is your custom driver and should
        // implement Illuminate\Auth\UserProviderInterface;
        return new Guard(new CustomUserProvider, App::make('session'));
    });
    

    如果这不能为您提供足够的信息,您应该能够通过查看以下类来了解它:

    EloquentUserProviderDatabaseUserProvider
    这些类是当前支持的身份验证驱动程序 . 他们应该指导您如何创建 CustomUserProvider (或您真正喜欢的任何名称) .

    Manager
    这是接受自定义驱动程序(包括AuthManager)的任何内容的基类 . 它提供了像在Laravel 3中一样注册它们的方法 .

相关问题