Symfony 2即使在错误的密码上也会保留_target_path

时间:2012-09-08 10:16:16

标签: security symfony

使用Symfony 2.10进行身份验证时,如果我按照sercurity.yml中的配置发布登录检查请求:

form_login:
    login_path: /account/login
    check_path: /account/login_check

在表单上正确设置了_target_path。

但是如果用户现在键入了错误的密码,_target_path就会丢失,当用户现在键入正确的密码时,它将被重定向到默认位置,在我的情况下是首页。

我希望安全处理程序一直保持_target_path,即使用户输入了错误的密码。

感谢任何帮助:)

3 个答案:

答案 0 :(得分:2)

我只找到一种方法:为form_login创建新的failure_handler

添加你的security.yml:

form_login
    failure_handler: some_failure_handler_id

在那项服务中:

public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
    ...

    $target_path = $request->request->get('_target_path');
    if ($target_path) {
        $request->getSession()->set('_security.login.target_path', $target_path);
    }

    ...
}

在SecurityController中:

if ($back_url = $session->get('_security.login.target_path')) {
    $session->remove('_security.login.target_path');
}

并在表单中再次设置target_path:

{% if back_url %}                               
    <input type="hidden" name="_target_path" value="{{ back_url }}" />
{% endif %}

答案 1 :(得分:0)

尝试将use_referer设为true [see reference]:

form_login:
    ...
    use_referer: true

答案 2 :(得分:0)

为确保您的_target_path不会被删除 - 您可以将其作为当前的uri在twig模板中设置:

<input type="hidden" value="{{ app.request.uri }}" name="_target_path" />
相关问题