symfony2:使用fosuserbundle登录后使用referer

时间:2014-01-02 08:41:31

标签: symfony login fosuserbundle referer

我遇到fosuserbundle在登录成功后将用户重定向到引用者的问题

的应用程序/配置/ 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 }

和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

不幸的是,当用户成功登录时,他总是会重定向到主页,尽管我的security.yml配置文件中有“use_referer:true”...

有人请帮助我吗?

3 个答案:

答案 0 :(得分:14)

好的,我找到了解决方案。在登录表单中,我添加了一个隐藏字段“_target_path”,并为其提供引用的值...它似乎工作正常

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

答案 1 :(得分:1)

这适用于我的symfony 3.2,
虽然referer-header解决方案没有。

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") }}">

答案 2 :(得分:0)

Symfony2.5:

        form_login:
            provider:       fos_userbundle
            login_path:     fos_user_security_login
            use_forward:    false
            check_path:     fos_user_security_check
            failure_path:   null
            always_use_default_target_path: false
            default_target_path: XXXXXXX_OF_YOUR_CHOICE

这对我有用