jmikola/auto-login-bundle

通过单个查询参数(例如电子邮件和新闻通讯链接)在您的Symfony应用程序中验证用户。

安装次数:313,979

依赖项: 0

建议者: 0

安全性: 0

星星: 84

关注者: 9

分支: 16

开放问题: 7

类型:symfony-bundle

2.0.1 2020-04-03 01:00 UTC

This package is auto-updated.

Last update: 2024-08-29 04:37:30 UTC


README

此包将AutoLogin库与Symfony集成,该库通过单个查询参数实现安全防火墙监听器来验证用户。这对于在电子邮件和新闻通讯链接中提供一键登录功能非常有用。

安装

该包作为发布,可以通过Composer安装。

$ composer require jmikola/auto-login-bundle=^2.0

在您的应用程序内核中激活该包

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Jmikola\AutoLoginBundle\JmikolaAutoLoginBundle(),
            // ...
        );
    }
}

兼容性

此包需要Symfony 4.3或更高版本。

使用和配置

您需要创建一个新的实现Jmikola\AutoLogin\User\AutoLoginUserProviderInterfaceUserProvider服务。此服务负责将URL令牌解析为用户对象。

此包注册了一个防火墙监听器,通过安全组件的防火墙配置中的jmikola_auto_login键进行配置。请参阅此示例配置

# services.yml
services:
  acme.auto_login_user_provider:
    # Implements Jmikola\AutoLogin\User\AutoLoginUserProviderInterface
    class: Acme\UserBundle\Security\AcmeAutoLoginUserProvider
# security.yml
security:
  firewalls:
    main:
      # We need not specify a "provider" for our firewall or listeners,
      # since SecurityBundle will default to the first provider defined.
      jmikola_auto_login:
        auto_login_user_provider: acme.auto_login_user_provider

在上面的示例中,我们指定了一个自定义服务用于auto_login_user_provider,因为默认的EntityUserProvider没有实现AutoLoginUserProviderInterface

当访问http://example.com/path?_al=foobar时,将调用AcmeAutoLoginUserProvider::loadUserByAutoLoginToken()方法,并传入值"foobar"。此方法应解析该值到用户对象或抛出Jmikola\AutoLogin\Exception\AutoLoginTokenNotFoundException

监听器选项

AutoLoginFactory定义以下监听器选项

  • auto_login_user_provider:AutoLoginUserProviderInterface服务,提供通过自动登录令牌(即查询参数)加载用户的方法。如果未定义此服务,则默认使用监听器的用户提供程序,并且如果提供程序没有实现所需接口(除UserProviderInterface外)将抛出异常。
  • provider:用户提供程序键。这是大多数安全监听器的标准选项。如果未定义,则使用防火墙的默认用户提供程序(请参阅:SecurityBundle文档)。
  • token_param:要检查自动登录令牌的查询参数。此查询参数的存在将决定是否尝试自动登录监听器的身份验证。在这方面,它与表单登录监听器的check_path选项类似。如果未定义,则默认为_al
  • override_already_authenticated:布尔选项确定自动登录令牌是否应该覆盖现有的已认证会话。默认为false。

替代配置示例

在此示例中,我们指定了一个同时实现Symfony\Component\Security\Core\User\UserProviderInterfaceJmikola\AutoLogin\User\AutoLoginUserProviderInterface的提供程序。我们还自定义了URL参数,使用"auto_login"而不是默认的"_al"。

# services.yml
services:
  acme.versatile_user_provider:
    # This class implements UserProviderInterface and
    # AutoLoginUserProviderInterface
    class: Acme\UserBundle\Security\VersatileUserProvider
# security.yml
security:
  providers:
    acme_user_provider:
      id: acme.versatile_user_provider
  firewalls:
    main:
      jmikola_auto_login:
        token_param: auto_login
        # We need not configure the auto_login_user_provider option here, as the
        # bundle will default to the firewall's default user provider, which
        # implements the necessary interface.

FOSUserBundle配置示例

如果您正在使用FOSUserBundle,为您用户提供程序定义服务ID将看起来很熟悉。您可以通过定义用于fos_user.user_manager的自定义服务轻松地将此包与FOSUserBundle集成。

services:
    acme.user_manager:
        # This class extends the appropriate UserManager from FOSUserBundle
        # and implements Jmikola\AutoLogin\User\AutoLoginUserProviderInterface
        class: Acme\UserBundle\Model\UserManager
        # Note: the remaining service configuration is abridged

fos_user:
    service:
        user_manager: acme.user_manager