2014-04-12 69 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

相关问题