我正在尝试在注册时创建一个令牌。 我使用FosUserBundle
作为身份验证器和 API 的FOSOAuthServerBundle
。
当我手动生成令牌时,一切正常。但是我想在成功注册后自动生成它。所以我创建了一个名为TokenSetter
的EventListener
这是代码
class TokenSetter
{
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function postPersist(LifecycleEventArgs $args)
{
$user = $args->getEntity();
if ($user instanceof Account) {
$clientManager = $this->container->get('fos_oauth_server.client_manager.default');
$client = $clientManager->createClient();
$client->setRedirectUris(array('http://127.0.0.1:8000'));
$client->setAllowedGrantTypes(array('password', 'refresh_token'));
$clientManager->updateClient($client);
$grantRequest = new Request(array(
'client_id' => $client->getPublicId(),
'client_secret' => $client->getSecret(),
'grant_type' => 'password',
'username' => $user->getUsername(),
'password' => $user->getPlainPassword()
));
$tokenResponse = $this->container->get('fos_oauth_server.server')->grantAccessToken($grantRequest);
$token = $tokenResponse->getContent();
}
}
}
问题是$user->getPlainPassword()
返回一个空值。这会导致创建后出现"invalid_request"。
有没有办法获取纯密码或以不同的方式生成令牌?
自己解决了。
有两种方法可以解决这个问题。
第一种方法是将password
更改为client_credentials
。这样,在生成访问令牌时无需发送用户名和密码。缺点是user_id
的值将设置为NULL
。
第二种解决方案是创建不同的侦听器。(RegistrationListener)
您需要编辑UserEntity
并添加TempPlainPass
getter和setter。 基本上,您需要做的是,将明文密码存储在数据库中onRegistrationSuccess
并在onRegistrationCompleted
上创建一个带有用户名和密码的令牌
重要! 不要忘记在生成令牌后删除TempPlainPass。
请参阅下面的代码:
class RegistrationListener implements EventSubscriberInterface
{
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
FOSUserEvents::REGISTRATION_COMPLETED => 'onRegistrationCompleted',
);
}
public function onRegistrationSuccess(FormEvent $event)
{
$user = $event->getForm()->getData();
$user->setTempPlainPass($user->getPlainPassword());
$user->setRoles(array('ROLE_ADMIN'));
}
public function onRegistrationCompleted(FilterUserResponseEvent $event)
{
$user = $event->getUser();
$clientManager = $this->container->get('fos_oauth_server.client_manager.default');
$client = $clientManager->createClient();
$client->setRedirectUris(array('http://127.0.0.1:8000'));
$client->setAllowedGrantTypes(array('password', 'refresh_token'));
$clientManager->updateClient($client);
$grantRequest = new Request(array(
'client_id' => $client->getPublicId(),
'client_secret' => $client->getSecret(),
'grant_type' => 'password',
'username' => $user->getUsername(),
'password' => $user->getTempPlainPass()
));
$this->container->get('fos_oauth_server.server')->grantAccessToken($grantRequest);
$em = $this->container->get('doctrine.orm.default_entity_manager');
$update = $em->getRepository(Account::class)->findOneById($user->getId());
$update->setTempPlainPass('');
$em->flush();
}
}