简体   繁体   English

Laravel 5.2自定义身份验证错误

[英]Laravel 5.2 Error In Custom Authentication

I am getting error while making custom authentication for my laravel 5.2 however this code works fine on my laravel 5.1 My config/auth.php file 我为我的laravel 5.2进行自定义身份验证时收到错误但是此代码在我的laravel 5.1 My config / auth.php文件中正常工作

'providers' => [
    'users' => [
        'driver' => 'custom',
        'model' => App\User::class,
    ],

    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],
],

My CustomUserProvider.php (Auth/CustomUserProvider) file 我的CustomUserProvider.php(Auth / CustomUserProvider)文件

    <?php namespace App\Auth;

use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;

class CustomUserProvider implements UserProvider {

    protected $model;

    public function __construct(UserContract $model)
    {
        $this->model = $model;
    }

    public function retrieveById($identifier)
    {

    }

    public function retrieveByToken($identifier, $token)
    {

    }

    public function updateRememberToken(UserContract $user, $token)
    {

    }

    public function retrieveByCredentials(array $credentials)
    {

    }

    public function validateCredentials(UserContract $user, array $credentials)
    {

    }

}

My CustomAuthProvider.php file 我的CustomAuthProvider.php文件

    <?php namespace App\Providers;

use App\User;
use Auth;
use App\Auth\CustomUserProvider;
use Illuminate\Support\ServiceProvider;

class CustomAuthProvider extends ServiceProvider {

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->app['auth']->extend('custom',function()
        {

            return new CustomUserProvider();
        });
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

}

Now this works fine in laravel 5.1 in 5.2 i am getting error like 现在这在laravel 5.1 in 5.2中运行良好我得到的错误就像

InvalidArgumentException in CreatesUserProviders.php line 40:
Authentication user provider [custom] is not defined.

The only point is to use 唯一的一点是使用

$this->app['auth']->provider(... 

instead of 代替

$this->app['auth']->extend(...

The last one is used in 5.1, the first one should be used in 5.2. 最后一个在5.1中使用,第一个应该在5.2中使用。

app/Models/User.php 应用/型号/ user.php的

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable {
protected $connection='conn';
protected $table='users-custom';

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'login', 'passwd'
];

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = [
    'passwd',
];

public function getAuthPassword(){
    //your passwor field name
    return $this->passwd;
}

public $timestamps = false;

} }

create app/Auth/CustomUserProvider.php 创建app / Auth / CustomUserProvider.php

 namespace App\Auth;

use Illuminate\Support\Str;

use Illuminate\Contracts\Auth\Authenticatable as UserContract;
use Illuminate\Contracts\Auth\UserProvider;

/**
 * Description of CustomUserProvider
 *
 */

class CustomUserProvider implements UserProvider {

/**
 * The hasher implementation.
 *
 * @var \Illuminate\Contracts\Hashing\Hasher
 */
protected $hasher;

/**
 * The Eloquent user model.
 *
 * @var string
 */
protected $model;

/**
 * Create a new database user provider.
 *
 * @param  \Illuminate\Contracts\Hashing\Hasher  $hasher
 * @param  string  $model class name of model
 * @return void
 */
public function __construct($model) {
    $this->model = $model;        
}

/**
 * Retrieve a user by their unique identifier.
 *
 * @param  mixed  $identifier
 * @return \Illuminate\Contracts\Auth\Authenticatable|null
 */
public function retrieveById($identifier) {
    return $this->createModel()->newQuery()->find($identifier);
}

/**
 * Retrieve a user by their unique identifier and "remember me" token.
 *
 * @param  mixed  $identifier
 * @param  string  $token
 * @return \Illuminate\Contracts\Auth\Authenticatable|null
 */
public function retrieveByToken($identifier, $token) {
    $model = $this->createModel();

    return $model->newQuery()
                    ->where($model->getAuthIdentifierName(), $identifier)
                    ->where($model->getRememberTokenName(), $token)
                    ->first();
}

/**
 * Update the "remember me" token for the given user in storage.
 *
 * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
 * @param  string  $token
 * @return void
 */
public function updateRememberToken(UserContract $user, $token) {
    $user->setRememberToken($token);

    $user->save();
}

/**
 * Retrieve a user by the given credentials.
 *
 * @param  array  $credentials
 * @return \Illuminate\Contracts\Auth\Authenticatable|null
 */
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();
}

