我只找到一种方法:为 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);
}
...
}
在安全控制器中:
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 %}