[英]Symfony2: Redirect path on login failure being overridden by SonataAdmin login
I am using 2 login forms, one for the user and one for Sonata Admin. 我正在使用2个登录表单,一个用于用户,一个用于Sonata Admin。
The problem is when the user attempts to login and fails, the re-direct goes to the Sonata Admin login route and does not stay on the user login route/page. 问题是,当用户尝试登录并失败时,重定向将转到Sonata Admin登录路径,而不停留在用户登录路径/页面上。
I've looked at the documentation and tried to use failure_path
but it still defaults back to the Sonata Admin login route. 我查看了文档并尝试使用
failure_path
但它仍默认返回到Sonata Admin登录路径。
It seems Sonata Admin bundle is overriding the re-direct path. 似乎Sonata Admin捆绑包覆盖了重定向路径。 I tried changing the order in the security.yml so the user section comes up first but that still doesn't fix the problem.
我尝试在security.yml中更改顺序,以便首先显示用户部分,但仍不能解决问题。
How can I fix this? 我怎样才能解决这个问题?
security.yml security.yml
firewalls:
admin:
pattern: ^/
form_login:
check_path: /login_check
login_path: /login
logout:
path: /logout
target: index
anonymous: ~
user:
pattern: ^/user
form_login:
# always_use_default_target_path: true
# default_target_path: login_form
failure_path: /user/login
check_path: /user/login_check
login_path: /user/login
logout:
path: /user/logout
target: index
anonymous: ~
1) Create an authentication handler 1)创建一个身份验证处理程序
<?php
namespace Company\Bundle\Handler;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Router;
class SecurityHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface
{
private $router;
public function __contruct(Router $router)
{
$this->router = $router;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
// only an example, make your own logic here
$referer = $request->headers->get('referer');
if (empty($referer)) {
return new RedirectResponse($this->router->generate('homepage'));
} else {
return new RedirectResponse($referer);
}
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
// Edit it to meet your requeriments
$request->getSession()->set('login_error', $error);
return new \Symfony\Component\HttpFoundation\RedirectResponse($this->router->generate('login_route'));
}
}
2) Register it as service 2)注册为服务
# src/Company/Bundle/Resources/config/services.yml
security_handler:
class: Company\Bundle\Handler\SecurityHandler
arguments: [@router]
3) Config to use this service as handler for login success and login failure, also you can use it in the admin
firewall 3)配置为使用此服务作为登录成功和登录失败的处理程序,也可以在
admin
防火墙中使用它
# app/config/security.yml
firewalls:
admin:
pattern: ^/
form_login:
check_path: /login_check
login_path: /login
success_handler: security_handler
failure_handler: security_handler
logout:
path: /logout
target: index
anonymous: ~
user:
pattern: ^/user
form_login:
# always_use_default_target_path: true
# default_target_path: login_form
failure_path: /user/login
check_path: /user/login_check
login_path: /user/login
success_handler: security_handler
failure_handler: security_handler
logout:
path: /user/logout
target: index
anonymous: ~
You should configure you configurations from specific to general , because of general configuration can match any specific one.. So, try to change the order of your firewalls like this: 您应该将配置从特定配置配置为一般 ,因为一般配置可以匹配任何特定配置。因此,请尝试按以下方式更改防火墙的顺序:
firewalls:
user:
pattern: ^/user
form_login:
# always_use_default_target_path: true
# default_target_path: login_form
failure_path: /user/login
check_path: /user/login_check
login_path: /user/login
success_handler: security_handler
failure_handler: security_handler
logout:
path: /user/logout
target: index
anonymous: ~
admin:
pattern: ^/
form_login:
check_path: /login_check
login_path: /login
success_handler: security_handler
failure_handler: security_handler
logout:
path: /logout
target: index
anonymous: ~
I came across a similar problem, and this is my solution. 我遇到了类似的问题,这是我的解决方案。 I also created a custom authentication handler:
我还创建了一个自定义身份验证处理程序:
class LoginFailureHandler extends DefaultAuthenticationFailureHandler implements AuthenticationFailureHandlerInterface
{
public function __construct(HttpKernelInterface $httpKernel, HttpUtils $httpUtils, array $options, $logger = null)
{
parent::__construct($httpKernel, $httpUtils, $options, $logger);
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
$request->getSession()->set('_security.user_area.target_path', $request->get('redirect_url'));
return parent::onAuthenticationFailure($request, $exception);
}
}
I used the _security.user_area.target_path
parameter for resetting the redirect_url if the authentication fails. 如果身份验证失败,我使用
_security.user_area.target_path
参数来重置redirect_url。 Works perfectly! 完美的作品!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.