我想用 zend 做一个 http auth,我读了那篇文章 http://framework.zend.com/manual/en/zend.auth.adapter.http.html但我认为它没有值(value)(为什么密码是从外部文件中获取的......?)。 我知道这可以简单地用标题来完成:
header('WWW-Authenticate: Basic realm=sdfsdf');
header('HTTP/1.0 401 Unauthorized');
die;
但是因为我们使用的是 Zend,所以我想转换它:
$response->setHeader('WWW-Authenticate', 'Basic realm="asda"', true);
$response->setHeader('Status', '401 Unauthorized', true);
它不会接受它,没有任何反应。即使它有效,我也不能在这之后立即使用 die();
。有人可以指出出路吗?
最佳答案
您不必使用文件解析器。您可以通过简单地扩展 Zend_Auth_Adapter_Http_Resolver_Interface 来编写自己的解析器类:
class MyOwnResolver implements Zend_Auth_Adapter_Http_Resolver_Interface
{
/**
* Resolve username/realm to password/hash/etc.
*
* @param string $username Username
* @param string $realm Authentication Realm
* @return string|false User's shared secret, if the user is found in the
* realm, false otherwise.
*/
public function resolve($username, $realm)
{
if ($username == 'testUser' && $realm == 'testPassword') {
return $realm;
} else {
return false;
}
}
}
/* In your controller */
$config = array(
'accept_schemes' => 'basic',
'realm' => 'My Realm',
'nonce_timeout' => 3600,
);
$adapter = new Zend_Auth_Adapter_Http($config);
$result = $adapter->setBasicResolver(new MyOwnResolver())
->setRequest($this->getRequest())
->setResponse($this->getResponse())
->authenticate();
关于php - 使用 Zend 进行 HTTP 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11154669/