Zend framework Zend框架用户身份验证

Zend framework Zend框架用户身份验证,zend-framework,authentication,Zend Framework,Authentication,ZV MVC中用户网站/REST身份验证的最佳实践是什么?在ZF框架中如何以及在哪里放置代码?你能给我一个代码示例吗 我有一个网站和一个REST服务器,是用Zend框架编写的,但没有实现用户会话jet 谢谢 在引导文件的\u initAutoload中设置身份验证,例如: if(Zend_Auth::getInstance()->hasIdentity()) { Zend_Registry::set('role', Zend_Auth::getInstance()

ZV MVC中用户网站/REST身份验证的最佳实践是什么?在ZF框架中如何以及在哪里放置代码?你能给我一个代码示例吗

我有一个网站和一个REST服务器,是用Zend框架编写的,但没有实现用户会话jet


谢谢

在引导文件的
\u initAutoload
中设置身份验证,例如:

if(Zend_Auth::getInstance()->hasIdentity()) {
    Zend_Registry::set('role', Zend_Auth::getInstance()
                        ->getStorage()->read()->role);
}else{
    Zend_Registry::set('role', 'guests');
}
在REST身份验证的情况下,您可能需要通过传递登录参数而不是通过表单登录来进行身份验证

因此,在您的
AuthenticationController
中可能是这样的:

private function getAuthAdapter() {
    $authAdapter = new Zend_Auth_Adapter_DbTable(
                       Zend_Db_Table::getDefaultAdapter());
    $authAdapter->setTableName('users') // the db table where users are stored
                ->setIdentityColumn('email')                     
                ->setCredentialColumn('password')
                ->setCredentialTreatment('SHA1(CONCAT(?,salt))');

    return $authAdapter;
}

public function logoutAction() {
    Zend_Auth::getInstance()->clearIdentity();
    $this->_redirect('index/index');
}

public function loginAction(){
    if (Zend_Auth::getInstance()->hasIdentity()){
        $this->_redirect('index/index');
    }
    if ($request->isPost()){
        $username = $request->getPost('username');
        $password = $request->getPost('password');

        if ($username != "" && $password != "") {
            $authAdapter = $this->getAuthAdapter ();
            $authAdapter->setIdentity($username)
                        ->setCredential($password);
            $auth = Zend_Auth::getInstance();
            $result = $auth->authenticate($authAdapter);

            if($result->isValid()){
              $identity = $authAdapter->getResultRowObject();
              $authStorage = $auth->getStorage();
              $authStorage->write($identity);
              $this->_redirect ( 'index/index' );
            } 
       }
   }
}

如果您需要有关
zend_auth
zend_acl
的更多帮助,您可以看看这个。

这方面运气好吗?我也有同样的情况。