2016-04-25 55 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 { } 
} 

自動加載還是應該把它撿起來。