gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 12:27:06 30 4
gpt4 key购买 nike

我的数据库中有一个包含用户的表。他们的密码是用我自己的自定义哈希函数生成的。

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

这是我一直在努力做的事情:

    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:警告:isset 中的非法偏移类型或 G:\Dropbox\Workspaces\www\video\vendor\laravel\framework\src\Illuminate\Foundation\Application.php 第 352 行

最佳答案

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

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 的提供程序数组中用“CustomHasherServiceProvider”替换了“Illuminate\Hashing\HashServiceProvider”

并在 composer.json 中添加“app/libraries”以自动加载类映射

关于php - 覆盖 laravel 4 的身份验证方法以使用自定义哈希函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15306196/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com