2016-04-25 20 views
3

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中工作,这是一个私人使用的api验证器包。

我想为它编写一个工厂,检查接口所有脑干从FosUserBundle

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

的例子,但我的类,它实现这个接口是在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 
{ 

services.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 

我的确切的问题是,即使我定义两个类是相同对方,但实现名称空间不同,即使我怎么能给这个firew所有?这是如何从防火墙中抽象出来的?

任何帮助,将不胜感激。

回答

0

好,我找到了答案,

当我专注于服务容器和security.yml防火墙配置。我只是忘了,我的防火墙配置可能不同于symfony2和symfony3应用程序。

以这种方式,我可以将定义为任一PreSymfony28和Symfony30一个BaseAccessTokenAuthenticatorAccessTokenAuthenticator,然后我将把所有的逻辑内部BaseAccessTokenAuthenticator和AccessTokenAuthenticator将多个和将有2个不同的服务。一个用于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的3.0类:

<?php 

namespace MyCompany\SBundle\Security; 

use Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface; 

/** 
* Class AccessTokenAuthenticatorSf30 
*/ 
class AccessTokenAuthenticatorSf30 extends BaseAccessTokenAuthenticator implements SimplePreAuthenticatorInterface 
{ 
} 

预Symfony的2。8类:

<?php 

namespace MyCompany\SBundle\Security; 

use Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface; 

/** 
* Class AccessTokenAuthenticatorPreSf28 
*/ 
class AccessTokenAuthenticatorPreSf28 extends BaseAccessTokenAuthenticator implements SimplePreAuthenticatorInterface 
{ 
} 

服务:

为symfony1.2
# 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"] 

广泛应用防火墙= < 2.8:

为symfony1.2
simple_preauth: 
     authenticator: mycompany.security.accesstoken_authenticator_sf28 

广泛应用防火墙> = 2.8:

simple_preauth: 
     authenticator: mycompany.security.accesstoken_authenticator_sf30 
0

这可能是非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 { } 
} 

自动加载还是应该把它捡起来。