簡體   English   中英

在身份驗證之前偵聽請求,或者在symfony3中重寫/ login_check

[英]listen to request before authentication or rewrite /login_check in symfony3

我正在嘗試注冊一個eventListener,它將在/ login_check嘗試登錄用戶之前被調用。

我正在編寫DDoS保護,每次嘗試都在數據庫中iIlog(日期,user_id,is_failure),如果用戶有超過N次錯誤的登錄嘗試,我會生成一個通過電子郵件發送給正確的用戶電子郵件的令牌。 沒有此令牌的任何人都將被禁止在10分鍾內嘗試再次登錄。

要繼續,我需要:

  • 要么能夠在/ login_check的開頭注冊一個eventListener
  • 要么能夠重寫/ login_check以添加事件

我沒有發現有關“ pre_authentication”的任何事件,您有解決方案嗎?

我不會在存儲庫方法中編寫代碼來吸引用戶,這不是它的位置。

謝謝

幾天前我也遇到了類似的問題。 就像您說的那樣,即使在嘗試進行身份驗證之前,我也無法執行合適的“ pre_authentication”,這時我仍可以執行檢查。 (在我的情況下,AuthenticationSuccess和AuthenticationFailure Handler不是一個選項,因為我想在嘗試之前阻止嘗試)

但是最后,我找到了一種適用於我的情況的方法(盡管可能有更好的方法,但我找不到)。

如果您的應用程序使用默認的用戶名/密碼身份驗證,則可以執行以下操作:

  1. 擴展UsernamePasswordFormAuthenticationListener

     class UsernamePasswordFormAuthenticationListener extends \\Symfony\\Component\\Security\\Http\\Firewall\\UsernamePasswordFormAuthenticationListener { /** @var EntityManagerInterface */ protected $entityManager; /** * setter is called through DI container * * @param EntityManagerInterface $entityManager */ public function setEntityManager(EntityManagerInterface $entityManager) { $this->entityManager = $entityManager; } /** * * @param Request $request * * @return null|RedirectResponse|\\Symfony\\Component\\HttpFoundation\\Response|\\Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface */ protected function attemptAuthentication(Request $request) { //do your logic and whatnot here // iE return a redirect repsonse if the token is needed but missing return parent::attemptAuthentication($request); } } 
  2. 覆蓋您的services.yml中的原始服務

     security.authentication.listener.form: class: AppBundle\\Listener\\UsernamePasswordFormAuthenticationListener parent: security.authentication.listener.abstract abstract: true calls: [ [setEntityManager, ["@doctrine.orm.entity_manager"]] ] 

(此處使用setter注入是因為構造函數需要大量參數)

也許這種方法可以滿足您的需求,但是沒有人有更好的主意

暫無
暫無

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

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