Symfony: Authentication request failed: Invalid CSRF token

只谈情不闲聊 提交于 2019-12-10 19:57:05

问题


I am getting the following error on logging in to a Symfony2 application:

[2014-06-27 00:36:22] security.INFO: Authentication request failed: Invalid CSRF token. [] []

Running on:

  • Symfony2
  • SonataUserBundle
  • Vagrant (using puppet via Puphpet.com)
  • Safari/OSX

Same setting is Working on an Ubuntu host system.

Thanks for any help.

security.yml:

firewalls:
    dev:
        pattern:  ^/(_(profiler|wdt)|css|images|js)/
        security: false

    secured_area:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            csrf_provider: form.csrf_provider
            login_path: fos_user_security_login
            check_path: fos_user_security_check
        logout:
            invalidate_session : false
            path: fos_user_security_logout
        anonymous: true
        switch_user: true

Application/Sonata/UserBundle/Resources/views/Security/login.html.twig

{% extends "FOSUserBundle::layout.html.twig" %}

{% trans_default_domain 'FOSUserBundle' %}

{% block fos_user_content %}
{% if error %}
    <div class="alert alert-danger">
        <button type="button" class="close" data-dismiss="alert">&times;</button>
        {{ error|trans }}
    </div>
{% endif %}

<form class="form-horizontal" action="{{ path("fos_user_security_check") }}" method="post">
    <fieldset>
        <legend>Login</legend>
        <input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />

        <div class="form-group">
            <label class="col-lg-2 control-label required" for="username">{{ 'security.login.username'|trans }}</label>
            <div class="col-lg-5">
                <input type="text" id="username" name="_username" placeholder="Username" value="{{ last_username }}" required="required" class="form-control" />
            </div>
        </div>

        <div class="form-group">
            <label class="col-lg-2 control-label required" for="password">{{ 'security.login.password'|trans }}</label>
            <div class="col-lg-5">
                <input type="password" id="password" name="_password" placeholder="Password" required="required" class="form-control" />
            </div>
        </div>

        <div class="form-group">
            <div class="col-lg-offset-2 col-lg-5">
                <input type="checkbox" id="remember_me" name="_remember_me" value="on" />
                <label for="remember_me">{{ 'security.login.remember_me'|trans }}</label>
            </div>
        </div>

        <div class="form-group">
            <div class="col-lg-offset-2 col-lg-5">
                <a href="{{ path("fos_user_resetting_request") }}">{{ 'resetting.request.submit'|trans }}</a>
                <input type="submit" id="_submit" name="_submit" value="{{ 'security.login.submit'|trans }}" class="btn btn-primary form-control" />
            </div>
        </div>
    </fieldset>
</form>
{% endblock fos_user_content %}

Anything else is used of the FOSUserBundle and SonataUserBundle


回答1:


I could solve my problem now. It seems to be a problem with write permissions on the session save path. If I change the configuration to use the default session save path as following:

# app/config/config.yml
framework:
    session:
        save_path: null

Then /var/lib/php/session is used instead of /var/www/myproject/app/cache/dev/sessions

The permissions on /var/www/crowdcustoms/app/cache/dev/sessions:

drwxr-xr-x   4 501 dialout    136 Jun 29 20:37 sessions/

The permissions on /var/lib/php/session

drwxrwxr-x  2 www-data www-data 4096 Jun 29 20:36 session



回答2:


You forgot the context, csrf_token('authentication') should do the trick!

Ref: http://symfony.com/doc/current/cookbook/security/csrf_in_login_form.html



来源:https://stackoverflow.com/questions/24441412/symfony-authentication-request-failed-invalid-csrf-token

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