簡體   English   中英

Spring Security Java Config自定義注銷處理程序不起作用

[英]Spring Security Java Config Custom Logout Handler Not Working

我已經搜索過一個解決方案但是找不到任何一個,至少不是當前的一個或一個使用非基於xml的Spring和Spring Security配置。

我需要實現一個將在spring注銷處理程序之前使用的處理程序。 我已經閱讀了很多關於LogoutSuccessHandler的文章,但是在Logout Filter成功注銷后調用了這個文章,我需要訪問存儲在用戶會話中的用戶數據來執行一些數據庫條目,站點注銷信息等。這個會話一旦春天退出用戶就會丟失,所以它必須在此之前。

我已經嘗試創建自己的自定義注銷類,並在我的應用程序配置類中定義它,如下所示:

@Bean
public CustomLogoutHandler customLogoutHandler() {
    return new CustomLogoutHandler();
}

我的類擴展了LogoutHandler,就像Spring文檔所說:

public class CustomLogoutHandler extends LogoutHandler {

    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        // business logic here
    }
}

這仍然無效。 我在代碼中放了一個斷點,它永遠不會被拾取。 有沒有人知道可能導致這個或我需要做什么才能使它工作?

要使用自己的自定義注銷處理程序來實現Spring的LogoutHandler.class,您需要讓Spring知道您在使用.addLogoutHandler的注銷選項下的配置文件中使用自己的。 我想你錯過了這一步。 在安全配置文件中:

public class SecurityConfig extends WebSecurityConfigurerAdapter {  

    ... // Other methods here

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .otherConfigOptions
            .logout()
                .addLogoutHandler(customLogoutHandler())  <- custom handler
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .otherConfigOptions....
    }
}

並定義bean,我把它放在SecurityConfig.class中,但我認為你可以把它放在web或app配置類中,具體取決於你設置項目的方式。

@Bean
public CustomLogoutHandler customLogoutHandler() {
    return new CustomLogoutHandler();
}

然后,創建CustomLogoutHandler.class,確保實現LogoutHandler並覆蓋注銷方法。 在這里,您可以使用Authentication類訪問已添加到用戶請求范圍的任何內容。

public class CustomLogoutHandler implements LogoutHandler {
    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {

        // business logic here
    }
}

您還應該看一下這個問題,並回答有關Spring中自定義處理程序映射順序的內容。

我希望這有幫助。

暫無
暫無

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

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