gpt4 book ai didi

authentication - Symfony 4 中的 Guard Authenticator

转载 作者:行者123 更新时间:2023-12-04 00:04:26 27 4
gpt4 key购买 nike

我正在 Symfony 4 中创建一个简单的登录身份验证系统并使用安全组件 Guard。我的 FormLoginAuthenticator 如下:

<?php
namespace App\Security;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
use Symfony\Component\Security\Core\Security;

class FormLoginAuthenticator extends AbstractFormLoginAuthenticator
{
private $router;
private $encoder;

public function __construct(RouterInterface $router, UserPasswordEncoderInterface $encoder)
{
$this->router = $router;
$this->encoder = $encoder;
}

public function getCredentials(Request $request)
{
if ($request->getPathInfo() != '/login_check') {
return;
}

$email = $request->request->get('_email');
$request->getSession()->set(Security::LAST_USERNAME, $email);
$password = $request->request->get('_password');

return [
'email' => $email,
'password' => $password,
];
}

public function getUser($credentials, UserProviderInterface $userProvider)
{
$email = $credentials['email'];

return $userProvider->loadUserByUsername($email);
}

public function checkCredentials($credentials, UserInterface $user)
{
$plainPassword = $credentials['password'];
if ($this->encoder->isPasswordValid($user, $plainPassword)) {
return true;
}

throw new BadCredentialsException();
}

public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
$url = $this->router->generate('welcome');

return new RedirectResponse($url);
}

public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
$request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);

$url = $this->router->generate('login');

return new RedirectResponse($url);
}

protected function getLoginUrl()
{
return $this->router->generate('login');
}

protected function getDefaultSuccessRedirectUrl()
{
return $this->router->generate('welcome');
}

public function supportsRememberMe()
{
return false;
}
}

但它显示以下错误:

(1/1) fatal error 异常错误:App\Security\FormLoginAuthenticator 类包含 1 个抽象方法,因此必须声明为抽象方法或实现其余方法 (Symfony\Component\Security\Guard\AuthenticatorInterface::supports)

能否告诉我这个类的哪个抽象方法导致了这个错误?

最佳答案

在 FormLoginAuthenticator 类中写入:

public function supports(Request $request)
{
return 'app_login' === $request->attributes->get('_route')
&& $request->isMethod('POST');
}

“app_login”是名称 Controller 登录

关于authentication - Symfony 4 中的 Guard Authenticator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51460188/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com