Zend框架2:添加不同的验证适配器两个不同模块 [英] Zend framework 2 : Add different authentication adapter for two different modules

查看:112
本文介绍了Zend框架2:添加不同的验证适配器两个不同模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的模块。现在我需要添加不同的认证机制,这两个模块。

所以我添加事件code第一模块的Module.php的onBootstrap方法

  $听者= $ serviceManager->获得(第一\\服务\\ AuthListener');
$ listener-> setAdapter($ serviceManager->获得(第一\\服务\\ BasicAuthAdapter'));
$ eventManager->附加(MvcEvent :: EVENT_ROUTE,$听众,0);

和第二模块的Module.php的onBootstrap方法

  $听者= $ serviceManager->获得(二\\服务\\ AuthListener');
$ listener-> setAdapter($ serviceManager->获得(二\\服务\\ AdvAuthAdapter'));
$ eventManager->附加(MvcEvent :: EVENT_ROUTE,$听众,0);

现在,如果我禁用模块之一,功能做工精细,并要求正确认证。同时实现这两个模块做一些重叠的正确认证因此,即使需要的模块,但其它模块事件code也得到了执行,系统就给未经过身份验证错误。

我想这是由于这两个module.php事件处理code,而不需要请求的模块网址照顾执行。

我可以验证之前,请求路由模式验证,但是,这看起来像一个黑客,而不是好的解决方案。

如果存在处理这个问题更好的解决办法?

更新:
我AuthListener code:

 命名空间首先\\服务;
使用Zend的\\认证\\适配器\\ AdapterInterface;
使用Zend的\\的mvc \\ MvcEvent;类AuthListener
{
    保护$适配器;    公共职能setAdapter(AdapterInterface $适配器)
    {
        $这个 - >适配器= $适配器;
    }    公共职能__invoke(MvcEvent $事件)
    {
        $结果= $这个 - >&适配器 - GT;身份验证();        如果($ result->!的isValid()){            $响应= $事件 - > GETRESPONSE();            //设置一些响应​​的内容
            $响应 - > setStatus code(401);            $ routeMatch = $事件 - > getRouteMatch();
            $ routeMatch-> setParam('控制','一\\控制器\\错误);
            $ routeMatch-> setParam('行动','验证');
        }
    }
}


解决方案

有是使模块的特定引导的好方法 - 使用 SharedManager

  $ E-> getApplication() -  GT; getEventManager() -  GT; getSharedManager()
   - >附加(__ NAMESPACE__,'派遣',函数(MvcEvent $ E){     //这个code将在当前__NAMESPACE__所有控制器执行},100);

<一个href=\"http://www.michaelgallego.fr/blog/2013/05/12/understanding-the-zend-framework-2-event-manager/\"相对=nofollow>这里是一个很好的文章,了解与 eventmanager进行 SharedEventManager

有是关于这个问题的听众没有额外的信息,但我试着猜测:


  • 如果您的监听器使用一些可赎回类 - 它的确定,只需更换函数(){} $监听器

  • 如果你作为监听使用一些类,实现
    ListenerAggregateInterface ,则应将监听器
    SharedListenerAggregateInterface 和使用方法 attachAggregate
    而不是连接

我希望它能帮助!

I have two different modules. Now I need to add different authentication mechanism for both modules.

So I added event code first module's Module.php's onBootstrap method

$listener = $serviceManager->get('First\Service\AuthListener');
$listener->setAdapter($serviceManager->get('First\Service\BasicAuthAdapter'));
$eventManager->attach(MvcEvent::EVENT_ROUTE, $listener, 0);

and in second module's Module.php's onBootstrap method

$listener = $serviceManager->get('Second\Service\AuthListener');
$listener->setAdapter($serviceManager->get('Second\Service\AdvAuthAdapter'));
$eventManager->attach(MvcEvent::EVENT_ROUTE, $listener, 0);

Now if I disable one of modules, functionality working fine and request properly authenticated. While enabling both module do some kind of overlapping So even required module properly authenticated, But other module event code also got executed and system give not authenticated error.

I am thinking this due to event handler code in both module.php is executed without take care of requested module url.

I can verify with requested route pattern before authentication, But that is look like a hack instead of good solution.

If better solution exists for handling this issue ?

UPDATE : My AuthListener Code :

namespace First\Service;
use Zend\Authentication\Adapter\AdapterInterface;
use Zend\Mvc\MvcEvent;

class AuthListener
{
    protected $adapter;

    public function setAdapter(AdapterInterface $adapter)
    {
        $this->adapter = $adapter;
    }

    public function __invoke(MvcEvent $event)
    {
        $result = $this->adapter->authenticate();

        if (!$result->isValid()) {

            $response = $event->getResponse();

            // Set some response content
            $response->setStatusCode(401);

            $routeMatch = $event->getRouteMatch();
            $routeMatch->setParam('controller', 'First\Controller\Error');
            $routeMatch->setParam('action', 'Auth');
        }
    }
}

解决方案

There is a good way to make module specific bootstrap - to use SharedManager:

$e->getApplication()->getEventManager()->getSharedManager()
  ->attach(__NAMESPACE__, 'dispatch', function(MvcEvent $e) {

     // This code will be executed for all controllers in current __NAMESPACE__

}, 100);

Here is a good article to understand difference between EventManager and SharedEventManager

There is no additional info about listeners in the question, but I try to guess:

  • If you use as listener some callable class - it's ok, just replace function() { } by your $listener.
  • If you use as listener some class, that implements ListenerAggregateInterface, you should convert listeners to SharedListenerAggregateInterface and use method attachAggregate instead of attach

I hope it helps!

这篇关于Zend框架2:添加不同的验证适配器两个不同模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