登录Symfony2应用程序时出现以下错误:
[2014-06-27 00:36:22] security.INFO:身份验证请求失败:无效的CSRF令牌。 [] []
继续:
相同的设置是在Ubuntu主机系统上工作。
感谢您的帮助。
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
应用程序/奏/ UserBundle /资源/视图/安全性/ 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">×</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 %}
FOSUserBundle和SonataUserBundle使用了其他任何东西
答案 0 :(得分:7)
我现在可以解决我的问题了。这似乎是会话保存路径上的写权限问题。如果我更改配置以使用默认会话保存路径,如下所示:
# app/config/config.yml
framework:
session:
save_path: null
然后使用/var/lib/php/session
代替/var/www/myproject/app/cache/dev/sessions
/var/www/crowdcustoms/app/cache/dev/sessions
上的权限:
drwxr-xr-x 4 501 dialout 136 Jun 29 20:37 sessions/
/var/lib/php/session
drwxrwxr-x 2 www-data www-data 4096 Jun 29 20:36 session
答案 1 :(得分:0)
你忘记了上下文,csrf_token('authentication')
应该做的伎俩!
参考:http://symfony.com/doc/current/cookbook/security/csrf_in_login_form.html