gpt4 book ai didi

php - 检查 ReSTLer API 框架上的 header 授权

转载 作者:行者123 更新时间:2023-12-05 00:14:17 24 4
gpt4 key购买 nike

我想扩展 ReSTLer 以检查是否传递了自定义 header 授权的有效值。我在绕过修复时遇到问题,我试过了,但没有机会:

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);
}
}
}
}

最佳答案

让我快速修复您的自定义身份验证 header 示例

class HeaderAuth implements iAuthenticate{
function __isAuthenticated(){
//we are only looking for a custom header called 'Auth'
//but $_SERVER prepends HTTP_ and makes it all uppercase
//thats why we need to look for 'HTTP_AUTH' instead
//also do not use header 'Authorization'. It is not
//included in PHP's $_SERVER variable
return isset($_SERVER['HTTP_AUTH']) && $_SERVER['HTTP_AUTH']=='password';
}
}

我已经测试过它以确保它有效!

这里是如何让它与 Authorization header 一起工作,它只在 apache 服务器上工作

 class Authorization implements iAuthenticate{
function __isAuthenticated(){
$headers = apache_request_headers();
return isset($headers['Authorization']) && $headers['Authorization']=='password';
}
}

我发现 PHP 将 Authorization header 转换为 $_SERVER['PHP_AUTH_DIGEST']$_SERVER['PHP_AUTH_USER']$_SERVER['PHP_AUTH_PW'] 根据身份验证请求的类型(摘要或基本),我们可以使用以下 .htaccess 文件来启用 $_SERVER[ 'HTTP_AUTHORIZATION'] header

目录索引index.php

DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization},last]
</IfModule>

重要的部分是RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization},last]

现在我们的例子可以简化为:

class Authorization implements iAuthenticate{
function __isAuthenticated(){
return isset($_SERVER['HTTP_AUTHORIZATION']) && $_SERVER['HTTP_AUTHORIZATION']=='password';
}
}

关于php - 检查 ReSTLer API 框架上的 header 授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7818867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com