Skip to content

Commit 893b651

Browse files
RequestMatcherDelegatingWebInvocationPrivilegeEvaluator doesn't provided access to the ServletContext
Closes gh-10779
1 parent ca353d6 commit 893b651

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

web/src/main/java/org/springframework/security/web/access/RequestMatcherDelegatingWebInvocationPrivilegeEvaluator.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,12 +19,14 @@
1919
import java.util.Collections;
2020
import java.util.List;
2121

22+
import javax.servlet.ServletContext;
2223
import javax.servlet.http.HttpServletRequest;
2324

2425
import org.springframework.security.core.Authentication;
2526
import org.springframework.security.web.FilterInvocation;
2627
import org.springframework.security.web.util.matcher.RequestMatcherEntry;
2728
import org.springframework.util.Assert;
29+
import org.springframework.web.context.ServletContextAware;
2830

2931
/**
3032
* A {@link WebInvocationPrivilegeEvaluator} which delegates to a list of
@@ -34,10 +36,13 @@
3436
* @author Marcus Da Coregio
3537
* @since 5.5.5
3638
*/
37-
public final class RequestMatcherDelegatingWebInvocationPrivilegeEvaluator implements WebInvocationPrivilegeEvaluator {
39+
public final class RequestMatcherDelegatingWebInvocationPrivilegeEvaluator
40+
implements WebInvocationPrivilegeEvaluator, ServletContextAware {
3841

3942
private final List<RequestMatcherEntry<List<WebInvocationPrivilegeEvaluator>>> delegates;
4043

44+
private ServletContext servletContext;
45+
4146
public RequestMatcherDelegatingWebInvocationPrivilegeEvaluator(
4247
List<RequestMatcherEntry<List<WebInvocationPrivilegeEvaluator>>> requestMatcherPrivilegeEvaluatorsEntries) {
4348
Assert.notNull(requestMatcherPrivilegeEvaluatorsEntries, "requestMatcherPrivilegeEvaluators cannot be null");
@@ -110,7 +115,7 @@ public boolean isAllowed(String contextPath, String uri, String method, Authenti
110115
}
111116

112117
private List<WebInvocationPrivilegeEvaluator> getDelegate(String contextPath, String uri, String method) {
113-
FilterInvocation filterInvocation = new FilterInvocation(contextPath, uri, method);
118+
FilterInvocation filterInvocation = new FilterInvocation(contextPath, uri, method, this.servletContext);
114119
for (RequestMatcherEntry<List<WebInvocationPrivilegeEvaluator>> delegate : this.delegates) {
115120
if (delegate.getRequestMatcher().matches(filterInvocation.getHttpRequest())) {
116121
return delegate.getEntry();
@@ -119,4 +124,9 @@ private List<WebInvocationPrivilegeEvaluator> getDelegate(String contextPath, St
119124
return Collections.emptyList();
120125
}
121126

127+
@Override
128+
public void setServletContext(ServletContext servletContext) {
129+
this.servletContext = servletContext;
130+
}
131+
122132
}

web/src/test/java/org/springframework/security/web/access/RequestMatcherDelegatingWebInvocationPrivilegeEvaluatorTests.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,9 +20,13 @@
2020
import java.util.Collections;
2121
import java.util.List;
2222

23+
import javax.servlet.http.HttpServletRequest;
24+
2325
import org.junit.Before;
2426
import org.junit.Test;
27+
import org.mockito.ArgumentCaptor;
2528

29+
import org.springframework.mock.web.MockServletContext;
2630
import org.springframework.security.authentication.TestingAuthenticationToken;
2731
import org.springframework.security.core.Authentication;
2832
import org.springframework.security.web.util.matcher.RequestMatcher;
@@ -158,6 +162,23 @@ public void isAllowedWhenDifferentArgumentsThenCallSpecificIsAllowedInDelegate()
158162
verifyNoMoreInteractions(spyDeny);
159163
}
160164

165+
@Test
166+
public void isAllowedWhenServletContextIsSetThenPassedFilterInvocationHttpServletRequestHasServletContext() {
167+
Authentication token = new TestingAuthenticationToken("test", "Password", "MOCK_INDEX");
168+
MockServletContext servletContext = new MockServletContext();
169+
ArgumentCaptor<HttpServletRequest> argumentCaptor = ArgumentCaptor.forClass(HttpServletRequest.class);
170+
RequestMatcher requestMatcher = mock(RequestMatcher.class);
171+
WebInvocationPrivilegeEvaluator wipe = mock(WebInvocationPrivilegeEvaluator.class);
172+
RequestMatcherEntry<List<WebInvocationPrivilegeEvaluator>> delegate = new RequestMatcherEntry<>(requestMatcher,
173+
Collections.singletonList(wipe));
174+
RequestMatcherDelegatingWebInvocationPrivilegeEvaluator requestMatcherWipe = new RequestMatcherDelegatingWebInvocationPrivilegeEvaluator(
175+
Collections.singletonList(delegate));
176+
requestMatcherWipe.setServletContext(servletContext);
177+
requestMatcherWipe.isAllowed("/foo/index.jsp", token);
178+
verify(requestMatcher).matches(argumentCaptor.capture());
179+
assertThat(argumentCaptor.getValue().getServletContext()).isNotNull();
180+
}
181+
161182
@Test
162183
public void constructorWhenPrivilegeEvaluatorsNullThenException() {
163184
RequestMatcherEntry<List<WebInvocationPrivilegeEvaluator>> entry = new RequestMatcherEntry<>(this.alwaysMatch,

0 commit comments

Comments
 (0)