Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony 2 security redirect after login

Tags:

php

symfony

acl

I have the next security.yml:

security:
    encoders:
        Test\BackEndBundle\Entity\User:
            algorithm: sha512
            encode-as-base64: true
            iterations: 10

    providers:
        main:
            entity: { class: TestBackEndBundle:User, property: username }

    firewalls:
        main:
            pattern: /.*
            form_login:
                check_path: _security_check
                login_path: _security_login
                default_target_path: homepage
            logout: true
            security: true
            anonymous: true

    access_control:
        - { path: ^/service, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/login, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: /.*, roles: {ROLE_PARTNER, ROLE_ADMIN} }

And the next routing:

homepage:
    pattern:  /
    defaults: { _controller: TestBackEndBundle:Default:index }

_security_login:
    pattern:  /login
    defaults: { _controller: TestBackEndBundle:Security:login }

_security_check:
    pattern:  /login_check

_security_logout:
    pattern:  /logout

Authentication works well, instead of redirection after login. Application redirects to /_wdt/5044c6f2a329c. How can I make redirect to the home page? Thanks.

like image 624
Alex Pliutau Avatar asked Dec 09 '22 21:12

Alex Pliutau


2 Answers

The solution is to unprotect your _wdt route.

You get this behaviour because your _wdt route is protected. When a page loads the toolbar from the _wdt route, it triggers the login. Your login form will try to redirect back to the route that triggered the login: In this case _wdt.

Add this to your access_control in security.yml

 - { path: ^/_wdt, roles: 'IS_AUTHENTICATED_ANONYMOUSLY' }

This will let the toolbar work in both protected and unprotected pages, and if the page is protected, it will be the page the one who triggers the login form = it will work as expected.

like image 168
JMerino Avatar answered Dec 27 '22 02:12

JMerino


  1. You have to create a success_handler implementing the AuthenticationSuccessHandlerInterface
  2. Then you have to declare it as a service (in services.xml or services.yml)
  3. And add it to your security configuration.
like image 26
i.am.michiel Avatar answered Dec 27 '22 00:12

i.am.michiel