我正在从 Laravel 4 迁移到 5.7,但我的自定义身份验证提供程序遇到了问题。我遵循了各种演练(例如1、2、3)以及相当多的谷歌搜索。
我尝试通过以下方式使其工作:
设置守卫和提供者并链接到我的目标模型。
'defaults' => [
'guard' => 'custom_auth_guard',
'passwords' => 'users',
],
'guards' => [
'custom_auth_guard' => [
'driver' => 'session',
'provider' => 'custom_auth_provider',
],
],
'providers' => [
'custom_auth_provider' => [
'driver' => 'custom',
'model' => App\UserAccount::class,
],
],
Run Code Online (Sandbox Code Playgroud)
注册上述提供程序中定义的驱动程序。为了方便起见,我搭载了 AuthServiceProvider
...
public function boot()
{
$this->registerPolicies();
\Auth::provider('custom',function() {
return new App\Auth\CustomUserProvider;
});
}
...
Run Code Online (Sandbox Code Playgroud)
创建了我的自定义提供程序,其中包含retrieveByCredentials等。我已经用一些 die() 替换了逻辑,以验证它是否在这里实现。在 Laravel 4 中,它曾经转到 validateCredentials()。
class CustomUserProvider implements UserProviderInterface {
public function __construct()
{
die('__construct');
}
public function retrieveByID($identifier)
{
die('retrieveByID');
}
public function retrieveByCredentials(array $credentials)
{
die('retrieveByCredentials');
}
public function validateCredentials(\Illuminate\Auth\UserInterface $user, array $credentials)
{
die('validateCredentials');
}
Run Code Online (Sandbox Code Playgroud)
作为参考,App/UserAccount 看起来像这样
class UserAccount extends Authenticatable
{
use Notifiable;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'public.user_account';
// no updated_at, created_at
public $timestamps = false;
private $_roles = [];
private $_permissions = [];
}
Run Code Online (Sandbox Code Playgroud)
最后,我通过我的控制器调用它。
if(\Auth::attempt($credentials){
return \Redirect::intended('/dashboard');
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过直接打电话给警卫
if(\Auth::guard('custom_auth_guard')->attempt($credentials){
return \Redirect::intended('/dashboard');
}
Run Code Online (Sandbox Code Playgroud)
这会导致以下错误:"Auth guard [custom_auth_guard] is not defined."
我尝试了一些其他命令以确保不存在缓存问题:
composer update
php artisan cache:clear
Run Code Online (Sandbox Code Playgroud)
结果:当我调用 Auth::attempt($credentials) 时,Laravel 尝试在 users 表上运行查询。预期的结果是它将命中 CustomUserProvider 中的 die() 之一...或者至少尝试查询模型中定义的 public.user_account 。
我已经搞乱这个有一段时间了,我一定错过了一些简单的东西......希望在 Laravel 5 中有更多经验的人能够看到我做错了什么。
提前致谢!!
设法解决了。有几个小问题,但主要的一个是我试图利用 AuthServiceProvider 而不是我自己的提供商。以下是我为让自定义身份验证提供程序在 Laravel 5.7 中工作所做的工作
在 config.auth.php 中设置提供程序。
'providers' => [
'user' => [
'driver' => 'eloquent',
'model' => \UserAccount::class,
],
],
Run Code Online (Sandbox Code Playgroud)
在 app/providers/ 中创建一个新的提供程序。这会将上面列出的提供商与正确的用户提供商代码链接起来。
namespace App\Providers;
use Auth;
use App\Auth\CustomUserProvider;
use Illuminate\Support\ServiceProvider;
class CustomAuthProvider extends ServiceProvider
{
public function register()
{
//
}
public function boot()
{
Auth::provider('eloquent',function()
{
return new CustomUserProvider(new \UserAccount());
});
}
}
Run Code Online (Sandbox Code Playgroud)
在 app/auth/ 中创建了我的自定义提供程序。这是验证用户的逻辑,并替换了 laravel 的 auth 函数。我在这里遇到一个问题,它正在验证但未填充用户对象。我最初进行了一个测试,看看该对象是否为空,如果是,则填充...但是它总是填充有一个空对象。删除测试允许我调用 Auth::user() 函数。
namespace App\Auth;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
use Illuminate\Auth\EloquentUserProvider;
class CustomUserProvider implements EloquentUserProvider{
public function __construct()
{
$this->user = $user;
}
public function retrieveByID($identifier)
{
$this->user = \UserAccount::find($identifier);
return $this->user;
}
public function retrieveByCredentials(array $credentials)
{
// find user by username
$user = \UserAccount::where('name', $credentials['username'])->first();
// validate
return $user;
}
public function validateCredentials(\Illuminate\Auth\UserInterface $user, array $credentials)
{
//logic to validate user
}
Run Code Online (Sandbox Code Playgroud)
更新的应用程序/模型/用户帐户看起来像这样
use Illuminate\Foundation\Auth\User as Authenticatable;
class UserAccount extends Authenticatable
{
protected $table = 'public.user_account';
// no updated_at, created_at
public $timestamps = false;
private $_roles = [];
private $_permissions = [];
}
Run Code Online (Sandbox Code Playgroud)
就是这样。我现在可以通过以下调用进行验证
if(\Auth::attempt($credentials){
return \Redirect::intended('/dashboard');
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10429 次 |
| 最近记录: |