在Laravel中自定义身份验证

在Laravel中自定义身份验证

问题描述:

我正在使用Laravel 5.1,我需要使用具有自己的密码算法的现有用户表. 经过数小时的研究,我找到了解决方案,这是步骤. 希望这对Laravelers有帮助.

I'm using Laravel 5.1, and I need to use existing user table that has its own password algorithm. After hours and hours of research, I've found solution and here are the steps. Hope this helps Laravelers.

在config/auth.php文件中, 将驱动程序值设置为自定义.像这样.

in config/auth.php file, set driver value to custom. like this.

...
'driver' => 'custom',
...

在app/Auth目录中创建文件'CustomUserProvider.php.

create a file 'CustomUserProvider.php in app/Auth directory.

<?php

namespace App\Auth;

use App\Model\User;
use Carbon\Carbon;
use Illuminate\Auth\GenericUser;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;

class CustomUserProvider implements UserProvider{

    /**
     * Retrieve a user by their unique identifier.
     *
     * @param  mixed $identifier
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */

    public function retrieveById($identifier){
        $qry = User::where('id', '=', $identifier);
        if ($qry->count() > 0){
            $user = $qry->select('id', 'firstName', 'lastName', 'city', 'state', 'zipcode', 'email', 'dob'
                , 'mobilePhone', 'password', 'question', 'answer', 'passwordSalt', 'promoCode', 'createdDate', 'isActivated', 'activationDate'
                , 'isDeleted', 'firstTimeLogin', 'role', 'remember_token')->first();
            return $user;
        }
        return NULL;
    }


    /**
     * Retrieve a user by 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){
        $qry = User::where('id', '=', $identifier)
            ->where('remember_token', '=', $token);
        if ($qry->count() > 0){
            $user = $qry->select('id', 'firstName', 'lastName', 'city', 'state', 'zipcode', 'email', 'dob'
                , 'mobilePhone', 'password', 'question', 'answer', 'passwordSalt', 'promoCode', 'createdDate', 'isActivated', 'activationDate'
                , 'isDeleted', 'firstTimeLogin', 'role', 'remember_token')->first();
            return $user;
        }
        return NULL;
    }


    /**
     * 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(Authenticatable $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){
        $qry = User::where('email', 'like', $credentials['email']);

        if ($qry->count() > 0){
            $user = $qry->select('id', 'firstName', 'lastName', 'city', 'state', 'zipcode', 'email', 'dob'
                , 'mobilePhone', 'password', 'question', 'answer', 'passwordSalt', 'promoCode', 'createdDate', 'isActivated', 'activationDate'
                , 'isDeleted', 'firstTimeLogin', 'role', 'remember_token')->first();
            return $user;
        }
        return NULL;
    }


    /**
     * Validate a user against the given credentials.
     *
     * @param  \Illuminate\Contracts\Auth\Authenticatable $user
     * @param  array $credentials
     * @return bool
     */

    public function validateCredentials(Authenticatable $user, array $credentials){
        $salt = base64_decode($user->passwordSalt);
        $password = $credentials['password'];
        $utf16Password = mb_convert_encoding($password, 'UTF-16LE', 'UTF-8');
        $calculatedPassword = base64_encode(sha1($salt . $utf16Password, true));
        if ($user->email == $credentials['email'] && $user->getAuthPassword() == $calculatedPassword){
            return true;
        }
        return false;
    }
}

?>

接下来,在app/Providers目录中创建一个文件'CustomAuthProvider.php'.

next, create a file 'CustomAuthProvider.php' in app/Providers directory.

<?php

namespace App\Providers;

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(){
        //
    }


}

?>

就是这样.