AuthenticatorInterface::supports() 方法的用途是什么?

Lou*_*pax 5 symfony symfony-security symfony-flex symfony4

在 Symfony 4 中,该AuthenticatorInterface::supports()方法有以下注释:

interface AuthenticatorInterface extends AuthenticationEntryPointInterface
{
    /**
     * Does the authenticator support the given Request?
     *
     * If this returns false, the authenticator will be skipped.
     *
     * @param Request $request
     *
     * @return bool
     */
    public function supports(Request $request);
Run Code Online (Sandbox Code Playgroud)

我发现措辞令人困惑。username当我尝试实现这一点时,我的第一直觉是如果请求包含and字段,则返回 true password,但后来我记得我收到的所有请求都经过身份验证,即使我没有使用登录表单。

supports()方法是重写参数的方法吗security.firewalls.myFirewall.pattern?它是处理多个身份验证器之间的流的东西吗?

我应该如何使用这个界面?

Ste*_*ant 2

我同意这个功能还没有得到很好的记录。我唯一能找到的是:

如何使用 Guard 创建自定义身份验证系统

支持(请求$请求)

这将在每个请求上调用,您的工作是决定是否应将身份验证器用于此请求(返回 true)或是否应跳过(返回 false)。

例如:您可以使用 来Request检查它是否是 XMLHttpRequest (AJAX),这样您就可以拥有专用的AjaxAuthenticator.

如何使用投票者检查用户权限VoterInterface::support()中记录了类似的功能 ( ) 。

  • 在阅读了更多源代码之后,这也是我的猜测。看起来当我们在特定防火墙内有多个身份验证器时会使用它 (2认同)