我正在尝试设置JSON Web令牌,以便通过移动应用与我的php后端进行通信。我可以请求令牌就好了。当我需要验证它(或向另一个端点发出请求)时,我使用以下格式设置Authorization标头:
Bearer <token here>
但由于某些原因,在我的后端,$_SERVER['HTTP_AUTHORIZATION']
未设置。
我在本地主机上使用Mamp Pro和PHP7。这是我的$_SERVER
数组的转储:
Array
(
[SERVER_SOFTWARE] => Apache
[REQUEST_URI] => /wp-json/jwt-auth/v1/token/validate/
[REDIRECT_STATUS] => 200
[HTTP_HOST] => localhost.dev
[CONTENT_TYPE] => application/x-www-form-urlencoded
[CONTENT_LENGTH] => 54
[HTTP_CONNECTION] => keep-alive
[HTTP_ACCEPT] => */*
[HTTP_USER_AGENT] => CocoaRestClient/15 CFNetwork/760.2.6 Darwin/15.3.0 (x86_64)
[HTTP_ACCEPT_LANGUAGE] => en-us
[HTTP_ACCEPT_ENCODING] => gzip, deflate
[PATH] => /usr/bin:/bin:/usr/sbin:/sbin
[SERVER_SIGNATURE] =>
[SERVER_NAME] => cloud.iblue.eu
[SERVER_ADDR] => ::1
[SERVER_PORT] => 80
[REMOTE_ADDR] => ::1
[DOCUMENT_ROOT] => /Applications/MAMP/htdocs/dev
[SERVER_ADMIN] => you@example.com
[SCRIPT_FILENAME] => /Applications/MAMP/htdocs/dev/index.php
[REMOTE_PORT] => 51804
[REDIRECT_URL] => /wp-json/jwt-auth/v1/token/validate/
[GATEWAY_INTERFACE] => CGI/1.1
[SERVER_PROTOCOL] => HTTP/1.1
[REQUEST_METHOD] => POST
[QUERY_STRING] =>
[SCRIPT_NAME] => /index.php
[PHP_SELF] => /index.php
[REQUEST_TIME_FLOAT] => 1459177711.33
[REQUEST_TIME] => 1459177711
[argv] => Array
(
)
[argc] => 0
)
当我尝试使用带有Basic dGVzdEB0ZXN0LmNvbToxMjM0NQ==
的HTTP基本身份验证作为授权标头时,它可以正常工作:
[PHP_AUTH_USER] => test@test.com
[PHP_AUTH_PW] => 12345
任何想法都错了吗?
答案 0 :(得分:10)
好的,我刚刚在这里找到答案:https://devhacksandgoodies.wordpress.com/2014/06/27/apache-pass-authorization-header-to-phps-_serverhttp_authorization/
所以我在htaccess文件中添加了以下行,它解决了我的问题:
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
答案 1 :(得分:0)
我刚遇到这个问题(同一个插件!),并通过编辑验证功能来解决它:
// in public/class-jwt-auth-plugin.php
// Check for auth in raw headers without $_SERVER variable (Apache Server).
if ( !$auth && function_exists('getallheaders') ) {
$headers = getallheaders();
$auth = isset($headers['Authorization'])
? $headers['Authorization'] : false;
}
虽然我不是PHP专家,但我不明白为什么这个代码不能包含在插件中。
答案 2 :(得分:0)