Symfony 6.2和json_login不保存认证状态



我有配置(security.yaml):

firewalls:
login:
pattern: ^/api/login
json_login:
username_path: email
password_path: password
check_path: api_login

main:
login_throttling:
max_attempts: 3
lazy: true
provider: app_user_provider
custom_authenticator: AppSecurityCustomAuthenticator
<?php
namespace AppController;
use AppEntityUser;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyComponentSecurityHttpAttributeCurrentUser;
#[Route('/api', name: 'api_')]
class ApiController extends AbstractController
{
#[Route('/login', name: 'login', methods: ['POST'])]
public function login(#[CurrentUser] ?User $user): Response
{
if (null === $user) {
return $this->json([
'message' => 'missing credentials',
], Response::HTTP_UNAUTHORIZED);
}
return $this->json(['user' => $user->getUserIdentifier()]);
}
}

在调试面板中我看到Authenticated我看到真正的getUserIdentifier()。但如果我重新加载页面,我就不会再登录了。如果将json_login移到主块中,那么一切都可以正常工作。缺少了什么?

我尝试了不同的自定义授权器,但没有帮助。我还查看了github中的开放存储库,但所有示例都是相同的

您不必仅为登录路径创建单独的防火墙-在main防火墙中添加条目。

我现在找不到它,但我可以打赌,在Symfony安全文档的某个地方有信息,登录不应该有单独的防火墙。但是from docs:

Each firewall is like a separate security system, being authenticated in one firewall doesn't make you authenticated in another one.

因此,您正在login防火墙中进行身份验证,但随后导航到main保护的任何端点将无法工作。