Symfony 2 security redirection after login

I have the following 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 following 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 redirecting after logging in. The application redirects to / _ wdt / 5044c6f2a329c . How can I redirect to the home page? Thanks.

+4
source share
3 answers
  • You need to create success_handler by implementing AuthenticationSuccessHandlerInterface
  • Then you should declare it as a service (in services.xml or services.yml).
  • And add it to the security configuration
+2
source

The solution is to remove protection from your _wdt route.

You get this behavior because your _wdt route is protected. When the page loads the toolbar from the _wdt route, it launches the login. Your login form will try to redirect back to the route that the login called: in this case _wdt.

Add this to your access_control in security.yml

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

This will allow the toolbar to work on both protected and unprotected pages, and if the page is protected, it will be a page that launches the login form = it will work as expected.

+12
source

Alternatively, you can set always_use_default_target_path: true , and then your default_target_path will work as you expect. See more details.

0
source

Source: https://habr.com/ru/post/1432106/


All Articles