OR关系中的多个Guard身份验证器

Ian*_*ips 4 authentication symfony

我正在使用Symfony 2.8的新Guard身份验证系统,我想允许用户使用两种方法之一进行身份验证.所以我已经为两者实现了Guard身份验证器,并像这样配置它们:

security:
  firewalls:
    my_firewall:
      pattern: ^/some-pattern
      guard:
        authenticators:
          - my_first_auth
          - my_second_auth
        entry_point: my_first_auth
Run Code Online (Sandbox Code Playgroud)

问题是它们运行,所以如果用户在第一次成功但在第二次成功时失败,他会得到一个禁止的响应.在这种情况下,我希望他成功.

有没有办法在OR关系中配置多个Guard身份验证器,这样如果第一个成功,第二个会被跳过?

Ian*_*ips 5

最后,我自己手动实现了一个解决方案,利用if if getCredentials()返回null 的事实,跳过了身份验证器.两个身份验证器都有如下代码:

class MyAuthenticator extends AbstractGuardAuthenticator {
    private $tokenStorage;

    public function __construct(TokenStorageInterface $tokenStorage) {
        $this->tokenStorage = $tokenStorage;
    }

    public function getCredentials(Request $request) {
        if ($this->isAlreadyAuthenticated()) {
            return null; // Skip this authenticator
        }
        // Get the credentials and continue with this authenticator
    }

    protected function isAlreadyAuthenticated() {
        $token = $this->tokenStorage->getToken();
        return $token && $token->isAuthenticated();
    }
Run Code Online (Sandbox Code Playgroud)