laravel使用"tymon/jwt-auth": "0.5.*"

时间:2019-10-22
本文章向大家介绍laravel使用"tymon/jwt-auth": "0.5.*",主要包括laravel使用"tymon/jwt-auth": "0.5.*"使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

基于 JWT-Auth 实现 API 验证

 

基于 JWT-Auth 实现 API 验证

需要提及的几点:

  • 使用session存在的问题:

    • session和cookie是为了解决http无状态的方案。session是用户保存在服务器中的状态信息,cookie中则保存jsessionId,请求服务器时,服务器读取jsessionId从而确定用户的身份信息,而session+cookie用在restful接口上破坏了其“无状态”的特性,session运行时都是保存在内存中,而随着认证用户的增多,服务端的开销会明显增大。这也是restful最致力于通过“无状态”解决的问题。如果使用session,那么restful也就没有什么意义了

    • session降低了系统扩展性。用户认证之后,服务端做认证记录,如果认证的记录被保存在内存中的话,这意味着用户下次请求还必须要请求在这台服务器上,这样才能拿到授权的资源,这样在分布式的应用上,相应的限制了负载均衡器的能力。这也意味着限制了应用的扩展能力

    • cookie不安全,很容易导致跨站请求伪造攻击(CSRF)

  • token存在的问题:

    • 如,如何确定token的过期时间?如果token的有效期过短,很容易造成用户用着用着就掉线的情况,降低用户体验。但目前看来好像并没有太好的办法,只能尽量延长token的有效期,或者每隔一次前端自动向服务端请求一次token

  • 基于 JWT-Auth 的 token 验证体系

  1. 运行软件版本

    • laravel 5.7

  2. 安装 JWT-Auth 扩展包

    composer require tymon/jwt-auth "0.5.*"

或者可以

