Polish LogoutWebFilter

This commit is contained in:
Rob Winch 2017-10-11 14:52:34 -05:00
parent af0a6efaab
commit 6366be9435

View File

@ -30,12 +30,16 @@ import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain; import org.springframework.web.server.WebFilterChain;
/** /**
* If the request matches, logs an authenticated user out by delegating to a
* {@link ServerLogoutHandler}.
*
* @author Rob Winch * @author Rob Winch
* @since 5.0 * @since 5.0
*/ */
public class LogoutWebFilter implements WebFilter { public class LogoutWebFilter implements WebFilter {
private AnonymousAuthenticationToken anonymousAuthenticationToken = new AnonymousAuthenticationToken("key", "anonymous", private AnonymousAuthenticationToken anonymousAuthenticationToken = new AnonymousAuthenticationToken("key", "anonymous",
AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS")); AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"));
private ServerLogoutHandler serverLogoutHandler = new SecurityContextServerLogoutHandler(); private ServerLogoutHandler serverLogoutHandler = new SecurityContextServerLogoutHandler();
private ServerWebExchangeMatcher requiresLogout = ServerWebExchangeMatchers private ServerWebExchangeMatcher requiresLogout = ServerWebExchangeMatchers
@ -46,23 +50,26 @@ public class LogoutWebFilter implements WebFilter {
return this.requiresLogout.matches(exchange) return this.requiresLogout.matches(exchange)
.filter( result -> result.isMatch()) .filter( result -> result.isMatch())
.switchIfEmpty(chain.filter(exchange).then(Mono.empty())) .switchIfEmpty(chain.filter(exchange).then(Mono.empty()))
.flatMap( result -> authentication(exchange)) .map(result -> exchange)
.flatMap( authentication -> this.serverLogoutHandler .flatMap(this::flatMapAuthentication)
.logout(new WebFilterExchange(exchange, chain), authentication)); .flatMap( authentication -> {
WebFilterExchange webFilterExchange = new WebFilterExchange(exchange,chain);
return this.serverLogoutHandler.logout(webFilterExchange, authentication);
});
} }
private Mono<Authentication> authentication(ServerWebExchange exchange) { private Mono<Authentication> flatMapAuthentication(ServerWebExchange exchange) {
return exchange.getPrincipal() return exchange.getPrincipal()
.cast(Authentication.class) .cast(Authentication.class)
.defaultIfEmpty(this.anonymousAuthenticationToken); .defaultIfEmpty(this.anonymousAuthenticationToken);
} }
public final void setServerLogoutHandler(ServerLogoutHandler serverLogoutHandler) { public void setServerLogoutHandler(ServerLogoutHandler serverLogoutHandler) {
Assert.notNull(serverLogoutHandler, "logoutHandler must not be null"); Assert.notNull(serverLogoutHandler, "logoutHandler must not be null");
this.serverLogoutHandler = serverLogoutHandler; this.serverLogoutHandler = serverLogoutHandler;
} }
public final void setRequiresLogout(ServerWebExchangeMatcher serverWebExchangeMatcher) { public void setRequiresLogout(ServerWebExchangeMatcher serverWebExchangeMatcher) {
Assert.notNull(serverWebExchangeMatcher, "serverWebExchangeMatcher must not be null"); Assert.notNull(serverWebExchangeMatcher, "serverWebExchangeMatcher must not be null");
this.requiresLogout = serverWebExchangeMatcher; this.requiresLogout = serverWebExchangeMatcher;
} }