简体   繁体   中英

Using multiple firewalls cause ERR_TOO_MANY_REDIRECTS in Symfony 2

I'm using symfony 2.3.4 and I tried to set up multiple firewalls. But now everytime I go to /admin/login there's error ERR_TOO_MANY_REDIRECTS.These are my routing.yml and security.yml files:

routing.yml

login_admin:
    pattern:   /admin/login/
    defaults:  { _controller: HerbanistAdminBundle:Security:login }

login_check_admin:
    pattern:   /admin/login_check/

logout_admin:
    path:   /admin/logout/

login_customer:
    pattern:   /customer/login/
    defaults:  { _controller: HerbanistStoreBundle:Security:login }

login_check_customer:
    pattern:   /customer/login_check/

logout_customer:
    path:   /customer/logout/

security.yml

security:
    encoders:
        Symfony\Component\Security\Core\User\User: plaintext

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

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

    firewalls:
        admin_secured_area:
            pattern: ^/admin
            form_login:
                check_path: /admin/login_check
                login_path: /admin/login
                always_use_default_target_path: true
                default_target_path: /admin
            logout:
                path:   /admin/logout
                target: /admin
        customer_secured_area:
            pattern:    ^/customer
            form_login:
                check_path: /customere/login_check
                login_path: /customer/login
                always_use_default_target_path: true
                default_target_path: /customer
            logout:
                path:   /customer/logout
                target: /customer

    access_control:
        - { path: ^/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin, roles: ROLE_ADMIN }
        - { path: ^/customer, roles: ROLE_USER }

Edit

Debug messages in Profiler:

DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest".
INFO - Matched route "login_admin" (parameters: "_controller": "Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction", "path": "/admin/login/", "permanent": "true", "scheme": "null", "httpPort": "80", "httpsPort": "443", "_route": "login_admin")
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest".
DEBUG - Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest".
DEBUG - Notified event "kernel.exception" to listener "Symfony\Component\Security\Http\Firewall\ExceptionListener::onKernelException".
INFO - Authentication exception occurred; redirecting to authentication entry point (A Token was not found in the SecurityContext.)
DEBUG - Calling Authentication entry point
DEBUG - Listener "Symfony\Component\Security\Http\Firewall\ExceptionListener::onKernelException" stopped propagation of the event "kernel.exception".
DEBUG - Listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelException" was not called for event "kernel.exception".
DEBUG - Listener "Symfony\Component\HttpKernel\EventListener\ExceptionListener::onKernelException" was not called for event "kernel.exception".
DEBUG - Notified event "kernel.response" to listener "Symfony\Component\Security\Http\Firewall\ContextListener::onKernelResponse".
DEBUG - Write SecurityContext in the session
DEBUG - Notified event "kernel.response" to listener "Symfony\Bridge\Monolog\Handler\FirePHPHandler::onKernelResponse".
DEBUG - Notified event "kernel.response" to listener "Symfony\Bridge\Monolog\Handler\ChromePhpHandler::onKernelResponse".
DEBUG - Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ResponseListener::onKernelResponse".
DEBUG - Notified event "kernel.response" to listener "Symfony\Component\Security\Http\RememberMe\ResponseListener::onKernelResponse".
DEBUG - Notified event "kernel.response" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\CacheListener::onKernelResponse".
DEBUG - Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelResponse".

Add anonymous: ~ to both of your firewalls and force the required roles in the access_control section. Read the Security chapter for more information.

The conflict was that paths in routing.yml and security.yml are not in exactly the same URL format. In routing.yml they are ending with '/', but in security.yml they don't. So the solution is to end each path with '/'. And also to add anonymous: ~ to both firewalls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM