问题描述
我目前在Symfony(4.4)应用程序上有两个登录系统。
(当我评论两个关联的防火墙之一时),这两种形式都可以很好地工作。但是当他们在一起时,第二个就不起作用了。 (没有任何反应,提交表单后页面仅重新加载)
security:
encoders:
App\Entity\User:
algorithm: auto
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
# used to reload user from session & other features (e.g. switch_user)
app_user_provider:
entity:
class: App\Entity\User
property: username
app_kit_provider:
entity:
class: App\Entity\Kit
property: serial_number
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
ecert:
pattern: ^/(en|fr)/
anonymous: lazy
provider: app_kit_provider
guard:
authenticators:
- App\Security\EcertAuthenticator
logout:
path: ecert_logout
target: ecert_login
main:
pattern: ^/(en|fr)/security/
anonymous: lazy
provider: app_user_provider
guard:
authenticators:
- App\Security\LoginFormAuthenticator
logout:
path: app_logout
target: security_home
LoginFormAuthenticator:
<?PHP
namespace App\Security;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Core\Exception\InvalidCsrftokenException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Csrf\Csrftoken;
use Symfony\Component\Security\Csrf\CsrftokenManagerInterface;
use Symfony\Component\Security\Guard\Authenticator\AbstractformLoginAuthenticator;
use Symfony\Component\Security\Guard\PasswordAuthenticatedInterface;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
class LoginFormAuthenticator extends AbstractformLoginAuthenticator implements PasswordAuthenticatedInterface
{
use TargetPathTrait;
public const LOGIN_ROUTE = 'app_login';
private $entityManager;
private $urlGenerator;
private $csrftokenManager;
private $passwordEncoder;
public function __construct(EntityManagerInterface $entityManager,UrlGeneratorInterface $urlGenerator,CsrftokenManagerInterface $csrftokenManager,UserPasswordEncoderInterface $passwordEncoder)
{
$this->entityManager = $entityManager;
$this->urlGenerator = $urlGenerator;
$this->csrftokenManager = $csrftokenManager;
$this->passwordEncoder = $passwordEncoder;
}
public function supports(Request $request)
{
return self::LOGIN_ROUTE === $request->attributes->get('_route')
&& $request->isMethod('POST');
}
public function getCredentials(Request $request)
{
$credentials = [
'username' => $request->request->get('username'),'password' => $request->request->get('password'),'csrf_token' => $request->request->get('_csrf_token'),];
$request->getSession()->set(
Security::LAST_USERNAME,$credentials['username']
);
return $credentials;
}
public function getUser($credentials,UserProviderInterface $userProvider)
{
$token = new Csrftoken('authenticate',$credentials['csrf_token']);
if (!$this->csrftokenManager->isTokenValid($token)) {
throw new InvalidCsrftokenException();
}
$user = $this->entityManager->getRepository(User::class)->findOneBy(['username' => $credentials['username']]);
if (!$user) {
// fail authentication with a custom error
throw new CustomUserMessageAuthenticationException('Username Could not be found.');
}
return $user;
}
public function checkCredentials($credentials,UserInterface $user)
{
return $this->passwordEncoder->isPasswordValid($user,$credentials['password']);
}
/**
* Used to upgrade (rehash) the user's password automatically over time.
*/
public function getpassword($credentials): ?string
{
return $credentials['password'];
}
public function onAuthenticationSuccess(Request $request,TokenInterface $token,$providerKey)
{
if ($targetPath = $this->getTargetPath($request->getSession(),$providerKey)) {
return new RedirectResponse($targetPath);
}
return new RedirectResponse($this->urlGenerator->generate('create_kit'));
}
protected function getLoginUrl()
{
return $this->urlGenerator->generate(self::LOGIN_ROUTE);
}
}
EcertAuthenticator:
<?PHP
namespace App\Security;
use App\Entity\Kit;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Core\Exception\InvalidCsrftokenException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Csrf\Csrftoken;
use Symfony\Component\Security\Csrf\CsrftokenManagerInterface;
use Symfony\Component\Security\Guard\Authenticator\AbstractformLoginAuthenticator;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
class EcertAuthenticator extends AbstractformLoginAuthenticator
{
use TargetPathTrait;
public const LOGIN_ROUTE = 'ecert_login';
private $entityManager;
private $urlGenerator;
private $csrftokenManager;
public function __construct(EntityManagerInterface $entityManager,CsrftokenManagerInterface $csrftokenManager)
{
$this->entityManager = $entityManager;
$this->urlGenerator = $urlGenerator;
$this->csrftokenManager = $csrftokenManager;
}
public function supports(Request $request)
{
return self::LOGIN_ROUTE === $request->attributes->get('_route')
&& $request->isMethod('POST');
}
public function getCredentials(Request $request)
{
$credentials = [
'serial_number' => $request->request->get('serial_number'),'random_key' => $request->request->get('random_key'),$credentials['serial_number']
);
return $credentials;
}
public function getUser($credentials,$credentials['csrf_token']);
if (!$this->csrftokenManager->isTokenValid($token)) {
throw new InvalidCsrftokenException();
}
$user = $this->entityManager->getRepository(Kit::class)->findOneBy(['serial_number' => $credentials['serial_number']]);
if (!$user) {
// fail authentication with a custom error
throw new CustomUserMessageAuthenticationException('Serial_number Could not be found.');
}
return $user;
}
public function checkCredentials($credentials,UserInterface $user)
{
if ($this->entityManager->getRepository(Kit::class)->findOneBy(['serial_number' => $credentials['serial_number'],'random_key' => $credentials['random_key']])) {
return true;
}
return false;
}
public function onAuthenticationSuccess(Request $request,$providerKey)) {
return new RedirectResponse($targetPath);
}
return new RedirectResponse($this->urlGenerator->generate('ecert'));
}
protected function getLoginUrl()
{
return $this->urlGenerator->generate(self::LOGIN_ROUTE);
}
}
你知道为什么吗?
谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)