簡體   English   中英

Symfony 6.2 和 json_login 不保存驗證 state

[英]Symfony 6.2 and json_login not save authenticated state

我有配置(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: App\Security\CustomAuthenticator
<?php

namespace App\Controller;

use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\CurrentUser;

#[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 安全文檔的某個地方,有信息表明登錄不應該有單獨的防火牆。 但是來自文檔:

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

因此,您正在login防火牆中進行身份驗證,但隨后導航到受main保護的任何端點將無法正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM