如何使用 LoginFormAuthenticator 和 Login Link Authenticator 为 Symfony 5.2 中的特定路径设置默认身份验证器

问题描述

我在 Symfony 5.2 中为我的客户使用新的登录链接方法 [1]。他们应该只使用这种身份验证方法。但我也为我的管理员用户使用登录表单身份验证 [2]。

当未经身份验证的客户访问受限路径(在我的情况下为 /app)时,他/她会被重定向登录表单而不是登录链接表单。当会话已过期或他们访问(未经身份验证)受限区域内的书签链接时,也会出现这种情况。

如何为 /app 设置认验证器? /行政?我想要这样的东西:

  • 客户区域/app -> /login(登录链接验证器)
  • 管理区域 /admin -> /login-password(登录表单验证器)

我的 security.yaml 如下所示:

security:
    encoders:
        App\Entity\User:
            algorithm: auto

    enable_authenticator_manager: true
    providers:
        app_user_provider:
            entity:
                class: App\Entity\User
                property: email
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false    
        main:
            lazy: true
            provider: app_user_provider
            guard:
                authenticators:
                    - App\Security\LoginFormAuthenticator
            login_link:
                check_route: login_check
                signature_properties: ['id']
                max_uses: 1
            logout:
                path: app_logout

    access_control:
        - { path: ^/admin,roles: ROLE_ADMIN }
        - { path: ^/app,roles: ROLE_USER }
        - { path: ^/,roles: PUBLIC_ACCESS }

[1] 登录链接方式:https://symfony.com/doc/current/security/login_link.html
[2] LoginFormAuthenticator:https://symfony.com/doc/current/security/form_login_setup.html

解决方法

好的,我自己想出了一个解决方案。如果有更好的解决方案,请告诉我!

我覆盖了 LoginFormAuthenticator 类中的 start() 方法。在那里,我将 $request obj 存储在私有类 prop 中,因为由于 AbstractFormLoginAuthenticator 类中的定义,我无法覆盖 getLoginUrl()。然后我在 getLoginUrl() 方法中添加一些逻辑来检查用户想要访问的区域(/admin 或 /app)并相应地重定向。而已。奇迹般有效。 ;-)

// App\Security\LoginFormAuthenticator

    public const LOGIN_ROUTE                = 'admin_login';
    public const LOGIN_ROUTE_PASSWORDLESS   = 'app_login';

    ...

    private $request;

    ...

    /**
     * Override to control what happens when the user hits a secure page
     * but isn't logged in yet.
     *
     * @return RedirectResponse
     */
    public function start(Request $request,AuthenticationException $authException = null)
    {
        $this->request = $request;
        $url = $this->getLoginUrl();

        return new RedirectResponse($url);
    }

    ...

    protected function getLoginUrl()
    {
        // Check if user would like to access /app
        $path = $this->request->getPathInfo();
        
        if ('/app' === substr($path,4)) {
            return $this->urlGenerator->generate(self::LOGIN_ROUTE_PASSWORDLESS);
        }

        return $this->urlGenerator->generate(self::LOGIN_ROUTE);
    }