浏览代码

Add DelegatingSecurityContextRepository

Issue gh-12023
Steve Riesenberg 2 年之前
父节点
当前提交
acc35aeb18

+ 111 - 0
web/src/main/java/org/springframework/security/web/context/DelegatingSecurityContextRepository.java

@@ -0,0 +1,111 @@
+/*
+ * Copyright 2002-2022 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.context;
+
+import java.util.Arrays;
+import java.util.List;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.springframework.security.core.context.DeferredSecurityContext;
+import org.springframework.security.core.context.SecurityContext;
+import org.springframework.util.Assert;
+
+/**
+ * @author Steve Riesenberg
+ * @author Josh Cummings
+ * @since 5.8
+ */
+public final class DelegatingSecurityContextRepository implements SecurityContextRepository {
+
+	private final List<SecurityContextRepository> delegates;
+
+	public DelegatingSecurityContextRepository(SecurityContextRepository... delegates) {
+		this(Arrays.asList(delegates));
+	}
+
+	public DelegatingSecurityContextRepository(List<SecurityContextRepository> delegates) {
+		Assert.notEmpty(delegates, "delegates cannot be empty");
+		this.delegates = delegates;
+	}
+
+	@Override
+	public SecurityContext loadContext(HttpRequestResponseHolder requestResponseHolder) {
+		return loadContext(requestResponseHolder.getRequest()).get();
+	}
+
+	@Override
+	public DeferredSecurityContext loadDeferredContext(HttpServletRequest request) {
+		DeferredSecurityContext deferredSecurityContext = null;
+		for (SecurityContextRepository delegate : this.delegates) {
+			if (deferredSecurityContext == null) {
+				deferredSecurityContext = delegate.loadDeferredContext(request);
+			}
+			else {
+				DeferredSecurityContext next = delegate.loadDeferredContext(request);
+				deferredSecurityContext = new DelegatingDeferredSecurityContext(deferredSecurityContext, next);
+			}
+		}
+		return deferredSecurityContext;
+	}
+
+	@Override
+	public void saveContext(SecurityContext context, HttpServletRequest request, HttpServletResponse response) {
+		for (SecurityContextRepository delegate : this.delegates) {
+			delegate.saveContext(context, request, response);
+		}
+	}
+
+	@Override
+	public boolean containsContext(HttpServletRequest request) {
+		for (SecurityContextRepository delegate : this.delegates) {
+			if (delegate.containsContext(request)) {
+				return true;
+			}
+		}
+		return false;
+	}
+
+	static final class DelegatingDeferredSecurityContext implements DeferredSecurityContext {
+
+		private final DeferredSecurityContext previous;
+
+		private final DeferredSecurityContext next;
+
+		DelegatingDeferredSecurityContext(DeferredSecurityContext previous, DeferredSecurityContext next) {
+			this.previous = previous;
+			this.next = next;
+		}
+
+		@Override
+		public SecurityContext get() {
+			SecurityContext securityContext = this.previous.get();
+			if (!this.previous.isGenerated()) {
+				return securityContext;
+			}
+			return this.next.get();
+		}
+
+		@Override
+		public boolean isGenerated() {
+			return this.previous.isGenerated() && this.next.isGenerated();
+		}
+
+	}
+
+}

+ 144 - 0
web/src/test/java/org/springframework/security/web/context/DelegatingSecurityContextRepositoryTests.java

