Skip to content

Commit a4c088a

Browse files
orange-buffalojzheaux
authored andcommitted
Introducing WebSessionServerLogoutHandler
Closes gh-4838
1 parent cdc902d commit a4c088a

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.server.authentication.logout;
18+
19+
import reactor.core.publisher.Mono;
20+
21+
import org.springframework.security.core.Authentication;
22+
import org.springframework.security.web.server.WebFilterExchange;
23+
import org.springframework.web.server.WebSession;
24+
25+
/**
26+
* A {@link ServerLogoutHandler} which invalidates the active {@link WebSession}.
27+
*
28+
* @author Bogdan Ilchyshyn
29+
* @since 5.6
30+
*/
31+
public class WebSessionServerLogoutHandler implements ServerLogoutHandler {
32+
33+
@Override
34+
public Mono<Void> logout(WebFilterExchange exchange, Authentication authentication) {
35+
return exchange.getExchange().getSession().flatMap(WebSession::invalidate);
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.server.authentication.logout;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.extension.ExtendWith;
21+
import org.mockito.Mock;
22+
import org.mockito.junit.jupiter.MockitoExtension;
23+
import reactor.core.publisher.Mono;
24+
25+
import org.springframework.security.core.Authentication;
26+
import org.springframework.security.web.server.WebFilterExchange;
27+
import org.springframework.web.server.ServerWebExchange;
28+
import org.springframework.web.server.WebSession;
29+
30+
import static org.mockito.Mockito.doReturn;
31+
import static org.mockito.Mockito.mock;
32+
import static org.mockito.Mockito.verify;
33+
34+
@ExtendWith(MockitoExtension.class)
35+
public class WebSessionServerLogoutHandlerTests {
36+
37+
@Mock
38+
ServerWebExchange webExchange;
39+
40+
@Mock
41+
WebFilterExchange filterExchange;
42+
43+
@Mock
44+
WebSession webSession;
45+
46+
@Test
47+
public void shouldInvalidateWebSession() {
48+
doReturn(this.webExchange).when(this.filterExchange).getExchange();
49+
doReturn(Mono.just(this.webSession)).when(this.webExchange).getSession();
50+
doReturn(Mono.empty()).when(this.webSession).invalidate();
51+
52+
WebSessionServerLogoutHandler handler = new WebSessionServerLogoutHandler();
53+
handler.logout(this.filterExchange, mock(Authentication.class)).block();
54+
55+
verify(this.webSession).invalidate();
56+
}
57+
58+
}

0 commit comments

Comments
 (0)