1
0
mirror of synced 2026-08-04 17:27:13 +00:00

Automatically add CsrfServerLogoutHandler if csrf enabled

The configuration DSL should automatically add CsrfServerLogoutHandler if csrf is enabled

Fixes gh-5337
This commit is contained in:
Eric Deandrea
2018-05-24 14:16:04 -04:00
committed by Rob Winch
parent 725b3b5482
commit b060ec050a
5 changed files with 205 additions and 21 deletions
@@ -18,16 +18,17 @@ package org.springframework.security.web.server.authentication.logout;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import reactor.core.publisher.Mono;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.server.WebFilterExchange;
import org.springframework.util.Assert;
import reactor.core.publisher.Mono;
/**
* Delegates to a collection of {@link ServerLogoutHandler} implementations.
*
@@ -35,21 +36,24 @@ import reactor.core.publisher.Mono;
* @since 5.1
*/
public class DelegatingServerLogoutHandler implements ServerLogoutHandler {
private final List<ServerLogoutHandler> delegates;
private final List<ServerLogoutHandler> delegates = new ArrayList<>();
public DelegatingServerLogoutHandler(ServerLogoutHandler... delegates) {
Assert.notEmpty(delegates, "delegates cannot be null or empty");
this.delegates = Arrays.asList(delegates);
this.delegates.addAll(Arrays.asList(delegates));
}
public DelegatingServerLogoutHandler(List<ServerLogoutHandler> delegates) {
public DelegatingServerLogoutHandler(Collection<ServerLogoutHandler> delegates) {
Assert.notEmpty(delegates, "delegates cannot be null or empty");
this.delegates = new ArrayList<>(delegates);
this.delegates.addAll(delegates);
}
@Override
public Mono<Void> logout(WebFilterExchange exchange, Authentication authentication) {
Stream<Mono<Void>> results = this.delegates.stream().map(delegate -> delegate.logout(exchange, authentication));
return Mono.when(results.collect(Collectors.toList()));
return Mono.when(this.delegates.stream()
.filter(Objects::nonNull)
.map(delegate -> delegate.logout(exchange, authentication))
.collect(Collectors.toList())
);
}
}
@@ -16,17 +16,17 @@
package org.springframework.security.web.server.authentication.logout;
import org.springframework.http.HttpMethod;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.util.Assert;
import reactor.core.publisher.Mono;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.security.web.server.WebFilterExchange;
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher;
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatchers;
import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
@@ -85,6 +85,10 @@ public class LogoutWebFilter implements WebFilter {
this.logoutSuccessHandler = logoutSuccessHandler;
}
/**
* Sets the {@link ServerLogoutHandler}. The default is {@link SecurityContextServerLogoutHandler}.
* @param logoutHandler The handler to use
*/
public void setLogoutHandler(ServerLogoutHandler logoutHandler) {
Assert.notNull(logoutHandler, "logoutHandler must not be null");
this.logoutHandler = logoutHandler;
@@ -0,0 +1,86 @@
/*
* Copyright 2002-2018 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
*
* http://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 static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.test.util.ReflectionTestUtils;
/**
* @author Eric Deandrea
* @since 5.1
*/
@RunWith(MockitoJUnitRunner.class)
public class LogoutWebFilterTests {
@Mock
private ServerLogoutHandler handler1;
@Mock
private ServerLogoutHandler handler2;
@Mock
private ServerLogoutHandler handler3;
private LogoutWebFilter logoutWebFilter = new LogoutWebFilter();
@Test
public void defaultLogoutHandler() {
assertThat(getLogoutHandler())
.isNotNull()
.isExactlyInstanceOf(SecurityContextServerLogoutHandler.class);
}
@Test
public void singleLogoutHandler() {
this.logoutWebFilter.setLogoutHandler(this.handler1);
this.logoutWebFilter.setLogoutHandler(this.handler2);
assertThat(getLogoutHandler())
.isNotNull()
.isInstanceOf(ServerLogoutHandler.class)
.isNotInstanceOf(SecurityContextServerLogoutHandler.class)
.extracting(ServerLogoutHandler::getClass)
.isEqualTo(this.handler2.getClass());
}
@Test
public void multipleLogoutHandlers() {
this.logoutWebFilter.setLogoutHandler(new DelegatingServerLogoutHandler(this.handler1, this.handler2, this.handler3));
assertThat(getLogoutHandler())
.isNotNull()
.isExactlyInstanceOf(DelegatingServerLogoutHandler.class)
.extracting(delegatingLogoutHandler -> ((Collection<ServerLogoutHandler>) ReflectionTestUtils.getField(delegatingLogoutHandler, DelegatingServerLogoutHandler.class, "delegates"))
.stream()
.map(ServerLogoutHandler::getClass)
.collect(Collectors.toList()))
.isEqualTo(Arrays.asList(this.handler1.getClass(), this.handler2.getClass(), this.handler3.getClass()));
}
private ServerLogoutHandler getLogoutHandler() {
return (ServerLogoutHandler) ReflectionTestUtils.getField(this.logoutWebFilter, LogoutWebFilter.class, "logoutHandler");
}
}