mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-07-16 23:33:31 +00:00
Polish ServerLogoutSuccessHandler
Extract to be used by LogoutWebFilter Issue: gh-4616
This commit is contained in:
parent
0c830f9ba8
commit
8c7fa85107
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user