Skip to content

Commit 1c55b65

Browse files
Update DefaultWebInvocationPrivilegeEvaluator to use current ServletContext
Closes spring-projectsgh-10208
1 parent 1cfe849 commit 1c55b65

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

web/src/main/java/org/springframework/security/web/FilterInvocation.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2+
* Copyright 2002-2021 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.
@@ -29,6 +29,7 @@
2929
import java.util.Map;
3030

3131
import javax.servlet.FilterChain;
32+
import javax.servlet.ServletContext;
3233
import javax.servlet.ServletRequest;
3334
import javax.servlet.ServletResponse;
3435
import javax.servlet.http.HttpServletRequest;
@@ -78,10 +79,19 @@ public FilterInvocation(String servletPath, String method) {
7879
}
7980

8081
public FilterInvocation(String contextPath, String servletPath, String method) {
81-
this(contextPath, servletPath, null, null, method);
82+
this(contextPath, servletPath, method, null);
83+
}
84+
85+
public FilterInvocation(String contextPath, String servletPath, String method, ServletContext servletContext) {
86+
this(contextPath, servletPath, null, null, method, servletContext);
8287
}
8388

8489
public FilterInvocation(String contextPath, String servletPath, String pathInfo, String query, String method) {
90+
this(contextPath, servletPath, pathInfo, query, method, null);
91+
}
92+
93+
public FilterInvocation(String contextPath, String servletPath, String pathInfo, String query, String method,
94+
ServletContext servletContext) {
8595
DummyRequest request = new DummyRequest();
8696
contextPath = (contextPath != null) ? contextPath : "/cp";
8797
request.setContextPath(contextPath);
@@ -90,6 +100,7 @@ public FilterInvocation(String contextPath, String servletPath, String pathInfo,
90100
request.setPathInfo(pathInfo);
91101
request.setQueryString(query);
92102
request.setMethod(method);
103+
request.setServletContext(servletContext);
93104
this.request = request;
94105
}
95106

@@ -160,6 +171,8 @@ static class DummyRequest extends HttpServletRequestWrapper {
160171

161172
private String method;
162173

174+
private ServletContext servletContext;
175+
163176
private final HttpHeaders headers = new HttpHeaders();
164177

165178
private final Map<String, String[]> parameters = new LinkedHashMap<>();
@@ -290,6 +303,15 @@ void setParameter(String name, String... values) {
290303
this.parameters.put(name, values);
291304
}
292305

306+
@Override
307+
public ServletContext getServletContext() {
308+
return this.servletContext;
309+
}
310+
311+
void setServletContext(ServletContext servletContext) {
312+
this.servletContext = servletContext;
313+
}
314+
293315
}
294316

295317
static final class UnsupportedOperationExceptionInvocationHandler implements InvocationHandler {

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2+
* Copyright 2002-2021 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.
@@ -18,6 +18,8 @@
1818

1919
import java.util.Collection;
2020

21+
import javax.servlet.ServletContext;
22+
2123
import org.apache.commons.logging.Log;
2224
import org.apache.commons.logging.LogFactory;
2325

@@ -28,6 +30,7 @@
2830
import org.springframework.security.core.Authentication;
2931
import org.springframework.security.web.FilterInvocation;
3032
import org.springframework.util.Assert;
33+
import org.springframework.web.context.ServletContextAware;
3134

3235
/**
3336
* Allows users to determine whether they have privileges for a given web URI.
@@ -36,12 +39,14 @@
3639
* @author Luke Taylor
3740
* @since 3.0
3841
*/
39-
public class DefaultWebInvocationPrivilegeEvaluator implements WebInvocationPrivilegeEvaluator {
42+
public class DefaultWebInvocationPrivilegeEvaluator implements WebInvocationPrivilegeEvaluator, ServletContextAware {
4043

4144
protected static final Log logger = LogFactory.getLog(DefaultWebInvocationPrivilegeEvaluator.class);
4245

4346
private final AbstractSecurityInterceptor securityInterceptor;
4447

48+
private ServletContext servletContext;
49+
4550
public DefaultWebInvocationPrivilegeEvaluator(AbstractSecurityInterceptor securityInterceptor) {
4651
Assert.notNull(securityInterceptor, "SecurityInterceptor cannot be null");
4752
Assert.isTrue(FilterInvocation.class.equals(securityInterceptor.getSecureObjectClass()),
@@ -82,7 +87,7 @@ public boolean isAllowed(String uri, Authentication authentication) {
8287
@Override
8388
public boolean isAllowed(String contextPath, String uri, String method, Authentication authentication) {
8489
Assert.notNull(uri, "uri parameter is required");
85-
FilterInvocation filterInvocation = new FilterInvocation(contextPath, uri, method);
90+
FilterInvocation filterInvocation = new FilterInvocation(contextPath, uri, method, this.servletContext);
8691
Collection<ConfigAttribute> attributes = this.securityInterceptor.obtainSecurityMetadataSource()
8792
.getAttributes(filterInvocation);
8893
if (attributes == null) {
@@ -101,4 +106,9 @@ public boolean isAllowed(String contextPath, String uri, String method, Authenti
101106
}
102107
}
103108

109+
@Override
110+
public void setServletContext(ServletContext servletContext) {
111+
this.servletContext = servletContext;
112+
}
113+
104114
}

web/src/test/java/org/springframework/security/web/FilterInvocationTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2+
* Copyright 2002-2021 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.
@@ -24,6 +24,7 @@
2424

2525
import org.springframework.mock.web.MockHttpServletRequest;
2626
import org.springframework.mock.web.MockHttpServletResponse;
27+
import org.springframework.mock.web.MockServletContext;
2728
import org.springframework.security.web.FilterInvocation.DummyRequest;
2829
import org.springframework.security.web.util.UrlUtils;
2930

@@ -131,4 +132,14 @@ public void dummyRequestIsSupportedByUrlUtils() {
131132
UrlUtils.buildRequestUrl(request);
132133
}
133134

135+
@Test
136+
public void constructorWhenServletContextProvidedThenSetServletContextInRequest() {
137+
String contextPath = "";
138+
String servletPath = "/path";
139+
String method = "";
140+
MockServletContext mockServletContext = new MockServletContext();
141+
FilterInvocation filterInvocation = new FilterInvocation(contextPath, servletPath, method, mockServletContext);
142+
assertThat(filterInvocation.getRequest().getServletContext()).isSameAs(mockServletContext);
143+
}
144+
134145
}

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2+
* Copyright 2002-2021 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.
@@ -18,25 +18,30 @@
1818

1919
import org.junit.jupiter.api.BeforeEach;
2020
import org.junit.jupiter.api.Test;
21+
import org.mockito.ArgumentCaptor;
2122

2223
import org.springframework.context.ApplicationEventPublisher;
24+
import org.springframework.mock.web.MockServletContext;
2325
import org.springframework.security.access.AccessDecisionManager;
2426
import org.springframework.security.access.AccessDeniedException;
2527
import org.springframework.security.access.intercept.RunAsManager;
2628
import org.springframework.security.authentication.AuthenticationManager;
2729
import org.springframework.security.authentication.TestingAuthenticationToken;
2830
import org.springframework.security.core.Authentication;
2931
import org.springframework.security.core.context.SecurityContextHolder;
32+
import org.springframework.security.web.FilterInvocation;
3033
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
3134
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
3235

3336
import static org.assertj.core.api.Assertions.assertThat;
3437
import static org.mockito.ArgumentMatchers.any;
3538
import static org.mockito.ArgumentMatchers.anyList;
3639
import static org.mockito.ArgumentMatchers.anyObject;
40+
import static org.mockito.ArgumentMatchers.eq;
3741
import static org.mockito.BDDMockito.given;
3842
import static org.mockito.BDDMockito.willThrow;
3943
import static org.mockito.Mockito.mock;
44+
import static org.mockito.Mockito.verify;
4045

4146
/**
4247
* Tests
@@ -106,4 +111,17 @@ public void deniesAccessIfAccessDecisionManagerDoes() {
106111
assertThat(wipe.isAllowed("/foo/index.jsp", token)).isFalse();
107112
}
108113

114+
@Test
115+
public void isAllowedWhenServletContextIsSetThenPassedFilterInvocationHasServletContext() {
116+
Authentication token = new TestingAuthenticationToken("test", "Password", "MOCK_INDEX");
117+
MockServletContext servletContext = new MockServletContext();
118+
ArgumentCaptor<FilterInvocation> filterInvocationArgumentCaptor = ArgumentCaptor
119+
.forClass(FilterInvocation.class);
120+
DefaultWebInvocationPrivilegeEvaluator wipe = new DefaultWebInvocationPrivilegeEvaluator(this.interceptor);
121+
wipe.setServletContext(servletContext);
122+
wipe.isAllowed("/foo/index.jsp", token);
123+
verify(this.adm).decide(eq(token), filterInvocationArgumentCaptor.capture(), any());
124+
assertThat(filterInvocationArgumentCaptor.getValue().getRequest().getServletContext()).isNotNull();
125+
}
126+
109127
}

0 commit comments

Comments
 (0)