Zend framework2正确的登录方式基本验证


zend framework2 proper way to basic login authentication

我目前正在学习Zend2。我的第一次尝试是创建具有基本登录表单的安全应用程序。因此,我的第一个想法是创建一个通用的SecuredController,它在构造函数中检查用户身份,并在必要时进行重定向。我看到了Zend1的解决方案,并且正在工作:

class SecuredController extends AbstractActionController
{
    function __construct()
    {
        $auth = new AuthenticationService();
        if ( $auth->hasIdentity() ) {
            return $this->redirect()->toRoute("ts");
        }
        return $this->redirect()->toRoute( "login" );
    }
}

然后扩展整个应用程序中使用的其他一些控制器:

class MainController extends SecuredController
{
    public function indexAction()
    {
        return new ViewModel();
    }
}

我省略了LoginController和IndexController(与现在的MainController相同),但你知道它是如何设置的。模块的配置如下:

<?php
namespace Main;
return array(
    'controllers' => array(
        'invokables' => array(
            'Main'Controller'Secured' => 'Main'Controller'Common'SecuredController',
            'Main'Controller'Login' => 'Main'Controller'LoginController',
            'Main'Controller'Main' => 'Main'Controller'MainController',
            'Main'Controller'Index' => 'Main'Controller'IndexController',
        ),
    ),
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Zend'Mvc'Router'Http'Literal',
                'options' => array(
                    'route' => '/',
                    'defaults' => array(
                        'controller' => 'Main'Controller'Index',
                        'action' => 'index',
                    ),
                ),
            ),
            'main' => array(
                'type' => 'segment',
                'options' => array(
                    'route'    => '/ts[/][:action]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ),
                    'defaults' => array(
                        'controller' => 'Main'Controller'Main',
                        'action'     => 'index',
                    ),
                ),
            ),
            'login' => array(
                'type' => 'Zend'Mvc'Router'Http'Literal',
                'options' => array(
                    'route' => '/login',
                    'defaults' => array(
                        'controller' => 'Main'Controller'Login',
                        'action' => 'login',
                    ),
                ),
            ),
            'logout' => array(
                'type' => 'Zend'Mvc'Router'Http'Literal',
                'options' => array(
                    'route' => '/logout',
                    'defaults' => array(
                        'controller' => 'Main'Controller'Login',
                        'action' => 'logout',
                    ),
                ),
            ),
        ),
    ),
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions' => true,
        'doctype' => 'HTML5',
        'not_found_template' => 'error/404',
        'exception_template' => 'error/index',
        'template_map' => array(
            'layout/login' => __DIR__ . '/../view/layout/login.phtml',
            'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
            'application/index/index' => __DIR__ . '/../view/main/index/index.phtml',
            'error/404' => __DIR__ . '/../view/error/404.phtml',
            'error/index' => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
    'doctrine' => array(
        'driver' => array(
            __NAMESPACE__ . '_driver' => array(
                'class' => 'Doctrine'ORM'Mapping'Driver'AnnotationDriver',
                'cache' => 'array',
                'paths' => array( __DIR__ . '/../src/' . __NAMESPACE__ . '/Entity' )
            ),
            'orm_default' => array(
                'drivers' => array(
                    __NAMESPACE__ . ''Entity' => __NAMESPACE__ . '_driver'
                )
            )
        )
    ),
);

但不幸的是它不工作,我有错误:

Url plugin requires that controller event compose a router; none found

有人知道如何实现我的场景吗?保护整个应用程序,重定向到/login路由用户而不需要身份。

添加你的超级控制器

namespace Admin'Controller;
use Zend'Mvc'Controller'AbstractActionController;
use Zend'Authentication'AuthenticationService;
...
class AdminController extends AbstractActionController
{
public function onDispatch('Zend'Mvc'MvcEvent $e)
{
    /**
     * Verifica se o usuario se encontra logado, caso contrario redirecion ele para o login
     */
    $this->authService = new AuthenticationService();
    if(!$this->authService->hasIdentity()){
        $this->redirect()->toRoute("login");
    }
        return parent::onDispatch($e);
}
 ...
}
我相信这将在您的项目设计中起作用。

issue 1

return $this->redirect()->toRoute("ts");

你的配置中没有名为'ts'的路由,你需要在使用toRoute()时设置路由。

2)

问题

您没有正确设置AuthenticationService。你需要指定一个适配器来使用它。

与其在控制器中实例化它,不如在你的ServiceManager配置中定义它。

配置:

'My'AuthService' => function($sm) {
    $auth = new 'Zend'Authentication'AuthenticationService();
    $auth->setAdatper(/**  LDAP or What ever **/);
    return $auth;
},

控制器:

// already setup for you in the service manager
$authService = $this->getServiceLocator()->get('My'AuthService');

你总是可以尝试zfcUser模块,如果你想要一些开箱即用的东西,如允许注册用户等:https://github.com/ZF-Commons/ZfcUser

认证模块宽度登录页面