在函数中看到“ isGranted”时,令牌有问题

在Symfony 4.2中创建功能测试时,令牌存在问题。

当我使用搜寻器对象与表单进行交互时,就会出现问题。首先,我创建令牌,该令牌将允许我进入应用程序。令牌在进入需要权限的操作时便会发挥作用,但是在代码执行过程中,如果它再次具有特定的进入权限或是否能够进入,则会遇到if评估为“ isGranted”的情况不经过。您必须对其进行评估时,令牌会丢失。为什么会这样呢?安全文件配置是否错误? ...

public function addCriteriosPropios ()
{
  try {
    if ($this->security->isGranted('ROLE_accESS')) {
      return;
    } else {
         ...   
    }
} catch (\Throwable $e) {
    echo $e;
    }
} 

Test Code
public function testBotonBuscar() {
  $this->logIn();

  $crawler = $this->client->request('POST','/Comm/buscador');

  $form = $crawler->selectButton('Buscar')->form();
  $form['exp']->setvalue('');
  $this->client->submit($form);
}


protected function loginIn() {
  $firewall = 'main';

  $token = new usernamePasswordToken($this->usuarioTest,null,$firewall,(array) $roles);


  $this->client->getcontainer()->get('security.token_storage')->setToken($token);

  $session = $this->client->getcontainer()->get('session');

  $session->set('_security_' . $firewall,serialize($token));
  $session->set('cuenta.id',$this->cuentaTest->getId());
  $session->set('cliente.id',$this->cuentaTest->getcliente()->getId());

  $cookie = new Cookie($session->getName(),$session->getId());

  $this->client->getcookieJar()->set($cookie);

  $session->save();
}

有1个失败:

1)App\Tests\Controller\Comm\BuscadorControllerTest::testBotonBuscar

无法断言类型为“异常”的异常与预期的异常“ Symfony\Component\Security\Core\Exception\accessDeniedException”匹配。 消息是:“

  

令牌存储区不包含身份验证令牌。一种可能   原因可能是没有为此URL配置防火墙。”   /var/www/html/wetender/vendor/symfony/security-core/Authorization/AuthorizationChecker.php:49

/var/www/html/wetender/vendor/symfony/security-core/Security.php:70

/var/www/html/wetender/src/Utils/LicitacionesUtils.php:125

/var/www/html/wetender/src/Utils/CommUtils.php:269

/var/www/html/wetender/src/Controller/Comm/BuscadorController.php:99

/var/www/html/wetender/vendor/symfony/http-kernel/HttpKernel.php:150

/var/www/html/wetender/vendor/symfony/http-kernel/HttpKernel.php:67

/var/www/html/wetender/vendor/symfony/http-kernel/Kernel.php:198

/var/www/html/wetender/vendor/symfony/http-kernel/Client.php:68

/var/www/html/wetender/vendor/symfony/framework-bundle/Client.php:131

/var/www/html/wetender/vendor/symfony/browser-kit/Client.php:405

/var/www/html/wetender/vendor/symfony/browser-kit/Client.php:325

/var/www/html/wetender/tests/Controller/Comm/BuscadorControllerTest.php:116
fengzi3688 回答:在函数中看到“ isGranted”时,令牌有问题

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3159817.html

大家都在问