2014-04-12 65 views
1

我正在爲使用API​​密鑰寫一個自定義身份驗證提供程序。我現在主要工作,但有一件事讓我感到困惑。我已經找到了創建這個行爲到Symfony的部分:在Symfony2中,爲什麼完全通過身份驗證的用戶也進行了匿名身份驗證?

供應商/ symfony中/ symfony中/ src目錄/ Symfony的/分量/安全/核心/授權/選民/ AuthenticatedVoter.php:

/** 
* {@inheritdoc} 
*/ 
public function vote(TokenInterface $token, $object, array $attributes) 
{ 
    $result = VoterInterface::ACCESS_ABSTAIN; 
    foreach ($attributes as $attribute) { 
     if (!$this->supportsAttribute($attribute)) { 
      continue; 
     } 

     $result = VoterInterface::ACCESS_DENIED; 

     if (self::IS_AUTHENTICATED_FULLY === $attribute 
      && $this->authenticationTrustResolver->isFullFledged($token)) { 
      return VoterInterface::ACCESS_GRANTED; 
     } 

     if (self::IS_AUTHENTICATED_REMEMBERED === $attribute 
      && ($this->authenticationTrustResolver->isRememberMe($token) 
       || $this->authenticationTrustResolver->isFullFledged($token))) { 
      return VoterInterface::ACCESS_GRANTED; 
     } 

     if (self::IS_AUTHENTICATED_ANONYMOUSLY === $attribute 
      && ($this->authenticationTrustResolver->isAnonymous($token) 
       || $this->authenticationTrustResolver->isRememberMe($token) 
       || $this->authenticationTrustResolver->isFullFledged($token))) { 
      return VoterInterface::ACCESS_GRANTED; 
     } 
    } 

    return $result; 
} 

爲什麼在最後一節中提到「如果你完全認證了,或者記住了你也是匿名登錄的」?當然,如果你是匿名登錄的,那麼你並不完全成熟,或者反之亦然。

我試圖解決這個問題的原因是因爲我有一個我的API的防火牆部分,你傳遞一個用戶名和密碼,以便從那時起創建一個用於身份驗證的令牌。如果您尚未使用API​​密鑰進行身份驗證,即如果用戶「已註銷」(無狀態),我只希望此區域可以訪問。

我該如何實現這種行爲?

回答

0

最後,我跟着隱約按照本指南:http://symfony.com/doc/current/cookbook/security/voters.html

我複製了原來的Symfony選民,並將其改爲如下所示:

/** 
* {@inheritdoc} 
*/ 
public function vote(TokenInterface $token, $object, array $attributes) 
{ 
    $result = VoterInterface::ACCESS_ABSTAIN; 
    foreach ($attributes as $attribute) { 
     if (!$this->supportsAttribute($attribute)) { 
      continue; 
     } 

     $result = VoterInterface::ACCESS_DENIED; 

     if (self::IS_AUTHENTICATED_FULLY === $attribute 
      && $this->authenticationTrustResolver->isFullFledged($token)) { 
      return VoterInterface::ACCESS_GRANTED; 
     } 

     if (self::IS_AUTHENTICATED_REMEMBERED === $attribute 
      && $this->authenticationTrustResolver->isRememberMe($token)) { 
      return VoterInterface::ACCESS_GRANTED; 
     } 

     if (self::IS_AUTHENTICATED_ANONYMOUSLY === $attribute 
      && $this->authenticationTrustResolver->isAnonymous($token)) { 
      return VoterInterface::ACCESS_GRANTED; 
     } 
    } 

    return $result; 
} 

然後我說的服務:

pp_security.apikey_authenticated_voter: 
    class: %pp_security.apikey_authenticated_voter.class% 
    arguments: 
     - "@security.authentication.trust_resolver" 
    public:  false 
    tags: 
     - { name: security.voter } 

然後,在我的應用程序security.yml文件我增加了以下內容:

security: 
    access_decision_manager: 
     strategy: unanimous 

這意味着,代替Symfony的授權訪問,如果允許一個單一的選民它現在需要所有選民授予訪問權,這當然需要考慮這個新的選民。

現在,用戶是三個中的一個,不再是其中一個。

0

我的用戶此功能可以告訴是有人登錄:

public function isAuthenticated() 
{ 
    $securityContext = $this->container->get('security.context'); 
    return $securityContext->isGranted('IS_AUTHENTICATED_REMEMBERED'); 
} 

所以在你的控制器動作,您可以用

if (isAuthenticated()) { 
    throw $this->createNotFoundException('This page does not exist'); 
} 
+0

不完全是我真的以後。我可以發現用戶是否以特定的方式足夠簡單地被認證。 Symfony似乎分配權限的方式似乎很奇怪。完全驗證應該意味着你不是匿名的,但在Symfony中你是默認的。 – Seer

相關問題