Spring安全性 – 禁用注销重定向

我正在使用Spring的Spring安全性,并且我使用URL( /logout )作为我的注销方法的端点。 但在调用此方法后,它将我重定向到( /login?logout ),我知道这是spring logOutSuccessUrl 。 我想摆脱重定向。 这是我的代码:

 protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/login").permitAll() .anyRequest().fullyAuthenticated() .and().requiresChannel().anyRequest().requiresSecure() .and().httpBasic().disable().logout() .disable() // .logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler(HttpStatus.OK)) .csrf().disable(); } 

我试图使用HttpStatusReturningLogoutSuccessHandler但它没有工作,甚至设置logoutSuccessUrl()也没有改变任何东西。

你知道我怎么能禁用这个重定向?

以下代码适用于我(注意它没有logout().disable()

 http.logout().permitAll(); http.logout().logoutSuccessHandler((new HttpStatusReturningLogoutSuccessHandler(HttpStatus.OK))); 

所以既然没有接受的答案,我发布我的解决方案,这对我有用:

 .logout() .logoutUrl("/api/user/logout") .permitAll() .logoutSuccessHandler((httpServletRequest, httpServletResponse, authentication) -> { httpServletResponse.setStatus(HttpServletResponse.SC_OK); }) .and() 

成功注销后返回一个干净的HTTP_OK(200) – 在这种情况下spring不会重定向你

使用此方法:

 .logout().logoutSuccessUrl("enter address here where you want to go after logout") 

你可能想试试这个

http.logout()。logoutRequestMatcher(new AntPathRequestMatcher(“/ thisistomisleadlogoutfilter”));

这有效地重定向/ thisistomisleadlogoutfilter登录?注销。 因此,您应该可以使用/ logout代替

Foo那些使用XML配置的人,这里是Tahir Akhtar给出的等效片段。

元素中,按如下方式配置元素:

  

并定义httpStatusReturningLogoutSuccessHandler bean,如下所示: