所以我会尽量明确:
我的 symfony 项目中有一个中间件,它应该增加会话变量 (RequestStack::getSession()) 中的连接尝试次数,这在 Postman 中工作得很好,问题是它不能与 Fetch 一起使用, “调试”后返回的会话似乎为空。我怀疑当 Symfony 创建会话时 PHPSESSID 没有存储在我的 Fetch 中
中间件
public function __construct(JWTEncoderInterface $jwtEncoder, RequestStack $requestStack, ManagerRegistry $doctrine)
{
$this->jwtEncoder = $jwtEncoder;
$this->requestStack = $requestStack;
$this->session = $this->requestStack->getSession();
$this->doctrine = $doctrine;
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): JsonResponse
{
// debug
// return new JsonResponse([$this->session->all(), 'error' => true, 'message' => 'Authentication failed'], 401);
// check if login is restricted
$this->loginResricted();
// increment attempt in session
$this->incrementAttempt($request);
return new JsonResponse(['error' => true, 'message' => 'Authentication failed'], 401);
}
我尝试调试会话,但没有输出任何内容,但我需要保持用户会话处于活动状态,直到他尝试登录
所以,如果有人正在寻找我的问题,我找到了一个替代方案:
两种方式:
如果你有兴趣我让我的方案:
用户尝试
{
id: int,
ip: string,
logins: array,
delay: int,
created_at: datetime,
updated_at: datetime
}
您将能够根据用户的 IP 跟踪用户尝试。