我在PHP
制作授权系统,我遇到了传递JWT令牌的承载方案,我读了RFC 6750。我有以下疑问:
Authorization: Bearer fdbghfbfgbjhg_something
,服务器应如何处理此问题,decodeFunc(explode(" ", $this->getRequest()->getHeader("Authorization"))[1])
?答案 0 :(得分:58)
1.提高安全性,因为如果在url中发送的标头中没有发送令牌,它将被网络系统,服务器日志记录....
2.获得承载令牌的良好功能
/**
* Get header Authorization
* */
function getAuthorizationHeader(){
$headers = null;
if (isset($_SERVER['Authorization'])) {
$headers = trim($_SERVER["Authorization"]);
}
else if (isset($_SERVER['HTTP_AUTHORIZATION'])) { //Nginx or fast CGI
$headers = trim($_SERVER["HTTP_AUTHORIZATION"]);
} elseif (function_exists('apache_request_headers')) {
$requestHeaders = apache_request_headers();
// Server-side fix for bug in old Android versions (a nice side-effect of this fix means we don't care about capitalization for Authorization)
$requestHeaders = array_combine(array_map('ucwords', array_keys($requestHeaders)), array_values($requestHeaders));
//print_r($requestHeaders);
if (isset($requestHeaders['Authorization'])) {
$headers = trim($requestHeaders['Authorization']);
}
}
return $headers;
}
/**
* get access token from header
* */
function getBearerToken() {
$headers = getAuthorizationHeader();
// HEADER: Get the access token from the header
if (!empty($headers)) {
if (preg_match('/Bearer\s(\S+)/', $headers, $matches)) {
return $matches[1];
}
}
return null;
}
答案 1 :(得分:-3)
我建议使用以下RegEx来检查它是否是有效的jwt-token:
/Bearer\s((.*)\.(.*)\.(.*))/
并使用匹配[1]访问它。
这是JWT-Token的结构,请参阅:https://jwt.io/