Laravel Auth 调用 tymon/jwt-auth 源码分析

版本

  • "tymon/jwt-auth": "1.0.0-rc.1"
  • "laravel/framework": "5.5.*"

tymon/jwt 的使用

这里省略安装步骤,直接写使用过程

  1. auth.php
'api' => [
   'driver' => 'jwt',
   'provider' => 'users',
],
  1. 路由调用
Route::middleware(['auth:api'])->group(function(){
});

Auth 中间件是如何调用 Guard

在请求通过路由时执行中间件,auth 中间件实例化的是 Illuminate\Auth\Middleware\Authenticate 类:

<?php

namespace Illuminate\Auth\Middleware;

use Closure;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Contracts\Auth\Factory as Auth;

class Authenticate
{
    /**
     * The authentication factory instance.
     *
     * @var \Illuminate\Contracts\Auth\Factory
     */
    protected $auth;

    /**
     * Create a new middleware instance.
     *
     * @param  \Illuminate\Contracts\Auth\Factory  $auth
     * @return void
     */
    public function __construct(Auth $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string[]  ...$guards
     * @return mixed
     *
     * @throws \Illuminate\Auth\AuthenticationException
     */
    public function handle($request, Closure $next, ...$guards)
    {
        $this->authenticate($guards);

        return $next($request);
    }

    /**
     * Determine if the user is logged in to any of the given guards.
     *
     * @param  array  $guards
     * @return void
     *
     * @throws \Illuminate\Auth\AuthenticationException
     */
    protected function authenticate(array $guards)
    {
        if (empty($guards)) {
            return $this->auth->authenticate();
        }

        foreach ($guards as $guard) {
            if ($this->auth->guard($guard)->check()) {
                return $this->auth->shouldUse($guard);
            }
        }

        throw new AuthenticationException('Unauthenticated.', $guards);
    }
}

请求过来后使用了中间件,会调用 handle 方法
所以我们看到 authenticate 方法中,会循环调用 guardcheck 方法,成功会返回 auth 实例,失败则统一抛出 AuthenticationException

namespace Illuminate\Auth;

use Exception;

class AuthenticationException extends Exception
{
    /**
     * All of the guards that were checked.
     *
     * @var array
     */
    protected $guards;

    /**
     * Create a new authentication exception.
     *
     * @param  string  $message
     * @param  array  $guards
     * @return void
     */
    public function __construct($message = 'Unauthenticated.', array $guards = [])
    {
        parent::__construct($message);

        $this->guards = $guards;
    }

    /**
     * Get the guards that were checked.
     *
     * @return array
     */
    public function guards()
    {
        return $this->guards;
    }
}

可以在 App\Exceptions\Handler 中的 unauthenticated() 捕获该异常,这也是为什么不管是 token 过期还是拉黑,使用 auth 中间件的错误提示一直是 Unauthenticated. 的原因!

为什么使用 auth:jwtGuard 的方式

因为 jwt.auth 不能指定 guard ,只会调用默认的 guard ,当你的 default 指向 web 时,会导致一直未认证。
解决办法:

  1. 定义中间件,每次调用 jwt.auth 前,都把默认的 guard 指定为你当前路由适用的 guard
  2. 使用 auth:jwtGuard 的调用方式,如 auth:apiapiguard driver 选择 jwt ,这样在错误信息返回时就没有具体原因了,都是统一的 Unauthenticated.
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值