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 __construct(Router $router)
{
$this->router = $router;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
$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) 配置 HWIO 将此服务用作处理程序
# app/config/security.yml
firewalls:
# ....
you_firewall:
oauth:
# ....
resource_owners:
# ....
success_handler: security_handler
failure_handler: security_handler # optional