我正在学习如何使用带有JSON登录的Symfony安全捆绑包。
因此我一直在遵循官方指南:https://symfony.com/doc/current/security.html#json-login
有趣的是,在通过身份验证后,我根本不需要发送生成的令牌,它似乎通过Session-Cookie记住了我。
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
作为一个初学者,我有以下问题:
- 假设您使用REST API进行身份验证,这是一个合理的行为吗?
- 如果我使用令牌,什么是可接受的创建方式?我可以只提供UID吗?
1条答案
按热度按时间zzwlnbp81#
对于令牌身份验证,您可以创建一个自定义的身份验证器。否则Symfony使用会话身份验证。这取决于前端的外观。您有办法在前端保存和重用会话吗?如果没有办法,您应该使用基于令牌的身份验证。
为了创建令牌,您可以为注册创建一个路由。这应该对匿名用户可用。这里是凭据(例如电子邮件和密码)。然后您创建一个令牌(最好是UUID)并将其保存在用户表中。最后,您在响应中发送回令牌。对于其他请求,您必须在报头中发送此令牌。然后,Symfony自动接管身份验证。
根据文档,首先创建一个验证器类。(https://symfony.com/doc/current/security/custom_authenticator.html)
然后您必须在配置中注册它。