在My Laravel Passport应用程序中,如何接受access_tokens(当客户端不发送Bearer标头时)

时间:2017-06-08 10:57:20

标签: laravel oauth-2.0

我使用的OAuth2客户端不发送授权:承载,但标准的laravel / passport设置会检查此标头。如何使它与Laravel方面兼容?

1 个答案:

答案 0 :(得分:0)

添加以下中间件

<?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,
    ];