3

zendでhttpauthを実行したいのですが、その記事http://framework.zend.com/manual/en/zend.auth.adapter.http.htmlを読みましたが、その価値はないと思います(なぜパスワードなのか)外部ファイルから取得...?)。私はそれがヘッダーで簡単にできることを知っています:

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();動作しても、この直後は使えません。誰かが抜け道を示すことができますか?

4

2 に答える 2

4

ファイルリゾルバーを使用する必要はありません。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();
于 2012-06-22T22:01:45.730 に答える
0

サンプル アクション コントローラの例:

    public function preDispatch() {

        if (
            !isset($_SERVER['PHP_AUTH_USER']) 
            || !isset($_SERVER['PHP_AUTH_PW']) 
            || 'admin' != $_SERVER['PHP_AUTH_USER'] 
            || 'admin' != $_SERVER['PHP_AUTH_PW']
        ) {
            $this->getResponse()->setHeader('WWW-Authenticate', 'Basic realm="Authentication required"');
            $this->getResponse()->setHttpResponseCode(401);
            if ('not-auth' !== $this->getRequest()->getActionName()) {
                $this->_forward('not-auth');
            }
        }
    }

    public function indexAction() { }

    public function notAuthAction() { }

}

この巧妙な解決策はここで見つかりました。 https://gist.github.com/umpirsky/1148691

于 2014-07-25T19:41:00.550 に答える