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

Chr*_*ian 8 php laravel laravel-4

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

如何覆盖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'));
});
Run Code Online (Sandbox Code Playgroud)

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

Chr*_*ian 15

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

库\ CustomHasherServiceProvider.php

use Illuminate\Support\ServiceProvider;

class CustomHasherServiceProvider extends ServiceProvider {

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

}
Run Code Online (Sandbox Code Playgroud)

库\ 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);
}

}
Run Code Online (Sandbox Code Playgroud)

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

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

  • 你真的只是将'CustomHasherServiceProvider'放在providers数组中吗?我得到了 - 找不到Class'CustomHasherServiceProvider' (2认同)