如何在 SpringBoot WebFlux 中使用 GET 请求注销

inq*_*ive 4 java spring-security spring-boot spring-webflux

如何配置securityWebFilterChain(ServerHttpSecurity http) 以便我的应用程序注销GET /logout

我有SpringBoot 2 Spring 5WebFlux

我试过:

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

Run Code Online (Sandbox Code Playgroud)

问题是, a 的LogoutPageGeneratingWebFilter位置早于LogoutWebFilter发出的 中的SecurityWebFilterChain。其中有一个硬编码.pathMatchers(HttpMethod.GET, "/logout")- 这导致我的应用程序总是在 GET 请求上发出 html 页面。

我发现没有办法抑制自动注销页面生成:(

Bar*_*ath 9

正如文档中提到的,

默认情况下,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();

    }
Run Code Online (Sandbox Code Playgroud)