簡體   English   中英

如何在 SpringBoot WebFlux 中使用 GET 請求注銷

[英]How to logout with GET request in SpringBoot WebFlux

我如何配置securityWebFilterChain(ServerHttpSecurity http)以便我的應用程序在GET /logoutGET /logout

我有SpringBoot 2 Spring 5WebFlux

我試過:

  http
    .logout()
      .requiresLogout(ServerWebExchangeMatchers.pathMatchers(HttpMethod.GET, "/logout"))
      .logoutSuccessHandler(logoutSuccessHandler("/after-life"))

問題是, LogoutPageGeneratingWebFilter比發出的SecurityWebFilterChainLogoutWebFilter更早。 其中有一個硬編碼的.pathMatchers(HttpMethod.GET, "/logout") - 這導致我的應用程序總是在 GET 請求上發出一個 html 頁面。

我發現沒有辦法抑制自動注銷頁面的生成:(

文檔中所述,

默認情況下,Spring Security 將在“/login”處生成一個登錄頁面,在“/logout”處生成一個注銷頁面。 如果這是自定義的: 不再提供默認的登錄和注銷頁面 應用程序必須在提供的 URL 處呈現登錄頁面 應用程序必須在提供的 URL 處呈現身份驗證錯誤頁面 + “?error” 身份驗證將發生POST 到提供的 URL

自定義配置具有默認登錄和沒有默認注銷。

    @Bean
    public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity httpSecurity){

        LoginPageGeneratingWebFilter loginpage= new LoginPageGeneratingWebFilter();
        loginpage.setFormLoginEnabled(true);
        return httpSecurity
                .addFilterAt(loginpage, SecurityWebFiltersOrder.LOGIN_PAGE_GENERATING)
                .authorizeExchange()
                    .pathMatchers("/home").authenticated()
                        .and().formLogin()                      
                            .loginPage("/login")                         
                        .and()
                        .logout()
                        .logoutUrl("/logout").requiresLogout(ServerWebExchangeMatchers.pathMatchers(HttpMethod.GET, "/logout"))
                        .and()

                .build();

    }

我有同樣的問題,但我使用的是 OAuth2Login 並且該應用程序位於反向代理后面,剝離了前綴並使用了ForwardedHeaderTransformer 一切正常,但注銷頁面具有硬編碼路徑/logout因此無法添加自定義前綴。 我的解決方案是將注銷 url 更改為/logout-oidc

http.logout(logout -> logout
            .requiresLogout(ServerWebExchangeMatchers.pathMatchers(HttpMethod.GET, "/logout-oidc"));

糟糕的是,沒有可以禁用LogoutPageGeneratingWebFilter方法setLogoutPageGenerating(boolean enable)

暫無
暫無

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

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