【问题标题】:Generate Token on postPersist with FOSOAuthServerBundle使用 FOSOAuthServerBundle 在 postPersist 上生成令牌
【发布时间】:2019-01-27 12:57:58
【问题描述】:

我正在尝试在注册时创建一个令牌。 我使用FosUserBundle 作为身份验证器,使用FOSOAuthServerBundle 作为API。

当我手动生成令牌时一切正常。但我想在注册成功后自动生成它。所以我创建了一个名为EventListener 的TokenSetter

这里是代码

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”。

有没有办法获取普通密码或以不同的方式生成令牌?

【问题讨论】:

    标签: php api symfony fosuserbundle fosoauthserverbundle


    【解决方案1】:

    自己解决了。

    有两种方法可以解决这个问题。

    第一种方法是将password更改为client_credentials。这样,您在生成访问令牌时无需发送用户名和密码。不利的一面是user_id 的值将设置为NULL。

    第二种解决方案是创建一个不同的侦听器。 (RegistrationListener)

    您需要编辑UserEntity 并添加TempPlainPass getter 和setter。 基本上你需要做的是,将plainpassword 存储在数据库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();
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-08-09
      • 2015-04-10
      • 2017-01-03
      • 1970-01-01
      • 2021-09-28
      • 2018-02-01
      • 2018-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多