Php Symfony自定义身份验证提供程序,具有多个防火墙
我已经按照Symfony官方文档中的建议创建了自定义身份验证提供商 然后,我为不同的防火墙使用相同的自定义身份验证提供程序,这些防火墙具有具有不同实体的独立用户提供程序Php Symfony自定义身份验证提供程序,具有多个防火墙,php,symfony,symfony-3.4,Php,Symfony,Symfony 3.4,我已经按照Symfony官方文档中的建议创建了自定义身份验证提供商 然后,我为不同的防火墙使用相同的自定义身份验证提供程序,这些防火墙具有具有不同实体的独立用户提供程序 #app/config/security.yml #... providers: abcuser: id: App\Bundle\AbcBundle\Security\UserProvider xyzuser: id: App\Bundle\XyzBundle\Security\
#app/config/security.yml
#...
providers:
abcuser:
id: App\Bundle\AbcBundle\Security\UserProvider
xyzuser:
id: App\Bundle\XyzBundle\Security\UserProvider
pqruser:
id: App\Bundle\PqrBundle\Security\UserProvider
firewalls:
api_token:
pattern: ^/((api/v1/abc/auth/token)|(api/v1/xyz/auth/token)|(api/v1/pqr/auth/token))$
stateless: true
anonymous: true
api_abc:
pattern: ^/api/v1/abc
stateless: true
provider: abcuser
custom_auth: true
api_xyz:
pattern: ^/api/v1/xyz
stateless: true
provider: xyzuser
custom_auth: true
api_pqr:
pattern: ^/api/v1/pqr
stateless: true
provider: pqruser
custom_auth: true
#...
现在,当我使用api/v1/pqr/auth/token
生成访问令牌,并使用api/v1/pqr/details
请求api来获取pqr的用户详细信息时
假设我在pqr上有一个用户名为apple
的用户,并且在abc上存在相同的用户名,那么它将从abc返回apple的用户详细信息。但看起来,防火墙pqr正在被激活,但userprovider正在自上而下地使用
查看Symfony的AuthenticationProviderManager,它似乎遍历了提供者列表(行号15)
我没有将custom\u auth
视为Symfony选项:这是您添加的内容吗?custom\u auth是我在Symfony中默认创建的自定义身份验证提供程序
<?php
// ...
namespace Symfony\Component\Security\Core\Authentication;
class AuthenticationProviderManager implements AuthenticationManagerInterface
{
// ...
public function authenticate(TokenInterface $token)
{
$lastException = null;
$result = null;
// this might be issue
foreach ($this->providers as $provider) {
if (!$provider instanceof AuthenticationProviderInterface) {
throw new \InvalidArgumentException(sprintf('Provider "%s" must implement the AuthenticationProviderInterface.', get_class($provider)));
}
if (!$provider->supports($token)) {
continue;
}
try {
$result = $provider->authenticate($token);
if (null !== $result) {
break;
}
} catch (AccountStatusException $e) {
$lastException = $e;
break;
} catch (AuthenticationException $e) {
$lastException = $e;
}
}
if (null !== $result) {
if (true === $this->eraseCredentials) {
$result->eraseCredentials();
}
if (null !== $this->eventDispatcher) {
$this->eventDispatcher->dispatch(AuthenticationEvents::AUTHENTICATION_SUCCESS, new AuthenticationEvent($result));
}
return $result;
}
if (null === $lastException) {
$lastException = new ProviderNotFoundException(sprintf('No Authentication Provider found for token of class "%s".', get_class($token)));
}
if (null !== $this->eventDispatcher) {
$this->eventDispatcher->dispatch(AuthenticationEvents::AUTHENTICATION_FAILURE, new AuthenticationFailureEvent($token, $lastException));
}
$lastException->setToken($token);
throw $lastException;
}
//...
}