Zend 2和auth配置路由

时间:2015-07-03 09:29:32

标签: authentication zend-framework2

我正在一个Zend2项目上工作,那里有一个整个网站的身份验证系统,直到我们必须开发一个公共网络服务模块之前一切正常。

我想知道是否可以允许用户访问Zend 2的特定模块/路由?

1 个答案:

答案 0 :(得分:0)

The Zend\Authentication\Adapter\Http为Zend Framework 2应用程序中的Apache认证提供了一种简单的方法。

它带有两个实现Basic和Digest HTTP Authentication,它们可以与两个子组件组合 - 类本身或FileResolver。我们将使用FileResolver读取存储的凭据并将它们与提交的值进行比较。

首先是第一件事。有一些重要的事情要知道。

  1. 在MODULE_NAME / config /中创建名为auth的文件夹。在该文件夹中创建两个文件basic.txt和digest.txt。文件格式对Apache .htpasswd文件很有吸引力。 Basic - <username>:<realm>:<credentials>,此处的凭据应以明文形式写成,例如:basic:authentication:plaintextpasswordDigest - <username>:<realm>:<credentials>, where <credentials>是所有3个部分的md5哈希值,例如:digest:authentication:dc45122ef294d83e84a8b5a3a6c5356b

  2. 在我们刚创建auth文件夹的同一模块中,打开module.config.php文件并放置此代码。

  3. 代码告诉我们接受哪些认证方案,领域(必须与basic / digest.txt文件中的领域相同,digest_domains(仅当我们使用摘要认证时)是我们想要的URL为了应用相同的有效信息,nonce_timeout设置nonce有效的秒数。

    /**
     * Used for basic authentication
     */
    'authentication_basic' => [
        'adapter' => [
            'config' => [
                'accept_schemes' => 'basic',
                'realm'          => 'authentication',
                'nonce_timeout'  => 3600,
            ],
            'basic'  => __DIR__.'/auth/basic.txt',
        ],
    ],
    /**
     * Used for digest authentication
     */
    'authentication_digest' => [
        'adapter' => [
            'config' => [
                'accept_schemes' => 'digest',
                'realm'          => 'authentication',
                'digest_domains' => '/learn-zf2-authentication/digest',
                'nonce_timeout'  => 3600,
            ],
            'digest' => __DIR__.'/auth/digest.txt',
        ],
    ]
    

    <强> LearnZF2Authentication \厂\ BasicAuthenticationAdapterFactory

      $config = $serviceLocator->get('Config');
        $authConfig = $config['authentication_basic']['adapter'];
        $authAdapter = new HttpAdapter($authConfig['config']);
    
        $basic = new FileResolver();
        $basic->setFile($authConfig['basic']);
    
        $authAdapter->setBasicResolver($basic);
    
        return $authAdapter;
    

    <强> LearnZF2Authentication \厂\ DigestAuthenticationAdapterFactory

        $config = $serviceLocator->get('Config');
        $authConfig = $config['authentication_digest']['adapter'];
        $authAdapter = new HttpAdapter($authConfig['config']);
    
        $digest = new FileResolver();
        $digest->setFile($authConfig['digest']);
    
        $authAdapter->setDigestResolver($digest);
    
        return $authAdapter;
    

    这些是我们用于传递身份验证信息的代码

    <强> Module.php

    /**
     * @var MvcEvent $e
     */
    $request  = $e->getRequest();
    $response = $e->getResponse();
    $view = $e->getApplication()->getMvcEvent()->getViewModel();
    $sm = $e->getApplication()->getServiceManager();
    $authAdapter = $sm->get('LearnZF2Authentication\BasicAuthenticationAdapter');
    
    /**
     * Not HTTP? Stop!
     */
    if (!($request instanceof Http\Request && $response instanceof Http\Response)) {
        return;
    }
    
    /**
     * Call the factory class and try to authenticate
     */
    if ($e->getRouteMatch()->getParam('action') == 'digest') {
        $authAdapter = $sm->get('LearnZF2Authentication\DigestAuthenticationAdapter');
    }
    $authAdapter->setRequest($request);
    $authAdapter->setResponse($response);
    
    if($e->getRouteMatch()->getParam('action') == 'basic' || $e->getRouteMatch()->getParam('action') == 'digest') {
        $result = $authAdapter->authenticate();
    
        /**
         * Pass the information to the view and see what we got
         */
        if ($result->isValid()) {
            return $view->identity = $result->getIdentity();
        } else {
            /**
             * Create a log function or just use the one from LearnZF2.
             * Also make sure to redirect to another page, 404 for example
             */
            foreach ($result->getMessages() as $msg) {
                return $view->authProblem = $msg;
            }
        }
    }
    

    这是我们用于传递身份验证信息的代码

    最后要注意的一件事是,您必须在请求中包含一个名为Authorization的特殊标题,替换:

        RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
    

    编译为CGI的PHP不支持apache_response_headers函数,但我们需要此标头,以便在使用CGI或FastCGI运行时执行基本的HTTP身份验证。

    RewriteRule ^(.*)$ %{ENV:BASE}index.php [E=HTTP_AUTHORIZATION:%    {HTTP:Authorization},L,NC]
    

    并添加到public / index.php

    的顶部
    if (isset($_SERVER["REDIRECT_HTTP_AUTHORIZATION"])) {
       $_SERVER["HTTP_AUTHORIZATION"] = $_SERVER["REDIRECT_HTTP_AUTHORIZATION"];
    }
    

    有些事情需要注意。 auth文件夹以及module.config.php中的身份验证代码最好放在你的主配置文件夹中,其中包含全局| local.php文件并从提交中排除。

相关问题