【问题标题】:laravel 5.7 API Authentication (Passport) Unauthenticatedlaravel 5.7 API 身份验证(护照)未经身份验证
【发布时间】:2019-06-14 10:43:49
【问题描述】:

我正在尝试在我的 laravel 5.7 应用程序中验证 api 调用。我已经按照文档安装了 Passport,我想我没有错过任何东西。每个 API 调用都会返回 401 Unauthenticated。

调试我发现问题是在 cookie 中加密的 csrf 令牌(一旦被解密)与请求中的 csrf 标头不匹配。我知道是因为我在vendor/laravel/passport/src/Guards/TokenGuard.phpgetTokenViaCookie($request) 方法中倾倒了$this->validCsrf($token, $request),它的值为false。

我想不出我做错了什么。

在我的layout.blade.php 中添加<meta name="csrf-token" content="{{ csrf_token() }}">

Kernel.php 中,我在\App\Http\Middleware\EncryptCookies::class 之后添加\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class

在望远镜中,我可以在请求中看到x-xsrf-token 标头和laravel_token cookie 在那里。

欢迎任何关于问题可能是什么或任何调试方法的想法。


我对代码进行了更深入的研究,我看到令牌已加密并保存到 cookie seems to be the session token 中。在passport/src/Http/Middleware/CreateFreshApiToken.php:

public function handle($request, Closure $next, $guard = null)
    {
        $this->guard = $guard;
        $response = $next($request);
        if ($this->shouldReceiveFreshToken($request, $response)) {
            $response->withCookie($this->cookieFactory->make(
                $request->user($this->guard)->getKey(), $request->session()->token()
            ));
        }
        return $response;
    }

所以看起来我的问题是加密的令牌是会话之一,然后它与 csrf 标头进行比较,当然身份验证失败。

我想知道这是否是正常行为,我错过了什么或者是错的?

【问题讨论】:

  • 你到底遇到了什么错误?
  • Tamilvanan N,Laravel 回复“401 Unauthorized”
  • Alexandre,我已经在该链接中尝试了您的解决方案,但没有任何区别。但是谢谢:)
  • 我已经用更多信息编辑了这个问题

标签: laravel-5 laravel-passport


【解决方案1】:

我发现了问题所在。

因为我使用的是 bootstrap-vue,所以我停止使用 bootstrap。我没有意识到的是,在the vue app 中,当它需要./bootstrap.js 时,它不仅在加载引导程序,而且还在设置一些标头。

【讨论】:

    猜你喜欢
    • 2017-01-06
    • 2019-02-28
    • 2017-04-24
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 2019-05-21
    • 2018-02-25
    • 1970-01-01
    相关资源
    最近更新 更多