【发布时间】:2014-02-22 02:22:03
【问题描述】:
如果参数具有特定值,我想限制对方法的访问。让我们以这个类为例:
简单的.php:
class Simple
{
function item($name)
{
if($name == "somerestricted")
{
// Here should be an authentication check (or somewhere else), hopefully, using an iAuthenticate class
// Later, there will be a check using a database to determine if authentication will be required
// So user/password may vary
if($authenticated)
{
// Proceed
}
else
{
// ???
}
}
else
{
echo "Hi!";
}
}
}
使用这个认证类:
基本身份验证.php:
class BasicAuthentication implements iAuthenticate
{
const REALM = 'Restricted API';
function __isAllowed()
{
if(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW']))
{
$user = $_SERVER['PHP_AUTH_USER'];
$pass = $_SERVER['PHP_AUTH_PW'];
if($user == 'laterfetched' && $pass == 'fromdatabase')
{
return true;
}
}
header('WWW-Authenticate: Basic realm="'.self::REALM.'"');
throw new RestException(401, 'Basic Authentication Required');
}
}
Index.php(网关): addAuthenticationClass('BasicAuthentication'); $r->addAPIClass('简单'); $r->句柄();
simple/item 方法现在可以公开访问。但是,如果我将item 转换为protected 函数,每个 请求都需要身份验证。这不是我想做的。只有simple/item/somerestricted 需要身份验证。
那么有没有办法将iAuthenticate 限制为特定的参数值?如果没有,我该如何解决这个问题?
用户名和密码在生产使用中会有所不同(取决于给定的参数)。
我发现了这些相关问题:Restler 3.0 Basic Authentication 和 Luracast Restler Authentication
我正在使用 Restler rc4。
【问题讨论】:
标签: php authentication basic-authentication http-authentication restler