symfony2 : using referer after login with fosuserbundle

一个人想着一个人 提交于 2019-12-05 21:56:36

问题


I've got problem with fosuserbundle to redirect user to the referer after login success

app/config/security.yml

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username_email

    firewalls:
        main:
            pattern: ^/

            form_login:
                provider: fos_userbundle
                login_path:     fos_user_security_login
                use_referer:                    true
                check_path:     fos_user_security_check
                csrf_provider: form.csrf_provider
            logout:
                path: fos_user_security_logout
            anonymous:    true

    access_control:
        - { path: ^/%locale%/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }

And config.yml:

fos_user:
    db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
    firewall_name: main
    user_class: Acme\UserBundle\Entity\User

    profile:
      form:
          type: Acme_user_registration
    registration:
      confirmation: { enabled: true }
      form:
          type: Acme_user_registration
      confirmation:
          enabled:    true
          from_email:
              address:        webmaster@acme.com
              sender_name:    administrateur de Acme.com
    resetting:
        token_ttl: 86400
        email:
            from_email: # Use this node only if you don't want the global email address for the resetting email
              address:        webmaster@Acme.com
              sender_name:    administrateur de Acme.com

unfortunately when user is logged successfully, he is invariably redirect to the homepage, in spite of "use_referer: true" in my security.yml config file...

Will someone please to help me ?


回答1:


Ok i found a solution. In the login form i added an hidden field "_target_path" and give it the value of the referer...it seems to work fine

{% if app.request.get('_route') != app.request.headers.get('referer') %}    
    <input type="hidden" name="_target_path" value="{{ app.request.headers.get('referer') }}" />    
{% endif %}



回答2:


This works for me with symfony 3.2,
while the referer-header solution didn't.

security.yml

 firewalls:
    main:
        form_login:
            use_referer: true

login.html.twig

<input type="hidden" 
       name="_target_path" 
       value="{{ app.session.get("_security.main.target_path") }}">


来源:https://stackoverflow.com/questions/20879164/symfony2-using-referer-after-login-with-fosuserbundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!