登录后symfony 2安全重定向

时间:2022-10-20 08:49:03

I have the next security.yml:

我有下一个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.

身份验证效果很好,而不是登录后重定向。应用程序重定向到/ _wdt / 5044c6f2a329c。如何重定向到主页?谢谢。

3 个解决方案

#1


2  

  1. You have to create a success_handler implementing the AuthenticationSuccessHandlerInterface
  2. 您必须创建一个实现AuthenticationSuccessHandlerInterface的success_handler
  3. Then you have to declare it as a service (in services.xml or services.yml)
  4. 然后你必须将它声明为一个服务(在services.xml或services.yml中)
  5. And add it to your security configuration.
  6. 并将其添加到您的安全配置中。

#2


12  

The solution is to unprotect your _wdt route.

解决方案是取消保护您的_wdt路由。

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.

您会收到此行为,因为您的_wdt路由受到保护。当页面从_wdt路由加载工具栏时,它会触发登录。您的登录表单将尝试重定向回触发登录的路由:在这种情况下_wdt。

Add this to your access_control in security.yml

将它添加到security.yml中的access_control

 - { 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.

这将使工具栏在受保护和不受保护的页面中都起作用,如果页面受到保护,那么它将是触发登录表单的页面=它将按预期工作。

#3


0  

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

此外,您可以设置always_use_default_target_path:true,然后您的default_target_path将按预期工作。查看具体信息。

#1


2  

  1. You have to create a success_handler implementing the AuthenticationSuccessHandlerInterface
  2. 您必须创建一个实现AuthenticationSuccessHandlerInterface的success_handler
  3. Then you have to declare it as a service (in services.xml or services.yml)
  4. 然后你必须将它声明为一个服务(在services.xml或services.yml中)
  5. And add it to your security configuration.
  6. 并将其添加到您的安全配置中。

#2


12  

The solution is to unprotect your _wdt route.

解决方案是取消保护您的_wdt路由。

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.

您会收到此行为,因为您的_wdt路由受到保护。当页面从_wdt路由加载工具栏时,它会触发登录。您的登录表单将尝试重定向回触发登录的路由:在这种情况下_wdt。

Add this to your access_control in security.yml

将它添加到security.yml中的access_control

 - { 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.

这将使工具栏在受保护和不受保护的页面中都起作用,如果页面受到保护,那么它将是触发登录表单的页面=它将按预期工作。

#3


0  

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

此外,您可以设置always_use_default_target_path:true,然后您的default_target_path将按预期工作。查看具体信息。