Symfony:防火墙,多种登录表单

Jus*_*tin 8 symfony

我无论如何都不是symfony的新手,但我总是使用FOSUserBundle默认情况下阻止一个人使用2种不同的登录表单来验证两种不同的用户类型.

我有两个实体,一个是Admins,另一个是Users.管理员只能登录管理区域,同样用户只能通过前端登录.

我已经关注:http: //symfony.com/doc/2.1/book/security.html ,它也引导我访问http://symfony.com/doc/2.1/cookbook/security/entity_provider.html

我的security.yml是:

jms_security_extra:
    secure_all_services: false
    expressions: true

security:
    encoders:
        Symfony\Component\Security\Core\User\User: sha512
        Fm\AdminBundle\Entity\Admins: sha512
        Fm\MainBundle\Entity\Users: sha512

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    providers:
        chain_provider:
            chain:
                providers: [in_memory, admin]
        in_memory:
            memory:
                users:
                    user:  { password: userpass, roles: [ 'ROLE_USER' ] }
                    admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }

        admin:
            entity: { class: Fm\AdminBundle\Entity\Admins, property: username }


    firewalls:
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false
            anonymous: true

        alogin:
            pattern:  ^/admin/login
            security: false
        login:
            pattern:  ^/login
            security: false
        secured_area:
            pattern:    ^/admin
            anonymous: false
            provider: chain_provider
            switch_user: true
            form_login:
                check_path: /admin/login_check
                login_path: /admin/login
            logout:
                path:   /admin/logout
                target: /admin
        members_area:
            pattern: ^/
            anonymous: false
            form_login: ~
            logout:
                path: /logout
                target: /
            #anonymous: ~
            #http_basic:
            #    realm: "Secured Demo Area"

    access_control:
        - { path: ^/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, roles: ROLE_ADMIN }
Run Code Online (Sandbox Code Playgroud)

在我的路线中,我已经在文档中定义了路线:(默认为/ admin/login和/ admin/login_check,因为我的主路由包括where/admin设置)

_admin_login:
    pattern:   /login
    defaults:  { _controller: FmAdminBundle:Security:login }

_admin_login_check:
    pattern:   /login_check
Run Code Online (Sandbox Code Playgroud)

我在浏览器中收到的错误是:

Unable to find the controller for path "/admin/login_check". Maybe you forgot to add the matching route in your routing configuration?

堆栈跟踪告诉我: WARNING - Unable to look for the controller as the "_controller" parameter is missing

ERROR - Symfony\Component\HttpKernel\Exception\NotFoundHttpException: Unable to find the controller for path "/admin/login_check". Maybe you forgot to add the matching route in your routing configuration? (uncaught exception) at /var/www/mysite.dev/symfony/app/bootstrap.php.cache line 1419

Bin*_*lai 10

要在symfony 2XX中实现多次登录,请尝试以下代码

Security.yml

security:
    encoders:
        Symfony\Component\Security\Core\User\User: plaintext
        Company\AngularBundle\Entity\User: plaintext
        Company\AngularBundle\Entity\Admin: plaintext

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    providers:
       users:
           entity: { class: CompanyAngularBundle:User, property: username }
       admin:
           entity: { class: CompanyAngularBundle:Admin, property: username }

    firewalls:
        admin_secured_area:
            pattern:   ^/admin
            anonymous: ~
            provider: admin
            form_login:
                login_path: /admin/login
                check_path: /admin/login_check
                default_target_path: /admin

        user_secured_area:
            pattern:   ^/
            anonymous: ~
            provider: users
            form_login:
                login_path: login
                check_path: login_check
                default_target_path: /home
Run Code Online (Sandbox Code Playgroud)

使用routing.yml

login_check:
    path: /login_check
admin_login_check:
   path: /admin/login_check
Run Code Online (Sandbox Code Playgroud)

Twig文件

Action of login form should be like this
<form action="{{ path('login_check') }}" method="post">

Action of admin/login form should be like this
<form action="{{ path('admin_login_check') }}" method="post">
Run Code Online (Sandbox Code Playgroud)

  • 新手错误/陷阱:请注意,如果你有^ /和^/admin,更具体的模式(^/admin)必须高于文件中更一般的模式^ /否则它将不会优先,因为^/can仍然匹配^/admin - 至少,这是最终使我的应用程序中的工作,我在security.yml中也有一个^ /模式,我没有意识到yml如何处理正则表达式优先级. (3认同)