Php 未获取symfony中authenticationUtils的身份验证错误
我在Symfony 4中登录我的应用程序时遇到身份验证错误问题。 我可以与所有用户完美地登录,但当我试图故意登录失败时,我不会收到任何错误消息,$error也不会填充。这里我发布了我的LoginFormAuthenticator和securityController的代码。 LoginFormAuthenticatorPhp 未获取symfony中authenticationUtils的身份验证错误,php,symfony,authentication,error-handling,symfony4,Php,Symfony,Authentication,Error Handling,Symfony4,我在Symfony 4中登录我的应用程序时遇到身份验证错误问题。 我可以与所有用户完美地登录,但当我试图故意登录失败时,我不会收到任何错误消息,$error也不会填充。这里我发布了我的LoginFormAuthenticator和securityController的代码。 LoginFormAuthenticator class LoginFormAuthenticator extends AbstractFormLoginAuthenticator { /** *
class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
{
/**
* @var UserRepository
*/
private $userRepository;
/**
* @var RouterInterface
*/
private $router;
/**
* @var CsrfTokenManagerInterface
*/
private $csrfTokenManager;
/**
* @var UserPasswordEncoderInterface
*/
private $passwordEncoder;
public function __construct(UserRepository $userRepository, RouterInterface $router, CsrfTokenManagerInterface $csrfTokenManager, UserPasswordEncoderInterface $passwordEncoder)
{
$this->userRepository = $userRepository;
$this->router = $router;
$this->csrfTokenManager = $csrfTokenManager;
$this->passwordEncoder = $passwordEncoder;
}
public function supports(Request $request)
{
return $request->attributes->get('_route') === 'app_login'
&& $request->isMethod('POST');
}
public function getCredentials(Request $request)
{
$credentials = [
'cargo' => $request->request->get('cargo'),
'password' => $request->request->get('password'),
'csrf_token' => $request->request->get('_csrf_token'),
];
$request->getSession()->set(
Security::LAST_USERNAME,
$credentials['cargo']
);
return $credentials;
}
public function getUser($credentials, UserProviderInterface $userProvider)
{
$token = new CsrfToken('authenticate', $credentials['csrf_token']);
if (!$this->csrfTokenManager->isTokenValid($token)) {
throw new InvalidCsrfTokenException();
}
return $this->userRepository->findOneBy(['cargo' => $credentials['cargo']]);
}
public function checkCredentials($credentials, UserInterface $user)
{
return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
// todo
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
return new RedirectResponse($this->router->generate('app_homepage'));
}
public function start(Request $request, AuthenticationException $authException = null)
{
// todo
}
public function supportsRememberMe()
{
// todo
}
protected function getLoginUrl()
{
return $this->router->generate('app_login');
}
}
登录功能:
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils)
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
]);
}
登录HTML小枝
<form class="form-signin" method="post">
{% if error %}
<div class="alert alert-danger">{{ error.messageKey|trans(error.messageData, 'security') }}</div>
<h3>{{ error.messageKey }}</h3>
{% endif %}
{%if错误%}
{{error.messageKey | trans(error.messageData,'security')}
{{error.messageKey}}
{%endif%}
谢谢 onAuthenticationFailure方法为空。请参阅parent::onAuthenticationFailure。
方法应该返回响应对象。只需注释掉onAuthenticationFailure方法,让父类处理它。如果您确实有编写自己的代码的冲动,那么请检查父代码,看看它需要做什么。