Zend Framework 2 - ZFCUser -如何从授权中排除登录页面


Zend Framework 2 - ZFCUser - How to exclude landing page from auth

我将ZF2ZFCUserbjyauthorize结合使用。我有一个登陆页,应该是全球可访问的。所有其他页面需要在登录后。

一开始我责怪bjyauthorize不让来宾用户访问我的登陆页面。但是经过一些讨论,似乎ZFCUser正在阻挡道路。

我的问题是:我怎么能告诉ZFCUser不阻止一个页面/动作?

编辑:

我的Application/Module.php看起来像这篇文章。当我将我的应用程序myApp添加到白名单时,我可以访问我的登录页面,但myApp的所有其他操作也是如此。

任何想法如何改变条件,我可以匹配URL或只是白名单我的前端动作?

也许我可以在我的登陆页面添加第二条路线。但这不是一个干净的解决方案,对吧?

如果你坚持在onBoostrap方法中检查身份验证,你可以这样做:

class Module
{
    protected $whitelist = array(
        'zfcuser/login' => array('login'),
        'your-landing-route' => array('your-landing-action'),
    );
    public function onBootstrap($e)
    {
        $app = $e->getApplication();
        $em  = $app->getEventManager();
        $sm  = $app->getServiceManager();
        $list = $this->whitelist;
        $auth = $sm->get('zfcuser_auth_service');
        $em->attach(MvcEvent::EVENT_ROUTE, function($e) use ($list, $auth) {
            $match = $e->getRouteMatch();
            // No route match, this is a 404
            if (!$match instanceof RouteMatch) {
                return;
            }
            // Route and action is whitelisted
            $routeName = $match->getMatchedRouteName();
            $action = $match->getParam("action");
            if(array_key_exists($routeName,$list) && in_array($action,$list[$routeName])) {
                return;
            }
            // User is authenticated
            if ($auth->hasIdentity()) {
                return;
            }
            // Redirect to the user login page, as an example
            $router   = $e->getRouter();
            $url      = $router->assemble(array(), array(
                'name' => 'zfcuser/login'
            ));
            $response = $e->getResponse();
            $response->getHeaders()->addHeaderLine('Location', $url);
            $response->setStatusCode(302);
            return $response;
        }, -100);
    }
}

我只是稍微改变了一下代码,所以你的白清单也包含了特定的操作。然后,我们可以检查动作参数,使其与白清单更具体一些。

我不知道这是不是最好的方法,我只是告诉你怎么做。

我不认为你甚至需要检查身份验证时使用BjyAuthorize,因为你可以只使用资源检查。如果用户具有来宾角色以外的任何角色,那么他们就是真正的用户并经过身份验证。再一次,我不是100%,但我知道我不使用ZfcUser身份验证检查在我的应用程序中使用BjyAuthorize。我只是使用路由保护来指定特定路由所需的角色级别。

也许有人能解释一下?