/**
 * Validate a user against the given credentials.
 *
 * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
 * @param  array  $credentials
 * @return bool
     */
    public function validateCredentials(UserContract $user, array $credentials) {
//your method auth
        $plain = $credentials['password'];
        return  md5($plain)==md5($user->getAuthPassword());
    }

/**
 * Create a new instance of the model.
 *
 * @return \Illuminate\Database\Eloquent\Model
 */
public function createModel() {
    $class = '\\' . ltrim($this->model, '\\');
    return new $class;
}

/**
 * Gets the hasher implementation.
 *
 * @return \Illuminate\Contracts\Hashing\Hasher
 */
public function getHasher() {
    return $this->hasher;
}

/**
 * Sets the hasher implementation.
 *
 * @param  \Illuminate\Contracts\Hashing\Hasher  $hasher
 * @return $this
 */
public function setHasher(HasherContract $hasher) {
    $this->hasher = $hasher;

    return $this;
}

/**
 * Gets the name of the Eloquent user model.
 *
 * @return string
 */
public function getModel() {
    return $this->model;
}

/**
 * Sets the name of the Eloquent user model.
 *
 * @param  string  $model
 * @return $this
 */
public function setModel($model) {
    $this->model = $model;

    return $this;
}

}

in config/auth.php 在config / auth.php中

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users_office',
        ],
        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],


'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],


'users_office' => [
        'driver' => 'customUser',
        'model' => App\Models\User::class,
    ],
// 'users' => [
//     'driver' => 'database',
//     'table' => 'users',
// ],
],

in \\vendor\\laravel\\framework\\src\\Illuminate\\AuthCreatesUserProviders.php 在\\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ AuthCreatesUserProviders.php

 public function createUserProvider($provider)
{
    $config = $this->app['config']['auth.providers.'.$provider];    


    if (isset($this->customProviderCreators[$config['driver']])) {
        return call_user_func(
            $this->customProviderCreators[$config['driver']], $this->app, $config
        );
    }

    switch ($config['driver']) {
        case 'database':
            return $this->createDatabaseProvider($config);
        case 'eloquent':
            return $this->createEloquentProvider($config);
        case 'customUser':
            return $this->createCustomUserProvider($config);
        default:
            throw new InvalidArgumentException("Authentication user provider [{$config['driver']}] is not defined.");
    }
}


protected function createCustomUserProvider($config){        
        return new \App\Auth\CustomUserProvider($config['model']);
    }

add App\\Providers\\CustomUserAuthProvider.php 添加App \\ Providers \\ CustomUserAuthProvider.php

    namespace App\Providers;

use Auth;
use App\Models\User;
use App\Auth\CustomUserProvider;
use Illuminate\Support\ServiceProvider;

/**
 * Description of CustomAuthProvider
 *
 */

class CustomUserAuthProvider extends ServiceProvider {

/**
 * Bootstrap the application services.
 *
 * @return void
 */
public function boot()
{        
     Auth::extend('customUser', function($app) {
        // Return an instance of Illuminate\Contracts\Auth\UserProvider...
        return new CustomUserProvider(new User);
    });
}

/**
 * Register the application services.
 *
 * @return void
 */
public function register()
{
    //
}

}

Try by replacing the boot function as below: 尝试更换启动功能,如下所示:

public function boot()
{
    Auth::provider('custom', function($app, array $config) {
        // Return an instance of Illuminate\Contracts\Auth\UserProvider...
        return new CustomUserProvider($app['custom.connection']);
    });
}

Replace the boot function as below: 更换启动功能如下:

public function boot()
    {
        Auth::provider('customUser', function($app, array $config) {
            return new CustomUserProvider($config['model']);
        });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM