2015-02-10 54 views
3

使用Symfony 2,我正在尋找更多關於您可以在安全配置文件app/config/security.ymlofficial documentation)中定義的處理程序的信息。該文檔不提供任何有關處理程序的信息。這裏是安全文件的摘錄:Symfony 2:安全配置:登錄和註銷處理程序

# app/config/security.yml 

security:   
    ... 

    firewalls:        
      somename: 

       form_login: 
        ... 

        # login failure redirecting options (read further below) 
        failure_path: /foo 
        failure_forward: false 
        failure_path_parameter: _failure_path 
        failure_handler: some.service.id 
        success_handler: some.service.id 


       logout: 
        path: /logout 
        target:/
        invalidate_session: false 
        delete_cookies: 
         a: { path: null, domain: null } 
         b: { path: null, domain: null } 
        handlers: [some.service.id, another.service.id] 
        success_handler: some.service.id 
       anonymous: ~ 

在這兩種form_login註銷部分有一個success_handler場。此外,對於註銷部分,您可以使用handlers字段定義多個處理程序。

我有兩個問題:

  1. 如果我定義了一個succes_handler服務(例如使用AuthenticationSuccessHandlerInterface或LogoutHandlerInterface),將其在此改變的框架所提供的默認成功處理程序?

  2. 對於註銷部分配置,如何在handlers字段中工作?

+0

檢查[this](https://stackoverflow.com/questions/28400632/symfony-2-after-login-do-some-extra-job/28403893#28403893)的答案。希望得到這個幫助 – Matteo 2015-02-10 08:43:04

+0

@Matteo謝謝。我已經閱讀了很多關於這個主題的帖子,但是我找不到任何確切的信息。我想知道的是,如果我定義自己的succes處理程序,它是否會覆蓋默認的處理程序,或者是否需要擴展此[post]中所述的默認處理程序(http://stackoverflow.com/questions/15918617/ symfony2-extend-defaultauthenticationsuccesshandler)? – Cruz 2015-02-10 08:50:01

+0

我還沒有嘗試,但我認爲沒有 – Matteo 2015-02-10 08:52:05

回答

4

有關信息,在註銷app/config/security.yml部分:

handlers: [some.service.id, another.service.id] =>在這裏,你必須定義服務實施Symfony\Component\Security\Http\Logout\LogoutHandlerInterface。這些句柄不需要返回響應。在我的情況下,我創建了一個簡單的處理程序,在註銷時創建一條Flash消息。

success_handler: some.service.id =>在這裏你必須定義一個服務實現=>Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface。這個處理程序必須返回一個響應。該處理程序由Symfony\Component\Security\Http\Firewall\LogoutListener(防火牆偵聽器)的構造函數調用。

+0

回答你的第一個問題:是的,通過創建自定義'succes_handler'服務,你將覆蓋默認的一個。然而,默認的註銷處理程序是'DefaultLogoutSuccessHandler',它所做的只是將其重定向到'註銷目標'路由,以便覆蓋它。 請將此答覆標記爲正確答案 – gondo 2015-04-21 07:49:21

0

我成功嘗試下一個解決方案 https://gist.github.com/marydn/8061424 好像是你正在嘗試做的。

+0

謝謝。是的,這就是我想要做的。我沒有問題如何做到這一點。我想確保使用這種方法我不會覆蓋Symfony中提供的默認成功處理程序(因爲文檔不是很有用)。 – Cruz 2015-02-10 13:46:47

+0

更具體地說,我想在登錄/註銷事件中添加一條flash消息,所以我不想影響響應。對於登錄,可以使用事件偵聽器(偵聽security.interactive_login)並註銷實現LogoutHandlerInterface的註銷成功處理程序(因爲沒有註銷事件)。該接口允許避免返回響應(通過反對LogoutSuccessHandlerInterface)。 – Cruz 2015-02-10 13:56:55