php - 必须保持向后兼容性中断的身份验证器类的方法

标签 php authentication symfony

在 Symfony 2.8 之前 SimplePreAuthenticatorInterface 位于以下命名空间 Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface 它在 2.8 中已被弃用,并在 3.0 中更改为 Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface

现在我正在写一个包,它必须在 symfony 2.7 和 symfony 3.0 中工作,它是一个供私有(private)使用的 api 身份验证器包。

我想为它写一个检查接口(interface)是否存在的工厂

示例来自 FosUserBundle

if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) {
    $tokenStorage = $this->get('security.token_storage');
} else {
    $tokenStorage = $this->get('security.context');
}

但是我实现这个接口(interface)的类是 DI 中的一个服务,symfony 防火墙直接使用它。

我的问题是如何以最佳实践和逻辑的方式进行这种抽象。

AccessTokenAuthenticator 类:

<?php

namespace MyCompany\SBundle\Security;
// this usage for before symfony 2.8
use Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface;

/**
 * Class AccessTokenAuthenticator
 */
class AccessTokenAuthenticator implements SimplePreAuthenticatorInterface
{

服务.yml

# Authentication
mycompany_security.security.accesstoken_authenticator:
    class:     MyCompany\SBundle\Security\AccessTokenAuthenticator
    arguments: ["@mycompany_security.security.accesstoken_userprovider"]

防火墙配置:

secure_area:
    pattern:  ^/
    stateless: true
    simple_preauth:
        authenticator: mycompany_security.security.accesstoken_authenticator

我的确切问题是,即使我定义了两个彼此相同但实现命名空间不同的类,即使我如何将其提供给防火墙?这如何从防火墙中抽象出来?

如有任何帮助,我们将不胜感激。

最佳答案

这可能不符合 PSR 但会起作用(可能)。

文件AccessTokenAuthenticator.php

if (interface_exists('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')) {

    class AccessTokenAuthenticator implements \Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface { }

} else {
      class AccessTokenAuthenticator implements \Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface { }
}

自动加载应该仍会拾取它。

关于php - 必须保持向后兼容性中断的身份验证器类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36835683/

相关文章:

php - 使用php运行mysql存储过程

php - 在 laravel 5.2 中使用默认身份验证路由对用户进行身份验证后设置 session 数据

authentication - golang中的单点登录认证

Symfony 3.3.2 Doctrine EntityRepository 构造函数参数

php - Symfony ExceptionHandler 抛出关于自身的错误

Symfony2 通过 Ajax 获取表单

php - 在 PHP 中缓存变量

PHP 使用 WHERE 选择特定列

php - Google PageSpeed API 范围的 URL

angularjs - NodeJs Passport isAuthenticated() 即使在登录后也返回 false