@@ -0,0 +1,144 @@
+/*
+ * Copyright 2002-2022 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.context;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+import org.springframework.security.authentication.TestingAuthenticationToken;
+import org.springframework.security.core.context.DeferredSecurityContext;
+import org.springframework.security.core.context.SecurityContext;
+import org.springframework.security.core.context.SecurityContextHolderStrategy;
+import org.springframework.security.core.context.SecurityContextImpl;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.BDDMockito.given;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+
+/**
+ * Tests for {@link DelegatingSecurityContextRepository}.
+ *
+ * @author Steve Riesenberg
+ * @since 5.8
+ */
+public class DelegatingSecurityContextRepositoryTests {
+
+	private MockHttpServletRequest request;
+
+	private MockHttpServletResponse response;
+
+	private SecurityContextHolderStrategy strategy;
+
+	private SecurityContext securityContext;
+
+	@BeforeEach
+	public void setUp() {
+		this.request = new MockHttpServletRequest();
+		this.response = new MockHttpServletResponse();
+		this.strategy = mock(SecurityContextHolderStrategy.class);
+		this.securityContext = mock(SecurityContext.class);
+	}
+
+	@ParameterizedTest
+	@CsvSource({ "0,false", "1,false", "2,false", "-1,true" })
+	public void loadDeferredContextWhenIsGeneratedThenReturnsSecurityContext(int expectedIndex, boolean isGenerated) {
+		SecurityContext actualSecurityContext = new SecurityContextImpl(
+				new TestingAuthenticationToken("user", "password"));
+		SecurityContext emptySecurityContext = new SecurityContextImpl();
+		given(this.strategy.createEmptyContext()).willReturn(emptySecurityContext);
+		List<SecurityContextRepository> delegates = new ArrayList<>();
+		for (int i = 0; i < 3; i++) {
+			SecurityContext context = (i == expectedIndex) ? actualSecurityContext : null;
+			SecurityContextRepository repository = mock(SecurityContextRepository.class);
+			SupplierDeferredSecurityContext supplier = new SupplierDeferredSecurityContext(() -> context,
+					this.strategy);
+			given(repository.loadDeferredContext(this.request)).willReturn(supplier);
+			delegates.add(repository);
+		}
+
+		DelegatingSecurityContextRepository repository = new DelegatingSecurityContextRepository(delegates);
+		DeferredSecurityContext deferredSecurityContext = repository.loadDeferredContext(this.request);
+		SecurityContext expectedSecurityContext = (isGenerated) ? emptySecurityContext : actualSecurityContext;
+		assertThat(deferredSecurityContext.get()).isEqualTo(expectedSecurityContext);
+		assertThat(deferredSecurityContext.isGenerated()).isEqualTo(isGenerated);
+
+		for (SecurityContextRepository delegate : delegates) {
+			verify(delegate).loadDeferredContext(this.request);
+			verifyNoMoreInteractions(delegate);
+		}
+	}
+
+	@Test
+	public void saveContextAlwaysCallsDelegates() {
+		List<SecurityContextRepository> delegates = new ArrayList<>();
+		for (int i = 0; i < 3; i++) {
+			SecurityContextRepository repository = mock(SecurityContextRepository.class);
+			delegates.add(repository);
+		}
+
+		DelegatingSecurityContextRepository repository = new DelegatingSecurityContextRepository(delegates);
+		repository.saveContext(this.securityContext, this.request, this.response);
+		for (SecurityContextRepository delegate : delegates) {
+			verify(delegate).saveContext(this.securityContext, this.request, this.response);
+			verifyNoMoreInteractions(delegate);
+		}
+	}
+
+	@Test
+	public void containsContextWhenAllDelegatesReturnFalseThenReturnsFalse() {
+		List<SecurityContextRepository> delegates = new ArrayList<>();
+		for (int i = 0; i < 3; i++) {
+			SecurityContextRepository repository = mock(SecurityContextRepository.class);
+			given(repository.containsContext(this.request)).willReturn(false);
+			delegates.add(repository);
+		}
+
+		DelegatingSecurityContextRepository repository = new DelegatingSecurityContextRepository(delegates);
+		assertThat(repository.containsContext(this.request)).isFalse();
+		for (SecurityContextRepository delegate : delegates) {
+			verify(delegate).containsContext(this.request);
+			verifyNoMoreInteractions(delegate);
+		}
+	}
+
+	@Test
+	public void containsContextWhenFirstDelegatesReturnTrueThenReturnsTrue() {
+		List<SecurityContextRepository> delegates = new ArrayList<>();
+		for (int i = 0; i < 3; i++) {
+			SecurityContextRepository repository = mock(SecurityContextRepository.class);
+			given(repository.containsContext(this.request)).willReturn(true);
+			delegates.add(repository);
+		}
+
+		DelegatingSecurityContextRepository repository = new DelegatingSecurityContextRepository(delegates);
+		assertThat(repository.containsContext(this.request)).isTrue();
+		verify(delegates.get(0)).containsContext(this.request);
+		verifyNoInteractions(delegates.get(1));
+		verifyNoInteractions(delegates.get(2));
+	}
+
+}