我已根据Symfony https://symfony.com/doc/3.4/security/custom_authentication_provider.html的官方文档中的建议创建为自定义身份验证提供程序。
然后,我对不同的防火墙使用了相同的自定义身份验证提供程序,这些防火墙具有单独的用户提供程序和不同的实体。
#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的用户详细信息。但是看来 firewall pqr 已被激活,但从上到下都在使用userprovider。
查看Symfony的AuthenticationProviderManager,它似乎遍历提供程序列表。 (第15行)
<?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;
}
//...
}
请提示我在哪里?