2017-05-24 152 views
3

我有一個Symfony 3.2應用程序公開REST API並使用Json Web Tokens(JWT)進行身份驗證。我最近轉而使用Symfony的Guard組件。現在我security.yml包含防火牆配置部分如下(我使用的是Lexik JWT包2.4.0,但是這不應該的問題):爲什麼Symfony Guard在每次請求時觸發'security.interactive_login'事件?

firewalls: 
    # ... 
    api: 
     pattern: ^/api 
     stateless: true 
     guard: 
      authenticators: 
       - lexik_jwt_authentication.jwt_token_authenticator 

因爲我沒有這個開關,我注意到,每處理請求就好像用戶剛剛登錄一樣,即發起了一個security.interactive_login事件。 在文檔(http://symfony.com/doc/current/components/security/authentication.html#authentication-events)它規定:用戶必須經過積極 登錄到您的網站被觸發

的security.interactive_login事件。將此操作與 非交互式身份驗證方法區分開來非常重要,例如:基於「記住我」Cookie的 身份驗證,基於您的會話的 身份驗證,使用HTTP基本或HTTP摘要標頭的 身份驗證。 你可以聽上security.interactive_login事件,例如,在 爲了每次登錄的時間給你的用戶一個值得歡迎的閃光消息

所以我絕對不希望這個事件爲每一個請求 - 正如文檔中指出的那樣,我希望能夠在每個請求中獲得security.authentication.success事件。

然而,Symfony的的GuardAuthenticatorHandler類調​​度其authenticateWithToken方法security.interactive_login事件,而這個方法是由GuardAuthenticationListener每個請求調用。 這是Symfony中的一個錯誤,是我的錯誤理解還是錯誤配置?

(這不是一個哲學問題 - 在我的情況下,它會導致用戶的上次登錄時間是在每次請求,這是沒有意義的更新的具體問題)

回答

0

您應該更改

 stateless: false 
+0

你能詳細說明原因嗎?對我來說,聽起來像JWT認證的請求被用來進行無狀態的對話。 – LBA

+0

這是在這裏解釋http://symfony.com/doc/current/security/api_key_authentication.html#storing-authentication-in-the-session如果你設置無狀態的真,那麼你不會在會話中存儲身份驗證,你預計每次都會進行身份驗證。 – ste

相關問題