【问题标题】:Symfony authenticate user with external api keySymfony 使用外部 api 密钥对用户进行身份验证
【发布时间】:2014-03-31 16:49:01
【问题描述】:

我正在尝试通过 http://symfony.com/doc/current/cookbook/security/api_key_authentication.html#cookbook-security-api-key-config 之后的外部 api 密钥请求对用户进行身份验证

什么是 ["@your_api_key_user_provider"] ? 如果我输入类似 ["test"] 的内容,则会出现错误。

[更新]

这是我的 ApiKeyAuthenticator.php:

// src/Acme/HelloBundle/Security/ApiKeyAuthenticator.php

 namespace Acme\HelloBundle\Security;
 use ////

 class ApiKeyAuthenticator implements SimplePreAuthenticatorInterface
 {
protected $userProvider;

public function __construct(ApiKeyUserProvider $userProvider)
{
    $this->userProvider = $userProvider;
}

public function createToken(Request $request, $providerKey)
{
    if (!$request->query->has('apikey')) {
        throw new BadCredentialsException('No API key found');
    }

    return new PreAuthenticatedToken(
        'anon.',
        $request->query->get('apikey'),
        $providerKey
    );
}

public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
{
    $apiKey = $token->getCredentials();
    $username = $this->userProvider->getUsernameForApiKey($apiKey);

    if (!$username) {
        throw new AuthenticationException(
            sprintf('API Key "%s" does not exist.', $apiKey)
        );
    }

    $user = $this->userProvider->loadUserByUsername($username);

    return new PreAuthenticatedToken(
        $user,
        $apiKey,
        $providerKey,
        $user->getRoles()
    );
}

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

}

虽然用户提供者是这样的:

// src/Acme/HelloBundle/Security/ApiKeyUserProvider.php

命名空间 Acme\HelloBundle\Security;

use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;

  class ApiKeyUserProvider implements UserProviderInterface
 {
public function getUsernameForApiKey($apiKey)
{
    // Look up the username based on the token in the database, via
    // an API call, or do something entirely different
    $username = ...;

    return $username;
}

public function loadUserByUsername($username)
{
    return new User(
        $username,
        null,
        // the roles for the user - you may choose to determine
        // these dynamically somehow based on the user
        array('ROLE_USER')
    );
}

public function refreshUser(UserInterface $user)
{
    // this is used for storing authentication in the session
    // but in this example, the token is sent in each request,
    // so authentication can be stateless. Throwing this exception
    // is proper to make things stateless
    throw new UnsupportedUserException();
}

public function supportsClass($class)
{
    return 'Symfony\Component\Security\Core\User\User' === $class;
}
}

服务应该是这样的:

services:
# ...

apikey_authenticator:
    class:     Acme\SeedBundle\Security\ApiKeyAuthenticator
    arguments: ["@ApiKeyUserProvider"]

但我收到此错误:服务“apikey_authenticator”依赖于不存在的服务“apikeyuserprovider”。

谢谢

【问题讨论】:

    标签: api symfony authentication


    【解决方案1】:

    这是您应该按照此文档创建的用户提供程序服务:

    http://symfony.com/doc/current/cookbook/security/custom_provider.html

    所以您将您的用户提供者注册为服务 IE:apikey_userprovider http://symfony.com/doc/current/cookbook/security/custom_provider.html#create-a-service-for-the-user-provider

    然后使用["@apikey_userprovider"]传递它

    所以你的服务文件应该是这样的:

    parameters:
        apikey_userprovider.class: Acme\HelloBundle\Security\ApiKeyUserProvider
        apikey_authenticator.class: Acme\SeedBundle\Security\ApiKeyAuthenticator
    services:
        apikey_userprovider:
            class: %apikey_userprovider.class%
        apikey_authenticator:
          class: %apikey_authenticator.class%
          arguments: ["@apikey_userprovider"]
    

    您需要将您的用户提供者定义为服务。这就是 @ 操作符告诉 symfony 去寻找的东西。在参数中定义你的类只是Symfony Coding Standards的一部分

    【讨论】:

    • 谢谢!但它不起作用!我更新了帖子!再次感谢您帮助我
    • 检查我更新的答案,您需要确保将您的用户提供者定义为服务并使用正确的名称调用它。
    • 谢谢!有效!现在我正在尝试按照手册中的说明存储会话“在会话中存储身份验证”,但出现此错误:Argument 1 passed to Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager::authenticate() must be an Symfony\Component\Security\Core\Authentication\Token\TokenInterface 的实例,给定 null
    • 看看我对其他问题的回答,该问题涉及在本季存储身份验证stackoverflow.com/questions/5886713/…
    猜你喜欢
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 2013-05-08
    • 2017-06-07
    • 2019-08-25
    • 1970-01-01
    • 2019-05-08
    • 2018-07-19
    相关资源
    最近更新 更多