首页 > 解决方案 > JWT 无法仅在实时站点上从请求中解析令牌

问题描述

我使用 PHP(7.0 版)构建了一个自定义 API,特别是 Laravel 框架(5.5.40 版),它使用 tymon/jwt-auth(1.0.0-rc.2 版)包进行用户授权。我的许多控制器功能都使用此行来获取当前用户的凭据:

$user = JWTAuth::parseToken()->authenticate();

我使用 Postman 来测试我的 api。

当我在本地环境中进行 api 调用时,授权正常通过并返回预期的数据。当我对我的实时站点 API(托管在我的 apache 服务器中)进行 api 调用时,我收到此错误:

无法从请求中解析令牌

由于此错误仅发生在我的实时 API 上,我推测我的 API 的 .htaccess 文件存在问题(这是应用程序通常识别的方式,我进行了一些调试并检查了我的 apache error_log 以确保对我的API 的 .htaccess 被识别)。我尝试添加这些行(以及这些行的不同变体,因为我在研究时遇到了多个建议):

RewriteEngine On
RewriteCond %{HTTP:Authorization} .+
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

但错误仍然存​​在。我尝试添加此代码:

var_dump($_SERVER);

在...之前:

$user = JWTAuth::parseToken()->authenticate();

我注意到在我的实时 API 中,$_SERVER 数组中缺少这些元素: REDIRECT_HTTP_AUTHORIZATION、HTTP_AUTHORIZATION

上面的 .htaccess 代码不是要手动添加这些吗?

这些是我在 Chrome DevTools 中看到的响应标头:

访问控制允许来源: http://example.gr

缓存控制:无缓存,私有

连接:关闭

内容编码: gzip

内容类型:应用程序/json

日期:格林威治标准时间 2019 年 5 月 14 日星期二 11:33:10

服务器:阿帕奇

传输编码:分块

变化:来源,接受编码

X-RateLimit-限制: 60

X-RateLimit-剩余: 59

这些是我的请求标头(显示的临时标头):

接受:应用程序/json,文本/纯文本,/

Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9hcGkudjIucmVzY2FwZXIuaG9uZXN0cGFydG5lcnMuZ3JcL2FwaVwvYXV0aFwvbG9naW4iLCJpYXQiOjE1NTc4MzM1ODksImV4cCI6MTY0NDIzMzU4OSwibmJmIjoxNTU3ODMzNTg5LCJqdGkiOiJweDRjNDlOckFQbGMyM21OIiwic3ViIjo1LCJwcnYiOiI4N2UwYWYxZWY5ZmQxNTgxMmZkZWM5NzE1M2ExNGUwYjA0NzU0NmFhIn0.YWPhPDeNrB4TI1gdhBYylrmIAs1-r1-K2M2eKyO89KY

来源: http ://example.gr

参考: http://example/summary

用户代理: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36

编辑:这是 httpd.conf 文件中该特定域的生产服务器的虚拟主机配置:

<VirtualHost |example-ip|:80>
  ServerName api.example.gr
    ServerAlias www.api.example.gr
  DocumentRoot /home/example/public_html/api/public
  ServerAdmin webmaster@api.example.gr
  UseCanonicalName Off

  ## User example # Needed for Cpanel::ApacheConf
  <IfModule userdir_module>
    <IfModule !mpm_itk.c>
      <IfModule !ruid2_module>
        <IfModule !mod_passenger.c>
          UserDir enabled example
        </IfModule>
      </IfModule>
    </IfModule>
  </IfModule>

  # Enable backwards compatible Server Side Include expression parser for Apache versions >= 2.4.
  # To selectively use the newer Apache 2.4 expression parser, disable SSILegacyExprParser in
  # the user's .htaccess file.  For more information, please read:
  #    http://httpd.apache.org/docs/2.4/mod/mod_include.html#ssilegacyexprparser
  <IfModule include_module>
    <Directory "/home/example/public_html/api/public">
      SSILegacyExprParser On
    </Directory>
  </IfModule>



  <IfModule suphp_module>
    suPHP_UserGroup example example
  </IfModule>
  <IfModule suexec_module>
    <IfModule !mod_ruid2.c>
      SuexecUserGroup example example
    </IfModule>
  </IfModule>
  <IfModule ruid2_module>
    RMode config
    RUidGid example example
  </IfModule>
  <IfModule mpm_itk.c>
    # For more information on MPM ITK, please read:
    #   http://mpm-itk.sesse.net/
    AssignUserID example example
  </IfModule>
  <IfModule mod_passenger.c>
    PassengerUser example
    PassengerGroup example
  </IfModule>

  <IfModule alias_module>
    ScriptAlias /cgi-bin/ /home/example/public_html/api/public/cgi-bin/
  </IfModule>





  # To customize this VirtualHost use an include file at the following location
  # Include "/etc/apache2/conf.d/userdata/std/2_4/example/api.example.gr/*.conf"
