Symfony根据远程API对用户进行身份验证

时间:2018-02-08 16:55:28

标签: php symfony authentication token guard

我有一个问题,或多或少与这个家伙相同:Symfony 3 authenticate user against remote API

但我的问题是,我该怎么处理令牌?

我使用了后卫,我做了一个api服务,我可以在那里检索令牌,但我可以在哪里存储它?我想设置一个cookie,但我不能从我的服务中编写cookie,否则我将无法启动会话,所以在答案中我不明白:“在前端存储中存储令牌”。

我真的没办法将令牌存放在cookie中吗?

谢谢!

2 个答案:

答案 0 :(得分:2)

为什么不能使用服务会话?将会话组件注入您的服务,您就可以了。

class TheService {
  public function __construct(SessionInterface $session) {
    $this->session = $session;
  }

  public function stuff() {
    $this->session->set('my_token', 'h3ll0');
  }
}

请参阅http://symfony.com/doc/current/components/http_foundation/sessions.html

如果您使用当前版本的Symfony,其自动装配功能将负责注入session服务,否则您需要在services.yml中配置服务:

services:
  my_service:
    class: AppBundle\Service\TheService
    public: true
    arguments: ['@session']

答案 1 :(得分:1)

它的意思不太清楚,但我认为最好的解决方案是将它存储在会话变量中。

相关问题