forked from spring-projects/spring-security
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DelegatingServerLogoutSuccessHandler.
Closes spring-projectsgh-14813
- Loading branch information
Max Batischev
authored and
Max Batischev
committed
Mar 27, 2024
1 parent
e771267
commit 60f176e
Showing
2 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
...ework/security/web/server/authentication/logout/DelegatingServerLogoutSuccessHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.web.server.authentication.logout; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.web.server.WebFilterExchange; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* A {@link ServerLogoutSuccessHandler}, that iterates over multiple | ||
* {@link ServerLogoutSuccessHandler}. | ||
* | ||
* @author Max Batischev | ||
* @since 6.3 | ||
*/ | ||
public final class DelegatingServerLogoutSuccessHandler implements ServerLogoutSuccessHandler { | ||
|
||
private final List<ServerLogoutSuccessHandler> delegates; | ||
|
||
public DelegatingServerLogoutSuccessHandler(List<ServerLogoutSuccessHandler> delegates) { | ||
Assert.notEmpty(delegates, "delegates cannot be null"); | ||
this.delegates = new ArrayList<>(delegates); | ||
} | ||
|
||
public DelegatingServerLogoutSuccessHandler(ServerLogoutSuccessHandler... delegates) { | ||
Assert.notEmpty(delegates, "delegates cannot be null"); | ||
this.delegates = List.of(delegates); | ||
} | ||
|
||
@Override | ||
public Mono<Void> onLogoutSuccess(WebFilterExchange exchange, Authentication authentication) { | ||
return Flux.fromIterable(this.delegates) | ||
.concatMap((delegate) -> delegate.onLogoutSuccess(exchange, authentication)) | ||
.then(); | ||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
.../security/web/server/authentication/logout/DelegatingServerLogoutSuccessHandlerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.web.server.authentication.logout; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import reactor.test.publisher.PublisherProbe; | ||
|
||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.web.server.WebFilterExchange; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
|
||
/** | ||
* @author Max Batischev | ||
* @since 6.3 | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
public class DelegatingServerLogoutSuccessHandlerTests { | ||
|
||
@Mock | ||
private ServerLogoutSuccessHandler delegate1; | ||
|
||
@Mock | ||
private ServerLogoutSuccessHandler delegate2; | ||
|
||
private final PublisherProbe<Void> delegate1Result = PublisherProbe.empty(); | ||
|
||
private final PublisherProbe<Void> delegate2Result = PublisherProbe.empty(); | ||
|
||
@Mock | ||
private WebFilterExchange exchange; | ||
|
||
@Mock | ||
private Authentication authentication; | ||
|
||
private DelegatingServerLogoutSuccessHandler logoutSuccessHandler; | ||
|
||
@Test | ||
public void logoutWhenDelegate1AndDelegate2PresentThenExecuted() { | ||
given(this.delegate1.onLogoutSuccess(any(WebFilterExchange.class), any(Authentication.class))) | ||
.willReturn(this.delegate1Result.mono()); | ||
given(this.delegate2.onLogoutSuccess(any(WebFilterExchange.class), any(Authentication.class))) | ||
.willReturn(this.delegate2Result.mono()); | ||
this.logoutSuccessHandler = new DelegatingServerLogoutSuccessHandler(this.delegate1, this.delegate2); | ||
|
||
this.logoutSuccessHandler.onLogoutSuccess(this.exchange, this.authentication).block(); | ||
|
||
this.delegate1Result.assertWasSubscribed(); | ||
this.delegate2Result.assertWasSubscribed(); | ||
} | ||
|
||
@Test | ||
public void logoutWhenDelegate1PresentThenExecuted() { | ||
given(this.delegate1.onLogoutSuccess(any(WebFilterExchange.class), any(Authentication.class))) | ||
.willReturn(this.delegate1Result.mono()); | ||
this.logoutSuccessHandler = new DelegatingServerLogoutSuccessHandler(List.of(this.delegate1)); | ||
|
||
this.logoutSuccessHandler.onLogoutSuccess(this.exchange, this.authentication).block(); | ||
|
||
this.delegate1Result.assertWasSubscribed(); | ||
} | ||
|
||
} |