</VirtualHost>
<VirtualHost 1|example-ip|:443>
  ServerName api.example.gr
  ServerAlias www.api.example.gr
  DocumentRoot /home/example/public_html/api/public
  ServerAdmin webmaster@api.example.gr
  UseCanonicalName Off

  ## User example # Needed for Cpanel::ApacheConf
  <IfModule userdir_module>
    <IfModule !mpm_itk.c>
      <IfModule !ruid2_module>
        <IfModule !mod_passenger.c>
          UserDir enabled example
        </IfModule>
      </IfModule>
    </IfModule>
  </IfModule>

  # Enable backwards compatible Server Side Include expression parser for Apache versions >= 2.4.
  # To selectively use the newer Apache 2.4 expression parser, disable SSILegacyExprParser in
  # the user's .htaccess file.  For more information, please read:
  #    http://httpd.apache.org/docs/2.4/mod/mod_include.html#ssilegacyexprparser
  <IfModule mod_include.c>
    <Directory "/home/example/public_html/api/public">
      SSILegacyExprParser On
    </Directory>
  </IfModule>


  <Proxymatch ^https?://127\.0\.0\.1:(2082|2083|2077|2078|2079|2080|2086|2087|2095|2096)/>
       <IfModule security2_module>
          SecRuleEngine Off
       </IfModule>
  </Proxymatch>

  <IfModule mod_suphp.c>
    suPHP_UserGroup example example
  </IfModule>
  <IfModule suexec_module>
    <IfModule !mod_ruid2.c>
      SuexecUserGroup example example
    </IfModule>
  </IfModule>
  <IfModule ruid2_module>
    RMode config
    RUidGid example example
  </IfModule>
  <IfModule mpm_itk.c>
    # For more information on MPM ITK, please read:
    #   http://mpm-itk.sesse.net/
    AssignUserID example example
  </IfModule>
  <IfModule mod_passenger.c>
    PassengerUser example
    PassengerGroup example
  </IfModule>

  <IfModule alias_module>
    ScriptAlias /cgi-bin/ /home/example/public_html/api/public/cgi-bin/
  </IfModule>
  <IfModule ssl_module>
    SSLEngine on

    SSLCertificateFile /var/cpanel/ssl/apache_tls/api.example.gr/combined

    SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
    <Directory "/home/example/public_html/api/public/cgi-bin">
      SSLOptions +StdEnvVars
    </Directory>
  </IfModule>




  # To customize this VirtualHost use an include file at the following location
  # Include "/etc/apache2/conf.d/userdata/ssl/2_4/example/api.example.gr/*.conf"

</VirtualHost>

需要注意的是,我的实际 API 的 ip 和 domain 已替换为示例:

前端网址: http ://example.gr

API 网址: http ://api.example.gr

标签: phplaravel.htaccessjwtapache2

解决方案


似乎 apache 正在删除授权标头。所以试试这个

RewriteCond %{HTTP:Authorization} .+
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

RewriteCond %{HTTP:Authorization} .
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

正则表达式有点不同。此配置是从 symfony/apache-pack 的默认 .htaccess 直接复制/粘贴,您可以在此处找到 symfony/apache-pack


推荐阅读