【发布时间】:2023-03-28 00:46:02
【问题描述】:
我使用 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 调用时,我收到此错误:
无法从请求中解析令牌
由于这个错误只发生在我的 live 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 中看到的响应标头:
Access-Control-Allow-Origin: http://example.gr
缓存控制:无缓存,私有
连接:关闭
内容编码: gzip
内容类型:应用程序/json
日期:格林威治标准时间 2019 年 5 月 14 日星期二 11:33:10
服务器: Apache
传输编码:分块
变化:来源、接受编码
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 59
这些是我的请求标头(显示的临时标头):
接受: application/json, text/plain, /
授权:强>承载eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9hcGkudjIucmVzY2FwZXIuaG9uZXN0cGFydG5lcnMuZ3JcL2FwaVwvYXV0aFwvbG9naW4iLCJpYXQiOjE1NTc4MzM1ODksImV4cCI6MTY0NDIzMzU4OSwibmJmIjoxNTU3ODMzNTg5LCJqdGkiOiJweDRjNDlOckFQbGMyM21OIiwic3ViIjo1LCJwcnYiOiI4N2UwYWYxZWY5ZmQxNTgxMmZkZWM5NzE1M2ExNGUwYjA0NzU0NmFhIn0.YWPhPDeNrB4TI1gdhBYylrmIAs1-R1-K2M2eKyO89KY P>
用户代理: Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,如 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和域已经被替换为示例:
前端网址: http://example.gr
api 网址: http://api.example.gr
【问题讨论】:
-
如您所见,问题出在 apache 配置中。在您的本地计算机上存在标头,但在生产服务器上它们丢失了。如果您有权访问这些文件,能否粘贴虚拟主机配置。
-
@DilyanTrayanov 嘿,我通过添加虚拟主机配置来编辑我的答案。
标签: php laravel .htaccess jwt apache2