DefaultAuthenticationFailureHandler.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Security\Http\Authentication;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\HttpKernelInterface;
  13. use Psr\Log\LoggerInterface;
  14. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  15. use Symfony\Component\Security\Core\SecurityContextInterface;
  16. use Symfony\Component\Security\Http\HttpUtils;
  17. /**
  18. * Class with the default authentication failure handling logic.
  19. *
  20. * Can be optionally be extended from by the developer to alter the behaviour
  21. * while keeping the default behaviour.
  22. *
  23. * @author Fabien Potencier <fabien@symfony.com>
  24. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  25. * @author Alexander <iam.asm89@gmail.com>
  26. */
  27. class DefaultAuthenticationFailureHandler implements AuthenticationFailureHandlerInterface
  28. {
  29. protected $httpKernel;
  30. protected $httpUtils;
  31. protected $logger;
  32. protected $options;
  33. /**
  34. * Constructor.
  35. *
  36. * @param HttpKernelInterface $httpKernel
  37. * @param HttpUtils $httpUtils
  38. * @param array $options Options for processing a failed authentication attempt.
  39. * @param LoggerInterface $logger Optional logger
  40. */
  41. public function __construct(HttpKernelInterface $httpKernel, HttpUtils $httpUtils, array $options, LoggerInterface $logger = null)
  42. {
  43. $this->httpKernel = $httpKernel;
  44. $this->httpUtils = $httpUtils;
  45. $this->logger = $logger;
  46. $this->options = array_merge(array(
  47. 'failure_path' => null,
  48. 'failure_forward' => false,
  49. 'login_path' => '/login',
  50. 'failure_path_parameter' => '_failure_path'
  51. ), $options);
  52. }
  53. /**
  54. * {@inheritDoc}
  55. */
  56. public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
  57. {
  58. if ($failureUrl = $request->get($this->options['failure_path_parameter'], null, true)) {
  59. $this->options['failure_path'] = $failureUrl;
  60. }
  61. if (null === $this->options['failure_path']) {
  62. $this->options['failure_path'] = $this->options['login_path'];
  63. }
  64. if ($this->options['failure_forward']) {
  65. if (null !== $this->logger) {
  66. $this->logger->debug(sprintf('Forwarding to %s', $this->options['failure_path']));
  67. }
  68. $subRequest = $this->httpUtils->createRequest($request, $this->options['failure_path']);
  69. $subRequest->attributes->set(SecurityContextInterface::AUTHENTICATION_ERROR, $exception);
  70. return $this->httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
  71. }
  72. if (null !== $this->logger) {
  73. $this->logger->debug(sprintf('Redirecting to %s', $this->options['failure_path']));
  74. }
  75. $request->getSession()->set(SecurityContextInterface::AUTHENTICATION_ERROR, $exception);
  76. return $this->httpUtils->createRedirectResponse($request, $this->options['failure_path']);
  77. }
  78. }