Search code examples
securityloopssymfonyconfigurationfosuserbundle

FosUserBundle how to fix redirect loop


Hi i have redirect loop after login useing fosuserbundle how it looks? i type good username and password then it log me in i can see it with symfony profiller and the profiler says that This request redirects to http://web.dev/app_dev.php/admin/panel. i go there and it says This request redirects to http://web.dev/app_dev.php/admin/login. and im no longer authenticated in this place

here is my security:

jms_security_extra:
    expressions: true

security:
    encoders:
        Symfony\Component\Security\Core\User\User: plaintext
        FOS\UserBundle\Model\UserInterface: sha512
        MainBundle\Security\User\SsoUser: sha512

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

    providers:
        in_memory:
            memory:
                users:
                    "%api_username%":  { password: "%api_password%", roles: [ 'ROLE_API' ] }     
        fos_userbundle:
            id: fos_user.user_provider.username_email
        ssoprovider:
            id: sso_user_provider

    firewalls:
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false
        login:
            pattern:  ^/demo/secured/login$
            security: false
        admin:
            pattern: ^/admin/
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
                login_path:                       fos_user_security_login
                check_path:                       fos_user_security_check
                default_target_path:              admin_main
            logout:    
                path: fos_user_security_logout   
                invalidate_session: true #https://github.com/symfony/symfony/issues/5868
            anonymous:    true
            remember_me:
                name:     "web_remember_me"
                key:      "%secret%"
                lifetime: 864000 # 10 days
                path:     /admin
        secured_area:
            pattern: ^/(?!admin)            
            form_login: 
                login_path : main_user_logout
            stateless: false
            anonymous: false
            simple_preauth:
                authenticator: sso_authenticator 
            logout:
                path:   main_user_logout
                invalidate_session: true 
                success_handler: sso_authentication_handler 
        api:
            pattern:    ^/api
            stateless:  true
            http_basic:
                realm: "Secured API Area"

    access_control:
        - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, role: ROLE_ADMIN }
        - { path: ^/api/, roles: ROLE_API }
        - { path: ^/, role: ROLE_USER }

Solution

  • It was caused because I had defined in my config.yml cookie_domain

    config yml:

    framework:
        session:
          cookie_domain: .%domain% 
    

    and I had to add for remember me domain configuration

    security.yml:

    admin:
        remember_me:
            domain:   ".%domain%"