2016-04-01 53 views
8

我使用彈簧安全性與REST,並使用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(); 

} 

我試圖用HttpStatusReturningLogoutSuccessHandler,但它沒有工作,甚至設置logoutSuccessUrl()不會改變任何東西。

你知道我該如何禁用此重定向?

回答

2

使用此方法:

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

由於正在從REST客戶端調用/註銷(可能是來自瀏覽器的AJAX請求),OP希望擺脫重定向 –

+1

我已經試過這個,還有重定向 – uncallable

13

下面的代碼對我的作品(請注意,它不具有logout().disable()

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

我已經試過了,還是得到了重定向 – uncallable

+1

嘗試設置sp的日誌級別環安全性來調試,它可能會給你一個提示。如果使用spring引導,您可以將'logging.level.org.springframework.security = DEBUG'添加到application.properties –

+0

@uncallable您是否使用它?我有類似的問題。試過所有解決方案,沒有任何工作。 – ddd

0

美孚那些誰使用XML配置,這裏是片段相當於由Tahir Akhtar給出的那個。

<http>元素,配置<logout>元素如下:

<logout 
    logout-url   = "/some/path/for/logout" 
    invalidate-session = "true" 
    delete-cookies  = "JSESSIONID" 
    success-handler-ref = "httpStatusReturningLogoutSuccessHandler" 
/> 

,並按如下定義httpStatusReturningLogoutSuccessHandler豆:

<bean 
    id  = "httpStatusReturningLogoutSuccessHandler" 
    class = "org.springframework.security.web.authentication.logout.HttpStatusReturningLogoutSuccessHandler" 
/> 
1

你可能想試試這個

http.logou t()。logoutRequestMatcher(new AntPathRequestMatcher(「/ thisistomisleadlogoutfilter」));

這有效地將/iistomisleadlogoutfilter重定向到登錄?註銷。因此,你應該能夠使用/註銷,而不是

3

所以,因爲沒有公認的答案,我後我的解決方案,它爲我工作:

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

只返回一個乾淨HTTP_OK(200)後,成功註銷 - 在這種情況下,spring將不會重定向您