[英]Symfony2: Redirect path on login failure being overridden by SonataAdmin login
我正在使用2个登录表单,一个用于用户,一个用于Sonata Admin。
问题是,当用户尝试登录并失败时,重定向将转到Sonata Admin登录路径,而不停留在用户登录路径/页面上。
我查看了文档并尝试使用failure_path
但它仍默认返回到Sonata Admin登录路径。
似乎Sonata Admin捆绑包覆盖了重定向路径。 我尝试在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)创建一个身份验证处理程序
<?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)注册为服务
# src/Company/Bundle/Resources/config/services.yml
security_handler:
class: Company\Bundle\Handler\SecurityHandler
arguments: [@router]
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: ~
您应该将配置从特定配置配置为一般 ,因为一般配置可以匹配任何特定配置。因此,请尝试按以下方式更改防火墙的顺序:
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: ~
我遇到了类似的问题,这是我的解决方案。 我还创建了一个自定义身份验证处理程序:
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);
}
}
如果身份验证失败,我使用_security.user_area.target_path
参数来重置redirect_url。 完美的作品!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.