繁体   English   中英

在没有数据库的情况下在 Symfony 3 中验证用户

[英]Authenticate User in Symfony 3 without Database

我有一个应用程序,其中登录过程针对外部 API 运行。API 给了我一个 JSON,我将其存储在我的用户 Enitiy 中。(称之为成员)

用户 Class 实现AdvancedUserInterface

还有我的登录过程。

    $firewall = 'main';
    $token = new UsernamePasswordToken($member, null, $firewall, $member->getRoles());
    $this->tokenStorage->setToken($token);
    $this->session->set('_security_'.$firewall, serialize($token));

    $request = new \Symfony\Component\HttpFoundation\Request();

    $event = new InteractiveLoginEvent($request, $token);
    $this->eventDispatcher->dispatch("security.interactive_login", $event);

但这是行不通的。 现在我在这里读到这个过程需要数据库中的用户。 我的应用程序没有用户数据库。 有没有办法在没有它的情况下登录用户。

您可以定义自定义守卫身份验证器: 如何使用 Guard 创建自定义身份验证系统 我将快速 go 完成此处的主要步骤,但请查看链接,因为它更深入地解释了这些步骤。

首先需要创建用户class和对应的用户提供者。 看起来你已经这样做了。 如果您以后不在身份验证器 class 中使用它,您也可以跳过用户提供程序 class(或创建一个假的)。

接下来创建您的 Authenticator class(比方说AppBundle\Security\ExternalApiAuthenticator )。 它只是一个普通的 symfony 服务,扩展了Symfony\Component\Security\Guard\AbstractGuardAuthenticator 关于 class 的评论很好地解释了如何实现它,因此您应该检查它们,但有一件事可能会有所帮助:您已经可以在getUser方法中使用 API 检查凭据,然后始终在checkCredentials中返回 true。

最后向app/config/security.yml添加一些配置:

# app/config/security.yml
security:
    # ...

    firewalls:
        # ...

        main:
            anonymous: ~
            logout: ~

            guard:
                authenticators:
                    - AppBundle\Security\ExternalApiAuthenticator

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM