检查 Restler API 框架上的标头授权
我想扩展 Restler 以检查是否通过了自定义标头授权的有效值。我在解决这个问题时遇到了麻烦,我尝试了这个,但没有机会:
class AuthenticateMe implements iAuthenticate() {
function __isAuthenticated() {
//return isset($_SERVER['HTTP_AUTH_KEY']) && $_SERVER['HTTP_AUTH_KEY']==AuthenticateMe::KEY ? TRUE : FALSE;
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
if($header == "Authorization") {
return TRUE;
} else {
//return FALSE;
throw new RestException(404);
}
}
}
}
I want to extend Restler to check if a valid value of custom header Authorization was passed. I am having trouble in getting around the fix, I tried this, but no chance:
class AuthenticateMe implements iAuthenticate() {
function __isAuthenticated() {
//return isset($_SERVER['HTTP_AUTH_KEY']) && $_SERVER['HTTP_AUTH_KEY']==AuthenticateMe::KEY ? TRUE : FALSE;
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
if($header == "Authorization") {
return TRUE;
} else {
//return FALSE;
throw new RestException(404);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我快速修复您的自定义身份验证标头示例,
我已经对其进行了测试以确保其有效!
以下是如何使其与 Authorization 标头一起工作,它仅适用于 apache 服务器
我发现 PHP 将
Authorization
标头转换为$_SERVER['PHP_AUTH_DIGEST']
或$_SERVER['PHP_AUTH_USER']
和$_SERVER['PHP_AUTH_PW']
取决于类型auth 请求(摘要或基本),我们可以使用以下.htaccess
文件来启用$_SERVER['HTTP_AUTHORIZATION']
标头DirectoryIndex index.php
重要部分是 RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization},last]
现在我们的示例可以简化为:
Let me quickly fix your custom auth header example
I have tested it to make sure it works!
Here is how to make it work with Authorization header, it works only on apache servers
I figured out that PHP converts
Authorization
header into$_SERVER['PHP_AUTH_DIGEST']
or$_SERVER['PHP_AUTH_USER']
and$_SERVER['PHP_AUTH_PW']
depending on the type of auth request (digest or basic), we can use the following.htaccess
file to enable the$_SERVER['HTTP_AUTHORIZATION']
headerDirectoryIndex index.php
important part is RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization},last]
Now our example can be simplified to:
标头身份验证
有三种方法可以实现
您可以从 PHP 手册
Restler 1.0 有一个摘要式身份验证示例。我进行了修改以使其与 Restler 2.0 一起使用
Header Authentication
there are three ways to do it
You can read more from PHP Manual
Restler 1.0 had a Digest Authentication example. I've modified to make it work with Restler 2.0