Laravel Auth加密驱动

作者: ONEDAYLOG | 来源:发表于2017-12-21 16:24 被阅读45次

    登录加密

    Auth认证加密方式并不适用于本人的原有数据,原有数据是MD5的16位加密

    创建一个驱动

    <?php
    namespace App\Exceptions;
    use Illuminate\Auth\EloquentUserProvider;
    use Illuminate\Contracts\Hashing\Hasher as HasherContract;
    use Illuminate\Contracts\Auth\Authenticatable as UserContract;
    
    /**
     * Created by PhpStorm.
     * User: hua
     * Date: 2017/12/13
     * Time: 15:46
     */
    class TestUserProvider extends EloquentUserProvider
    {
        public function __construct(HasherContract $hasher, $model)
        {
            $this->model = $model;
            $this->hasher = $hasher;
        }
    
        public function validateCredentials( UserContract $user, array $credentials)
        {
            $plain = $credentials['password'];
    
            $datapassword = substr(md5($plain), 8, 16);
    
    //        return md5($plain) == $user->getAuthPassword();
            return $datapassword == $user->getAuthPassword();
        }
    }
    

    AuthServiceProvider注册驱动

        public function boot()
        {
            $this->registerPolicies();
            Auth::provider('user-eloquent', function ($app, $config) {
                return New TestUserProvider($app['hash'], $config['model']);
            });
            //
        }
    

    注册后的驱动名称为user-eloquent,修改auth.php中调用的的驱动

        'providers' => [
            'users' => [
                'driver' => 'user-eloquent',
                'model' => \App\Models\UniUser::class,
            ],
            'admins' => [
                'driver' => 'eloquent',
                'model' => \Encore\Admin\Auth\Database\Administrator::class,
            ],
    
            // 'users' => [
            //     'driver' => 'database',
            //     'table' => 'users',
            // ],
        ],
    

    学习资源
    Laravel5.1自带认证系统(Auth)改变加密方式带来的思考
    如何重写Laravel 的attempt方法呢?因为加密方法是自定义的。

    相关文章

      网友评论

        本文标题:Laravel Auth加密驱动

        本文链接:https://www.haomeiwen.com/subject/jhhawxtx.html