我有一个用symfony2开发的服务器端api,现在我尝试进行身份验证。
客户端/移动设备应用程序必须使用API密钥进行身份验证
使用应用程序的用户必须使用电子邮件+密码进行身份验证并获得访问令牌
因此我使用这个防火墙和apikey验证器
firewalls:
login:
pattern: ^/login$
security: false
secured_area:
pattern: ^/
stateless: true
simple_preauth:
authenticator: apikey_authenticator
API密钥验证器
namespace Rental\APIBundle\Security;
use Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
class ApiKeyAuthenticator implements SimplePreAuthenticatorInterface {
protected $userProvider;
public function __construct(ApiKeyUserProvider $userProvider)
{
$this->userProvider = $userProvider;
}
public function createToken(Request $request, $providerKey)
{
//$apiKey = $request->query->get('apikey');
// use test value
$apiKey = "234234234";
if (!$apiKey) {
throw new BadCredentialsException('No API key found');
}
return new PreAuthenticatedToken(
'anon.',
$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;
}
}
到目前为止,没问题。现在这个类使用以下类的方法
namespace Rental\APIBundle\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
// use test value
$username = "Alex";
return $username;
}
public function loadUserByUsername($username)
{
// return User by Username
}
public function refreshUser(UserInterface $user)
{
// code
}
public function supportsClass($class)
{
return 'Symfony\Component\Security\Core\User\User' === $class;
}
}
我的具体问题是:
方法需要通过搜索用户名来查找用户实体。但从这个类我没有访问数据库的权限。我发现了使用静态方法
loadUserByUsername
的示例,但是没有这种方法,而且实体(mvc的模型)也没有访问数据库的权限。如何将用户从数据库中取出?我想首先认证应用程序本身,以便进行一般的api访问,其次认证用户,以获取个人信息和有限的编辑权限。当用户通过电子邮件和密码登录时,数据被保存,例如在usernamepassworttoken中,来自同一客户端的下一个调用如何使用访问令牌访问数据。会话对这些ajax http请求没有影响。
最佳答案
一。
您应该对它使用依赖注入,并在您的提供者中注入实体管理器。
your_api_key_user_provider:
class: Rental\APIBundle\Security\ApiKeyUserProvider
arguments: ["@doctrine.orm.entity_manager"]
apikey_authenticator:
class: Rental\APIBundle\Security\ApiKeyAuthenticator
arguments: [""@your_api_key_user_provider"]
之后,将其添加到提供程序中:
use Doctrine\ORM\EntityManager;
class ApiKeyUserProvider implements UserProviderInterface {
protected $em;
public function __construct(EntityManager $em){
$this->em = $entityManager;
}
//... Now you have access to database
}
2.ajax可以发送cookies,php可以像处理普通会话和use会话一样处理这些请求。确保您的请求是发送cookies(请参见Why is jquery's .ajax() method not sending my session cookie?)