public function onAuthenticationSuccess(Request $request, TokenInterface $token) { $userId = $token->getUser()->id; $forbidden = AuthenticationHelper::checkLoginForbidden($request); if ($forbidden['status'] == 'error') { $exception = new AuthenticationException($forbidden['message']); throw $exception; } else { $this->getUserService()->markLoginSuccess($userId, $request->getClientIp()); } $sessionId = $request->getSession()->getId(); //$sessionId = $this->createToken($request); $this->getUserService()->rememberLoginSessionId($userId, $sessionId); if ($request->isXmlHttpRequest()) { $content = array('success' => true); return new JsonResponse($content, 200); } if ($this->getAuthService()->hasPartnerAuth()) { $url = $this->httpUtils->generateUri($request, 'partner_login'); $queries = array('goto' => $this->determineTargetUrl($request)); $url = $url . '?' . http_build_query($queries); return $this->httpUtils->createRedirectResponse($request, $url); } return parent::onAuthenticationSuccess($request, $token); }
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) { $request->getSession()->set('_target_path', $request->request->get('_target_path')); if ($exception->getMessage() == "Bad credentials") { $message = "用户名或密码错误"; } else { goto end; } $default = array('temporary_lock_enabled' => 0, 'temporary_lock_allowed_times' => 5, 'ip_temporary_lock_allowed_times' => 20, 'temporary_lock_minutes' => 20); $setting = $this->getSettingService()->get('login_bind', array()); $setting = array_merge($default, $setting); if (empty($setting['temporary_lock_enabled'])) { goto end; } $forbidden = AuthenticationHelper::checkLoginForbidden($request); if ($forbidden['status'] == 'error') { $message = $forbidden['message']; $exception = new AuthenticationException($message); } else { $failed = $this->getUserService()->markLoginFailed($forbidden['user'] ? $forbidden['user']['id'] : 0, $request->getClientIp()); if ($forbidden['user']) { if ($failed['ipFaildCount'] >= $setting['ip_temporary_lock_allowed_times']) { $message = "您当前IP下帐号或密码输入错误过多,请在{$setting['temporary_lock_minutes']}分钟后再试。"; } elseif ($failed['leftFailedCount']) { $message = "帐号或密码错误,您还有{$failed['leftFailedCount']}次输入机会"; } else { $message = "帐号或密码输入错误过多,请在{$setting['temporary_lock_minutes']}分钟后再试,您可以通过找回并重置密码来解除封禁。"; } $exception = new AuthenticationException($message); } else { $message = $exception->getMessage(); } } end: if ($request->isXmlHttpRequest()) { $content = array('success' => false, 'message' => $message); return new JsonResponse($content, 400); } return parent::onAuthenticationFailure($request, $exception); }