zoukankan      html  css  js  c++  java
  • Laravel用户认证

    前期准备

    Laravel的权限配置文件位于 config/auth.php,Laravel的认证组件由“guards”和“providers”组成,
    Guard 通过 session 来维护用户登录的状态。Provider 是登录以及访问页面的时候获取用户的信息。本篇主要讲的是如何自定义Provider ,获取用户信息。

    config/auth.php文件

    Laravel提供了两种guard,web以及api,采取默认的web认证。在guards的web中,用了users提供者。接下来就需要注意了,我们自定义了服务提供者,就需要换到新的providers。首先,定义一个使用新驱动的provider:

     'providers' => [
         'users' => [
               'driver' => 'Focus',  //名称自定义,这里为Focus
               'model' => AppModelsUser::class,  //Model放在Models文件夹下
          ],
    ],
    

    Notes: 默认提供了两种驱动database和eloquent,而这两种的提供者分别是DatabaseUserProvider和EloquentUserProvider,都处于laravelframeworksrcIlluminateAuth文件夹下,实现了UserProvider,我们自定义的 Focus provider 也将实现UserProvider。

    生成路由和视图

    php artisan make:auth  //命令可快速生成认证所需要的路由和视图
    

    Http/Controllers 和 resources/views下会相应生成控制器和视图
    默认用的Email,我们用username

    LoginController:
    //添加此方法,返回username

    public function username(){
        return 'username';
    }
    

    login.blade.php:
    将邮箱改为域账号,email 改为username

    数据库

    修改.env配置中的数据库信息。

    php artisan make:model Models/User  // 使用命令创建User模型
    

    默认User是继承Model的,需要修改。

    use IlluminateFoundationAuthUser as Authenticatable;  //引入Authenticatable
    class User extends Authenticatable
    {
    	protected $table      = 'employee';
            protected $primaryKey = 'employee_id';
    
             //can set all fields to user model
            protected $guarded = [];
    }
    

    为什么要引入Authenticatable呢,是因为Authenticatable实现了IlluminateContractsAuthAuthenticatable接口,而FocusUserProvider 需要用到接口的实现。

    创建扩展

    在app下新建 Extensions/FocusUserProvider 文件,参考DatabaseUserProvider和EloquentUserProvider,实现UserProvider:

    namespace AppExtensions;
    
    use AppServicesLdapValidator;
    use IlluminateSupportStr;
    use IlluminateContractsAuthUserProvider;
    use IlluminateContractsAuthAuthenticatable;
        
    class LaravelUserProvider implements UserProvider {
    
        protected $model;
    
        public function __construct($model)
        {
               $this->model = $model;
        } 
       
       //登录成功后,通过此方法获取用户信息,返回匹配该ID的 Authenticatable 实现
        public function retrieveById($identifier) {
           
          //此处可以将信息放入缓存,缓解数据库压力。
        	$model = $this->createModel();
            return $model->newQuery()
                ->where($model->getAuthIdentifierName(), $identifier)
                ->first();
        }
    
        public function retrieveByToken($identifier, $token) {
        }
        public function updateRememberToken(Authenticatable $user, $token) {
        }
    
        //该方法可以根据账号名去查询数据库是否存在匹配的账号
        public function retrieveByCredentials(array $credentials) {  
    
            if (empty($credentials)) {
                return;
            }
    
            // 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(); 	
        }
      
       //该方法可以验证密码是否正确,因为我们是ldap登录,可以在此验证域账号
        public function validateCredentials(Authenticatable $user, array $credentials) {
           
          //LdapValidator类是为了验证域密码的,放在了app/Services,在上面已经引入
            $Ldap  = new LdapValidator($user->username, $credentials['password']);
            
            return $Ldap->validatePassword();
        }
    
        /**
         * Create a new instance of the model.
         *
         * @return IlluminateDatabaseEloquentModel
         */
        public function createModel()
        {
            $class = '\'.ltrim($this->model, '\');
    
            return new $class;
        }
    
    }
    

    注册提供者

    Laravel 提供了AuthServiceProvider, 我们可以在这里注册。

    public function boot()
    {
            $this->registerPolicies();
           
           //Focus为auth.php里面定义的驱动
            Auth::provider('Focus', function($app, array $config){
    
                 return new FocusUserProvider ($config['model']);
            });
    } 
    

    下面就可以访问http://你的域名/login 登录系统

  • 相关阅读:
    NFS服务
    rsync
    jquery animate
    一个简单的widget
    EXTJS学习(一)
    jquery+linq制作博客(二)
    EXTJS学习(二)Message
    Jquery ui widget中的_create(),_init(),destroy()
    Jquery ui widget开发
    Json.net简单用法
  • 原文地址:https://www.cnblogs.com/SexyPhoenix/p/11769959.html
Copyright © 2011-2022 走看看