Php 一种必须保持向后兼容性的身份验证程序类的方法
在Symfony 2.8之前Php 一种必须保持向后兼容性的身份验证程序类的方法,php,authentication,symfony,Php,Authentication,Symfony,在Symfony 2.8之前 simplereauthenticatorinterface位于以下命名空间Symfony\Component\Security\Core\Authentication\simplereauthenticatorinterface 它在2.8中被弃用,在3.0中被更改为Symfony\Component\Security\Http\Authentication\simplereauthenticatorinterface 现在我正在编写一个捆绑包,它必须在symfo
simplereauthenticatorinterface
位于以下命名空间Symfony\Component\Security\Core\Authentication\simplereauthenticatorinterface
它在2.8中被弃用,在3.0中被更改为Symfony\Component\Security\Http\Authentication\simplereauthenticatorinterface
现在我正在编写一个捆绑包,它必须在symfony 2.7和symfony 3.0中工作。它是一个供私人使用的api验证器捆绑包
我想写一个工厂,它检查接口的存在
来自
但我实现这个接口的类是DI中的一个服务,symfony firewall直接使用它
我的问题是,我如何才能以最佳实践和逻辑的方式进行抽象
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
{
防火墙配置:
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 { }
}
自动加载仍应将其拾取 好的,我找到了答案
当我关注服务容器和security.yml防火墙配置时。我只是忘记了,我的防火墙配置可能不同于symfony2和symfony3应用程序
通过这种方式,我可以为PreSymfony28和Symfony30定义BaseAccessTokenAuthenticator
和AccessTokenAuthenticator
,然后我将所有逻辑放在BaseAccessTokenAuthenticator
中,AccessTokenAuthenticator将是多个,并且将有两个不同的服务。一个用于symfony2.7防火墙配置一个用于symfony3.0
基类:
<?php
namespace MyCompany\SBundle\Security;
/**
* Abstract Class BaseAccessTokenAuthenticator
*/
abstract class BaseAccessTokenAuthenticator
{
public function createToken(Request $request, $providerKey)
{
// Implementation
}
public function authenticateToken(
TokenInterface $token,
UserProviderInterface $userProvider,
$providerKey
) {
// Implementation.
}
public function supportsToken(TokenInterface $token, $providerKey)
{
// Implementation.
}
}
symfony=<2.8的宽应用防火墙:
simple_preauth:
authenticator: mycompany.security.accesstoken_authenticator_sf28
symfony>=2.8的宽应用防火墙:
simple_preauth:
authenticator: mycompany.security.accesstoken_authenticator_sf30
<?php
namespace MyCompany\SBundle\Security;
use Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface;
/**
* Class AccessTokenAuthenticatorSf30
*/
class AccessTokenAuthenticatorSf30 extends BaseAccessTokenAuthenticator implements SimplePreAuthenticatorInterface
{
}
<?php
namespace MyCompany\SBundle\Security;
use Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface;
/**
* Class AccessTokenAuthenticatorPreSf28
*/
class AccessTokenAuthenticatorPreSf28 extends BaseAccessTokenAuthenticator implements SimplePreAuthenticatorInterface
{
}
# Authentication
mycompany.security.accesstoken_authenticator_pre_sf28:
class: MyCompany\SBundle\Security\AccessTokenAuthenticatorPreSf28
arguments: ["@mycompany.security.accesstoken_userprovider"]
# Authentication
mycompany.security.accesstoken_authenticator_sf30:
class: MyCompany\SBundle\Security\AccessTokenAuthenticatorSf30
arguments: ["@mycompany.security.accesstoken_userprovider"]
simple_preauth:
authenticator: mycompany.security.accesstoken_authenticator_sf28
simple_preauth:
authenticator: mycompany.security.accesstoken_authenticator_sf30