使用SimplePreAuthenticator& amp;时处理短暂令牌的最佳方法PreAuthenticatedToken

时间:2015-12-08 08:50:56

标签: php symfony security authentication

我正在使用此处所述的SimpleAuthentication功能:http://symfony.com/doc/2.6/cookbook/security/api_key_authentication.html

目的是通过作为查询(get)参数或标头传递的标记字符串提供身份验证。此令牌,让我们称之为 TemporaryAccessToken 以避免与Sf2令牌混淆,由控制器生成,通过电子邮件发送给用户(此处未描述)并且应该可用于限制时间量(专用实体中有valid_until \DateTime列。

对于记录,当第一次验证过程如下所示访问受保护的页面(通过simple_user_account防火墙)时:

  • SimplePreAuthenticationListener已触发
  • 基本上,calls MyAuthenticator->createToken()
  • 然后calls AuthenticationManager->authenticate()calls MyAuthenticator->authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)

问题是:一旦用户通过身份验证,并且因为我在防火墙配置中使用stateless: false,则不会再次触发身份验证过程,因为已经有一个有效的Sf2令牌会话。

我看到了2个逻辑解决方案,但我无法弄清楚如何正确地做到这一点:

  • 使会话有效,直到设置到TemporaryAccessToken.valid_until列的日期为止。有没有办法实现这一目标"本地" ? (我看到remember_me防火墙有一个lifetime参数)
  • ,能够重新检查TemporaryAccessToken的有效性(请参阅我的身份验证器中的if( $accessToken && $accessToken->isValid() )行)

app/config/security.yml

firewalls:
    simple_user_account:
        pattern:   ^/account/access
        stateless:  false
        simple_preauth:
            authenticator: app.security.simple_user_authenticator
        logout:
            path:   /logout_simple
            target: /

我的身份验证器类看起来像:

class SimpleUserAuthenticator implements SimplePreAuthenticatorInterface, AuthenticationFailureHandlerInterface
{
    /**
     * @var AccessTokenManager
     */
    private $accessTokenManager;

    /**
     * SimpleUserAuthenticator constructor.
     * @param AccessTokenManager $accessTokenManager
     */
    public function __construct(AccessTokenManager $accessTokenManager)
    {
        $this->accessTokenManager = $accessTokenManager;
    }

    public function createToken(Request $request, $providerKey)
    {
        $TemporaryAccessToken = $request->query->get('simple_user_token');
        if (!$TemporaryAccessToken) {
            throw new BadCredentialsException('No simple_user token found');
        }

        return new PreAuthenticatedToken(
            'anonymous',
            $TemporaryAccessToken,
            $providerKey
        );
    }


    public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
    {
        if( $token->getUser() instanceof SimpleUser )
        {
            $newToken = new PreAuthenticatedToken(
                $token->getUser(),
                $token->getCredentials(),
                $providerKey,
                array('ROLE_USER')
            );
            return $newToken;
        }

        $accessToken = $this->accessTokenManager->getRepository()->findOneByToken($token->getCredentials());
        if( $accessToken && $accessToken->isValid() )
        {
            $user = $userProvider->loadUserByUsername($accessToken->getAccount()->getEmailCanonical());
            $newToken = new PreAuthenticatedToken(
                $user,
                $token->getCredentials(),
                $providerKey,
                array('ROLE_USER')
            );
            return $newToken;
        }
    }

    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        $response =  new RedirectResponse(
            '/login'
        );
        return $response;
    }

    public function supportsToken(TokenInterface $token, $providerKey)
    {
        return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
    }


}

Symfony2版本:2.5.6

1 个答案:

答案 0 :(得分:1)

好吧,如果您需要使用会话,可以在config.yml会话设置下设置 List<WebElement> linksize=null; String links[]=null; linksize = driver.findElements(By.cssSelector("div[class=panel-section]")); int linksCount = linksize.size(); links= new String[linksCount]; for(int i=0;i<linksCount;i++) { links[i] = linksize.get(i).getAttribute("style"); if(links[i].isEmpty()) { System.out.println("I am div without style"); linksize.get(i).click(); } }

lifetime

您可以在几秒钟内将# config.yml framework: session: # handler_id set to null will use default session handler from php.ini handler_id: ~ lifetime: 3600 值更改为任何值(默认为3600或1小时)

此外,您可以尝试configure garbage collector

如果这个答案没有用,请告诉我。

相关问题