我的数据库中有一个用户表 . 他们的密码是使用我自己的自定义散列函数生成的 .
如何覆盖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 回答
这就是最终解决问题的方法:
libraries\CustomHasherServiceProvider.php
libraries\CustomHasher.php
然后我在app / config / app.php的providers数组中用'CustomHasherServiceProvider'替换'Illuminate \ Hashing \ HashServiceProvider'
并在composer.json中将“app / libraries”添加到autoload类映射中
@vFragosop在正确的道路上延伸
Auth
.有几种方法可以让猫皮肤变好,这就是我如何在不更换默认的
Hasher
类的情况下做到这一点:包含在_1167709中或任何地方:
创建并自动加载
CustomHasher
类(即app/libraries/CustomHasher.php
):而已 .
Warning: 我无法确保这是开箱即用的工具,可能会有一些问题在这里和那里 . 请记住,Laravel 4仍处于开发阶段 . 希望我能提供更准确的答案,但代码库仍然经历了许多变化,而不是所有内容都被正确记录 . 无论如何,你正在寻找这样的东西:
如果这不能为您提供足够的信息,您应该能够通过查看以下类来了解它:
EloquentUserProvider和DatabaseUserProvider
这些类是当前支持的身份验证驱动程序 . 他们应该指导您如何创建
CustomUserProvider
(或您真正喜欢的任何名称) .Manager
这是接受自定义驱动程序(包括AuthManager)的任何内容的基类 . 它提供了像在Laravel 3中一样注册它们的方法 .