"require": {
  "tymon/jwt-auth": "0.5.*"
" }
composer update
 
  1. 安装完后在配置文件config/app.php 中添加注册服务提供者和别名:

    ...
    'providers' => [
      ...
       Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class,
    ]
    ...
    'aliases' => [
      ...
       'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class,
    ]
  2. 发布资源配置

    php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\JWTAuthServiceProvider"
  3. 运行以下命令生成密钥key在生成的config/jwt.php 中

    // 如果运行后报错,提示ERROR:Method Tymon\JWTAuth\Commands\JWTGenerateCommand::handle() does not exist,将vendor\tymon\jwt-auth\src\Commands\JWTGenerateCommand.php文件中的 fire() 方法修改为 handle()即可正常生成秘钥
    php artisan jwt:generate
  4. 编辑 app/Http/Kernel.php 添加 jwt.auth 和 jwt.refresh 到应用路由中间件数组:

    protected $routeMiddleware = [
      ...
       'jwt.auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class,
    //   'jwt.refresh' => \Tymon\JWTAuth\Middleware\RefreshToken::class,
    ];
  5. JWTAuth 自身中间件\Tymon\JWTAuth\Middleware\GetUserFromToken 中包含了对生成token的各类情况的验证,以及异常的抛出。下面是其底层验证类GetUserFromToken::class

    // file_path : vendor\tymon\jwt-auth\src\Middleware\GetUserFromToken.php
    <?php

    /*
    * This file is part of jwt-auth.
    *
    * (c) Sean Tymon <tymon148@gmail.com>
    *
    * For the full copyright and license information, please view the LICENSE
    * file that was distributed with this source code.
    */

    namespace Tymon\JWTAuth\Middleware;

    use Tymon\JWTAuth\Exceptions\JWTException; //验证异常类
    use Tymon\JWTAuth\Exceptions\TokenExpiredException;//token过期异常验证类

    class GetUserFromToken extends BaseMiddleware
    {
       /**
        * Handle an incoming request.
        *
        * @param \Illuminate\Http\Request $request
        * @param \Closure $next
        * @return mixed
        */
       public function handle($request, \Closure $next)
      {
           if (! $token = $this->auth->setRequest($request)->getToken()) {
               return $this->respond('tymon.jwt.absent', 'token_not_provided', 400);
          }

           try {
               $user = $this->auth->authenticate($token);
          } catch (TokenExpiredException $e) {
               return $this->respond('tymon.jwt.expired', 'token_expired', $e->getStatusCode(), [$e]);
          } catch (JWTException $e) {
               return $this->respond('tymon.jwt.invalid', 'token_invalid', $e->getStatusCode(), [$e]);
          }

           if (! $user) {
               return $this->respond('tymon.jwt.user_not_found', 'user_not_found', 404);
          }

           $this->events->fire('tymon.jwt.valid', $user);

           return $next($request);
      }
    }

    其中,调用的respond 的方法在vendor\tymon\jwt-auth\src\Middleware\BaseMiddleware.php文件中

     /**
        * Fire event and return the response.
        *
        * @param string   $event
        * @param string   $error
        * @param int $status
        * @param array   $payload
        * @return mixed
        */
       protected function respond($event, $error, $status, $payload = [])
      {
           $response = $this->events->fire($event, $payload, true);

           return $response ?: $this->response->json(['error' => $error], $status);
      }

    可看到,当出现异常需要返回错误信息时,会连带返回一个fire event 的警告事件对象,这里不做详述。

  6. 由底层代码中,可以了解到,我们如果想自定义自己所需要的验证方法,可以将这GetUserFromToken::class 内容复制到我们自己自定义的中间件中。比如:

    • 创建自定义的验证中间件App\Http\Middleware\JwtAuth.php

      php artisan make:middleware JwtAuth
    • 全部复制到自定义的中间件中后,校验下中间件中需要的类是否应用完全,命名空间是否正确等等,检查无误后根据需要自行定义需要的验证功能。

      // demo 
      namespace App\Http\Middleware;

      use Tymon\JWTAuth\Exceptions\JWTException;
      use Tymon\JWTAuth\Exceptions\TokenExpiredException;
      use Closure;
      use Tymon\JWTAuth\Middleware\BaseMiddleware;

      class JwtAuth extends BaseMiddleware
      {
         public function handle($request, \Closure $next)
        {
             if (! $token = $this->auth->setRequest($request)->getToken()) {
                 return $this->respond('tymon.jwt.absent', 'token_not_provided', 400);
            }

             try {
                 $user = $this->auth->authenticate($token);
            } catch (TokenExpiredException $e) {
                 return $this->respond('tymon.jwt.expired', 'token_expired', $e->getStatusCode(), [$e]);
            } catch (JWTException $e) {
                 return $this->respond('tymon.jwt.invalid', 'token_invalid', $e->getStatusCode(), [$e]);
            }

             if (! $user) {
                 return $this->respond('tymon.jwt.user_not_found', 'user_not_found', 404);
            }

             $this->events->fire('tymon.jwt.valid', $user);

             return $next($request);
        }
      }
    • 定义完成后将自定义的中间件放入app\Http\Kernel.php的中间件数组中。

       protected $routeMiddleware = [
      ...
             //'jwt.auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class,
             'jwt.auth_self' => \App\Http\Middleware\JwtAuth::class
        ];
    • 添加好后,即可在routes/api.php 中对需要控制的api路由进行验证控制

      Route::group(['middleware'=>'jwt.auth_self'],function(){
         // 需要控制的api路由
         // ... code
      });
  7. 我们现在可以对请求来的路由进行token的验证,那么接下来我们就需要生成这个token,让后续访问中间件中的请求路由都携带这个token就能实现验证。这里要提一下在安装JWT-Auth过程中生成的配置文件config/jwt.php

    <?php
       return [
      ...
       /*
       |--------------------------------------------------------------------------
       | User Model namespace
       |--------------------------------------------------------------------------
       |
       | Specify the full namespace to your User model.
       | e.g. 'Acme\Entities\User'
       |
       */
    // 设置你的用户model,默认为laravel自带的 User model
       'user' => 'App\User',
    ]

    如果需求需要,可在配置文件中修改用户mode ,但配置的model 中需要引用Illuminate\Foundation\Auth\User as Authenticatable,并继承,写法和User model一致

  8. 具体的请求登录以及获取token信息,登出等功能实现,可参考此文章

    Laravel 5 中使用 JWT(Json Web Token) 实现基于API的用户认证,这里简单提及下常用到的有关其token的方法

    <?php
       namespace App\Http\Controller;

    use Tymon\JWTAuth\JWTAuth;

    class Auth{
           public function test (JWTAuth $JWTAuth){
               
               // 获取请求携带中的token
               $token = $JWTAuth -> getToken();
               // 获取token中的用户信息
               $user = $JWTAuth -> parseToken() -> authenticate();
               // 销毁此次生成的token
               $JWTAuth->setToken( $JWTAuth->getToken() )->invalidate();
               // 自定义生成token,如果需要
               $JWTAuth->setToken('foo.bar.baz');
          }
    }

原文地址:https://www.cnblogs.com/zhanghuilong/p/11719258.html