zoukankan      html  css  js  c++  java
  • laravel5.5jwt-auth的使用

    laravel5.5 + jwt-auth:dev-develop

    1. 安装扩展
        composer require tymon/jwt-auth:dev-develop --prefer-source
    
    1. 添加服务提供器

    config/app.php中增加provider者和aliases,写入对应的数组

    //provider
    'TymonJWTAuthProvidersLaravelServiceProvider'
    
    //aliases
    'JWTAuth' => 'TymonJWTAuthFacadesJWTAuth'
    'JWTFactory' => 'TymonJWTAuthFacadesJWTFactory'
    
    
    1. 发布配置文件
        php artisan vendor:publish --provider="TymonJWTAuthProvidersLaravelServiceProvider" --force
    

    命令会生成 config/jwt.php 配置文件

    1. 生成key
        php artisan jwt:secret
    

    会在.env 添加JWT_SECRET

    JWT_SECRET=z4Pv7YXnOOodpuGO7FOy1vLsxxxxicmoU
    
    1. 更改user model
    <?php
    
    namespace App;
    
    use TymonJWTAuthContractsJWTSubject;
    use IlluminateNotificationsNotifiable;
    use IlluminateFoundationAuthUser as Authenticatable;
    
    class User extends Authenticatable implements JWTSubject
    {
        use Notifiable;
    
        // Rest omitted for brevity
    
        /**
         * Get the identifier that will be stored in the subject claim of the JWT.
         *
         * @return mixed
         */
        public function getJWTIdentifier()
        {
            return $this->getKey();
        }
    
        /**
         * Return a key value array, containing any custom claims to be added to the JWT.
         *
         * @return array
         */
        public function getJWTCustomClaims()
        {
            return [];
        }
    }
    
    1. 配置auth看守器

    config/auth.php中修改看守器

    'defaults' => [
        'guard' => 'api',
        'passwords' => 'users',
    ],
    
    ...
    
    'guards' => [
        'api' => [
            'driver' => 'jwt',
            'provider' => 'users',
        ],
    ],
    
    1. 配置路由
    Route::get('/signin', "AuthController@signin");
    
    //这里分配了中间件,验证是否登录
    Route::group(['middleware' => ['auth:api']], function(){
        Route::get('menu', 'MenuController@index');
    });
    
    1. 登录验证
    public function signin(Request $request)
    {
    
        if($token = JWTAuth::getToken()){
            try{
                JWTAuth::invalidate($token);
            }catch(Exception $e){
            }
        }
        $credentials = $request->only('name', 'password');
        if (! $token = JWTAuth::attempt($credentials)) {
            return $this->error('用户名或密码错误');
        }
        return $this->success(['token' => $token]);
    }
    

    参考资料

    https://github.com/tymondesigns/jwt-auth/wiki/Installation

    http://jwt-auth.readthedocs.io/en/docs/quick-start/#update-your-user-model

    https://github.com/tymondesigns/jwt-auth/issues/1298

    由于使用老版产生的问题

    1. Class TymonJWTAuthProvidersJWTNamshi does not exist

      php artisan vendor:publish --provider="TymonJWTAuthProvidersLaravelServiceProvider" --force

    2. Argument 1 passed to TymonJWTAuthJWT::fromUser() must be an instance of TymonJWTAuthContractsJWTSubject

    更改user model

    <?php
    
    namespace App;
    
    use TymonJWTAuthContractsJWTSubject;
    use IlluminateNotificationsNotifiable;
    use IlluminateFoundationAuthUser as Authenticatable;
    
    class User extends Authenticatable implements JWTSubject
    {
        use Notifiable;
    
        // Rest omitted for brevity
    
        /**
         * Get the identifier that will be stored in the subject claim of the JWT.
         *
         * @return mixed
         */
        public function getJWTIdentifier()
        {
            return $this->getKey();
        }
    
        /**
         * Return a key value array, containing any custom claims to be added to the JWT.
         *
         * @return array
         */
        public function getJWTCustomClaims()
        {
            return [];
        }
    }
    
  • 相关阅读:
    运算符
    JavaScript
    javascript——1
    学习总结
    java开发在线下载功能,自动打开浏览器下载功能下载网络文件或图片
    input 输入框正则表达式限制小数,数字
    【Linux】清理Redis病毒程序kdevtmpfsi
    IDEA 2020.1 版修改pom.xml无法自动导入MAVEN依赖
    【Python】 requests 爬取博客园内容AttributeError: 'NoneType' object has no attribute 'xpath'
    Mysql Column 'xxxxx' in field list is ambiguous"
  • 原文地址:https://www.cnblogs.com/redirect/p/10066675.html
Copyright © 2011-2022 走看看