0

我有一个问题,或多或少和这个人一样:Symfony 3 authenticate user against remote API

但我的问题是,我该如何处理令牌?

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

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

谢谢!

4

2 回答 2

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']
于 2018-02-08T17:26:07.423 回答
1

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

于 2018-02-08T16:58:53.173 回答