【问题标题】:In My Laravel Passport Application, how can I accept access_tokens (when the client does not send a Bearer header)在我的 Laravel Passport Application 中,我如何接受 access_tokens(当客户端不发送 Bearer 标头时)
【发布时间】:2017-06-09 16:54:49
【问题描述】:

我正在使用不发送 Authorization: Bearer 的 OAuth2 客户端,但标准 laravel/passport 设置会检查此标头。如何使其与 Laravel 端兼容?

【问题讨论】:

    标签: laravel oauth-2.0


    【解决方案1】:

    添加如下中间件

    <?php
    
    namespace App\Http\Middleware;
    
    
    class ConvertTokenToBearer
    {
        /**
         * @param \Illuminate\Http\Request $request
         * @param                          $next
         *
         * @return \Symfony\Component\HttpFoundation\Response
         */
        public function handle($request, $next)
        {
            if ($request->get('access_token')) {
                $request->headers->set('Authorization', 'Bearer ' . $request->get('access_token'));
            }
            $response = $next($request);
            return $response;
        }
    }
    

    并将其添加到您的 app/Http/Kernel.php 文件中。

    class Kernel extends HttpKernel
    {
        /**
         * The application's global HTTP middleware stack.
         *
         * These middleware are run during every request to your application.
         *
         * @var array
         */
        protected $middleware = [
            \App\Http\Middleware\ConvertTokenToBearer::class,
            \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
            \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
            \App\Http\Middleware\TrimStrings::class,
            \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        ];
    

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 2021-05-16
      • 2020-06-21
      • 2020-07-17
      • 2018-11-04
      • 1970-01-01
      • 1970-01-01
      • 2021-10-25
      • 2021-01-01
      相关资源
      最近更新 更多