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

unc*_*ble 18 java spring spring-security

我正在使用Spring的Spring安全性,并且我使用URL(/logout)作为我的注销方法的端点.但在调用此方法后,它将我重定向到(/login?logout),我知道这是春天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();

}
Run Code Online (Sandbox Code Playgroud)

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

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

Tah*_*tar 31

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

http.logout().permitAll();
http.logout().logoutSuccessHandler((new HttpStatusReturningLogoutSuccessHandler(HttpStatus.OK)));
Run Code Online (Sandbox Code Playgroud)


Seb*_*ian 16

因为还没有接受的答案,我发布了我的解决方案,这对我有用:

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

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


Ahm*_*mad 2

使用这个方法:

.logout().logoutSuccessUrl("enter address here where you want to go after logout")
Run Code Online (Sandbox Code Playgroud)

  • OP 希望摆脱重定向,因为 /logout 是从 REST 客户端调用的(可能是来自浏览器的 AJAX 请求) (5认同)