Polish ServerLogoutSuccessHandler

Extract to be used by LogoutWebFilter

Issue: gh-4616
This commit is contained in:
Rob Winch 2017-10-12 14:29:51 -05:00
parent 0c830f9ba8
commit 8c7fa85107
4 changed files with 23 additions and 17 deletions

View File

@ -42,6 +42,8 @@ public class LogoutWebFilter implements WebFilter {
private ServerLogoutHandler serverLogoutHandler = new SecurityContextServerLogoutHandler();
private ServerLogoutSuccessHandler serverLogoutSuccessHandler = new RedirectServerLogoutSuccessHandler();
private ServerWebExchangeMatcher requiresLogout = ServerWebExchangeMatchers
.pathMatchers("/logout");
@ -54,7 +56,7 @@ public class LogoutWebFilter implements WebFilter {
.flatMap(this::flatMapAuthentication)
.flatMap( authentication -> {
WebFilterExchange webFilterExchange = new WebFilterExchange(exchange,chain);
return this.serverLogoutHandler.logout(webFilterExchange, authentication);
return logout(webFilterExchange, authentication);
});
}
@ -64,6 +66,21 @@ public class LogoutWebFilter implements WebFilter {
.defaultIfEmpty(this.anonymousAuthenticationToken);
}
private Mono<Void> logout(WebFilterExchange webFilterExchange, Authentication authentication) {
return this.serverLogoutHandler.logout(webFilterExchange, authentication)
.then(this.serverLogoutSuccessHandler.onLogoutSuccess(webFilterExchange, authentication));
}
/**
* Sets the {@link ServerLogoutSuccessHandler}. The default is {@link RedirectServerLogoutSuccessHandler}.
* @param serverLogoutSuccessHandler the handler to use
*/
public void setServerLogoutSuccessHandler(
ServerLogoutSuccessHandler serverLogoutSuccessHandler) {
Assert.notNull(serverLogoutSuccessHandler, "serverLogoutSuccessHandler cannot be null");
this.serverLogoutSuccessHandler = serverLogoutSuccessHandler;
}
public void setServerLogoutHandler(ServerLogoutHandler serverLogoutHandler) {
Assert.notNull(serverLogoutHandler, "logoutHandler must not be null");
this.serverLogoutHandler = serverLogoutHandler;

View File

@ -16,6 +16,7 @@
package org.springframework.security.web.server.authentication.logout;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.server.DefaultServerRedirectStrategy;
import org.springframework.security.web.server.ServerRedirectStrategy;
import org.springframework.security.web.server.WebFilterExchange;
@ -36,7 +37,7 @@ public class RedirectServerLogoutSuccessHandler implements ServerLogoutSuccessHa
private ServerRedirectStrategy serverRedirectStrategy = new DefaultServerRedirectStrategy();
@Override
public Mono<Void> onLogoutSuccess(WebFilterExchange exchange) {
public Mono<Void> onLogoutSuccess(WebFilterExchange exchange, Authentication authentication) {
return this.serverRedirectStrategy
.sendRedirect(exchange.getExchange(), this.logoutSuccessUrl);
}

View File

@ -37,23 +37,10 @@ import java.net.URI;
public class SecurityContextServerLogoutHandler implements ServerLogoutHandler {
private ServerSecurityContextRepository serverSecurityContextRepository = new WebSessionServerSecurityContextRepository();
private ServerLogoutSuccessHandler serverLogoutSuccessHandler = new RedirectServerLogoutSuccessHandler();
@Override
public Mono<Void> logout(WebFilterExchange exchange,
Authentication authentication) {
return this.serverSecurityContextRepository.save(exchange.getExchange(), null)
.then(this.serverLogoutSuccessHandler.onLogoutSuccess(exchange));
}
/**
* Sets the {@link ServerLogoutSuccessHandler}. The default is {@link RedirectServerLogoutSuccessHandler}.
* @param serverLogoutSuccessHandler the handler to use
*/
public void setServerLogoutSuccessHandler(
ServerLogoutSuccessHandler serverLogoutSuccessHandler) {
Assert.notNull(serverLogoutSuccessHandler, "serverLogoutSuccessHandler cannot be null");
this.serverLogoutSuccessHandler = serverLogoutSuccessHandler;
return this.serverSecurityContextRepository.save(exchange.getExchange(), null);
}
/**

View File

@ -16,6 +16,7 @@
package org.springframework.security.web.server.authentication.logout;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.server.WebFilterExchange;
import reactor.core.publisher.Mono;
@ -25,5 +26,5 @@ import reactor.core.publisher.Mono;
*/
public interface ServerLogoutSuccessHandler {
Mono<Void> onLogoutSuccess(WebFilterExchange exchange);
Mono<Void> onLogoutSuccess(WebFilterExchange exchange, Authentication authentication);
}