Laravel 护照更改 header 身份验证

标签 laravel http laravel-passport

我正在使用 Laravel 护照,它需要在每个请求中发送要发送的 header 身份验证。

是否可以将 header 的名称更改为 X-Access-Token?

我看到passport用的包

League\OAuth2\Server\AuthorizationValidators;

方法:

/**
 * {@inheritdoc}
 */
public function validateAuthorization(ServerRequestInterface $request)
{
    dd($request);
    if ($request->hasHeader('authorization') === false) {
        throw OAuthServerException::accessDenied('Missing "Authorization" header');
    }

    $header = $request->getHeader('authorization');
    $jwt = trim(preg_replace('/^(?:\s+)?Bearer\s/', '', $header[0]));

我试图在此处进行更改,但似乎 header 的验证发生在该方法之前。

最佳答案

有许多基础代码片段依赖于 authorization header 的存在。

如果您愿意,可以自己动手。

另请注意,authorization 是一个web 标准 请求 header 。 X-Access-Token 是一种响应 header 模式。

*编辑** 根据我们下面的对话,您可以使用中间件和中间件优先级来指示哪个先运行,观察具有 X-Access-Token 的请求并使用 addHeader 转换值authorization 的 header :

php artisan make:middleware AuthorizationToolMiddleware

然后在handle函数中:

public function handle($request, Closure $next)
{

    $request->headers->set('Authorization', $request->headers->get('X-Access-Token'));

    return $next($request);
}

此中间件应在其他中间件之前执行,以确保在 passport 处理请求时设置 header 。

关于Laravel 护照更改 header 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49961008/

相关文章:

php - laravel/ui[v3.2.0, ..., 3.x-dev] 需要照明/控制台 ^8.0

php - Laravel 黄昏 : unknown error: call function result missing 'value'

c# - .NET HTTP 解析器

laravel - 如何为经过身份验证的用户和未经身份验证的用户提供路由

laravel - Guzzle 不向 Laravel Passport 发送授权类型

php - Mac OS X El Capitan 需要 Mcrypt PHP 扩展

php - 随机运行时异常 : The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key length

java - 使用 Apache HttpComponents 在重定向时覆盖 header

android - Android 上的 HttpClient : NoHttpResponseException through UMTS/3G

Laravel Passport 返回 403 错误而不是路由 ('login' )