FOSUserBundle 从 symfony 2.4 升级到 2.6 后无法登录


FOSUserBundle no login possible after upgrade from symfony 2.4 to 2.6

最近我一直在将我的网站从symfony 2.4升级到2.6。我遵循了我找到的所有升级说明,几乎所有作品都找到了。但是我现在无法再登录我的网站了。我使用FOSUserBundle,我的composer.json包括这一行:

"friendsofsymfony/user-bundle": "2.0.*@dev",

我至少阅读了所有教程三遍,比较了所有选项,现在我已经拔掉了头发,因为我找不到有效的解决方案!

在 2.4 中一切正常,所以我认为这可能与 security.yml 中定义的选项有关。

问题是我在apache/symfony日志文件中没有得到任何异常,警告或任何可疑的东西。这是我的安全.yml:

parameters:
    security.acl.permission.map.class:
      Sonata'AdminBundle'Security'Acl'Permission'AdminPermissionMap
    sonata.admin.security.mask.builder.class:
      Sonata'AdminBundle'Security'Acl'Permission'MaskBuilder
security:
    acl:
        connection: default
    encoders:
        FOS'UserBundle'Model'UserInterface: sha512
    access_denied_url:    ~
    always_authenticate_before_granting:  false
    role_hierarchy:
        ROLE_ADMIN:       [ROLE_USER, ROLE_SONATA_ADMIN]
        ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
        ROLE_AUTHOR:      [ROLE_USER, SONATA]
        SONATA:
          - ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT
    providers:
        fos_userbundle:
            id: fos_user.user_provider.username
        fos_facebook_provider:
            id: fos_facebook.user.login
        chain_provider:
          chain:
            providers: [fos_userbundle, fos_facebook_provider]
    firewalls:
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
          pattern:   ^/
          fos_facebook:
            app_url: "http://apps.facebook.com/XXX/"
            server_url: "http://XXX"
            login_path: /user/login
            check_path: /facebook/login_check
            default_target_path: /Account
            provider: fos_facebook_provider
          form_login:
            provider: fos_userbundle
            csrf_provider: form.csrf_provider
            remember_me: true
            login_path:     /user/login
            default_target_path: /Account
            use_forward:    false
            check_path:     /user/login_check
            failure_path:   null
          logout:
            path: /user/logout
          anonymous: true
          provider: main
          context: main_auth
          remember_me:
            key: XXX
            lifetime: XXX
            path: /
            domain: ~
    access_control:
      - { path: ^/user/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/secured, role: ROLE_USER }
      - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
#      # The WDT has to be allowed to anonymous users to avoid requiring the login with the AJAX request
      - { path: ^/wdt/, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/profiler/, role: IS_AUTHENTICATED_ANONYMOUSLY }
##      # AsseticBundle paths used when using the controller for assets
      - { path: ^/js/, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/css/, role: IS_AUTHENTICATED_ANONYMOUSLY }
#      # URL of FOSUserBundle which need to be available to anonymous users
#      - { path: ^/secured/.*, role: [ROLE_FACEBOOK] } # This is the route secured with fos_facebook
#      - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY } # for the case of a failed login
      - { path: ^/user/new$, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/user/check-confirmation-email$, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/user/confirm/, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/user/confirmed$, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/user/request-reset-password$, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/user/send-resetting-email$, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/user/check-resetting-email$, role: IS_AUTHENTICATED_ANONYMOUSLY }
      - { path: ^/user/reset-password/, role: IS_AUTHENTICATED_ANONYMOUSLY }
#      - { path: ^/secured/.*, role: [IS_AUTHENTICATED_FULLY] } # This is the route secured with fos_facebook
#      # Secured part of the site
      # This config requires being logged for the whole site and having the admin role for the admin part.
      # Change these rules to adapt them to your needs
      - { path: ^/admin/.*, role: [ROLE_ADMIN, ROLE_SONATA_ADMIN, ROLE_AUTHOR] }
#      - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }

输入凭据后,我被重定向到login_check网址。在此请求中,我可以看到"302"重定向回到我的登录页面。但我认为这很正常。之后,我将被重定向到我的登录页面,而无需进行身份验证。

您是否在我的配置中看到任何错误?如果您需要更多代码,请告诉我!


app.yml 中的 FOS 配置

fos_user:
    db_driver: orm
    firewall_name: main
    user_class: XXX'UserBundle'Entity'User
    group:
        group_class: XXX'UserBundle'Entity'Group
    service:
      mailer: fos_user.mailer.twig_swift
      user_manager: fos_user.user_manager.default
    registration:
      form:
        type: my_user_registration
      confirmation:
        enabled: true
        template: MyBundle:Mail:registration.email.twig

树枝中的登录表单

<form class="login_formular" action="{{ path("fos_user_security_check") }}" method="post">
    <input type="hidden" name="_csrf_token" value="{{ csrf_token }}"/>
    <table>
        <tr>
            <td><label for="username">{{ 'security.login.username'|trans({}, 'FOSUserBundle') }}</label></td>
            <td><input class="login_input" type="text" id="username" name="_username" value="{{ last_username }}"
                       required="required"/></td>
        </tr>
        <tr>
            <td><label for="password">{{ 'security.login.password'|trans({}, 'FOSUserBundle') }}</label></td>
            <td><input class="login_input" type="password" id="password" name="_password" required="required"/></td>
        </tr>
        <tr>
            <td><label class="login_input" for="remember_me">Eingeloggt bleiben:</label></td>
            <td><input type="checkbox" id="remember_me" name="_remember_me" value="on" style="margin-top: 5px;"/>
            </td>
        </tr>
        {% if error %}
            <tr>
                <td colspan="2" id="login_form_errors">{{ error.messageKey|trans(error.messageData, 'security') }}</td>
            </tr>
        {% endif %}
        <tr>
            <td colspan="2">
                <input type="submit" id="_submit" name="_submit"
                       value="{{ 'security.login.submit'|trans({}, 'FOSUserBundle') }}"/>
            </td>
        </tr>
    </table>
    <div class="clear"></div>
</form>

我发现了问题和解决方案,但我对发现它的方式感到失望。

我的问题描述缺少导致错误的 config.yml。

这是我配置中的错误部分:

framework:
    session:
      save_path: %kernel.root_dir%/var/sessions
      cookie_httponly: true

由于某些原因,我更改了save_path设置并激活了过去cookie_httponly。在我看来,这种组合使框架无法保存任何与客户端相关的数据。

但是,我在symfony或apache日志中找不到任何错误,指出save_path下的路径是错误的并且不存在。只有这条消息才能节省我几个小时的搜索时间。最后,我最终在我的项目 git 历史记录中搜索了可能导致问题的配置和源代码中的任何更改。

此配置现在正在工作:

framework:
    session:
      save_path: ~
#      cookie_httponly: true

如果有人确切地知道为什么这些参数阻止登录,我将欢迎任何评论!

我认为您正在 security.yml 中使用

{
encoders:
    FOS'UserBundle'Model'UserInterface:
        algorithm: sha512
        encode_as_base64: false
        iterations: 1
}

评论encode_as_base64iterations,然后它将解决。