Skip to content

Commit 48acaa4

Browse files
committed
Make AutoConfigureMockMvc use SecurityProperties' filter ordering
Previously, AutoConfigureMockMvc used Spring Security's default filter ordering, ignoring the value configured by SecurityProperties that is used at runtime. This resulted in different ordering at runtime and in tests. This commit updates the configuration for AutoConfigureMockMvc to import the Spring Security filter auto-configuration, thereby ensuring that the ordering configured via SecurityProperties is applied. Fixes gh-21801
1 parent 36faa1d commit 48acaa4

File tree

4 files changed

+116
-2
lines changed

4 files changed

+116
-2
lines changed

spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ org.springframework.boot.test.autoconfigure.web.servlet.MockMvcAutoConfiguration
109109
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcWebClientAutoConfiguration,\
110110
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcWebDriverAutoConfiguration,\
111111
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,\
112+
org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfiguration,\
112113
org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration,\
113114
org.springframework.boot.test.autoconfigure.web.servlet.MockMvcSecurityConfiguration
114115

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2012-2020 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.boot.test.autoconfigure.web.servlet.mockmvc;
18+
19+
import java.io.IOException;
20+
import java.security.Principal;
21+
22+
import javax.servlet.Filter;
23+
import javax.servlet.FilterChain;
24+
import javax.servlet.ServletException;
25+
import javax.servlet.ServletRequest;
26+
import javax.servlet.ServletResponse;
27+
import javax.servlet.http.HttpServletRequest;
28+
29+
import org.springframework.boot.autoconfigure.security.SecurityProperties;
30+
import org.springframework.core.Ordered;
31+
32+
/**
33+
* {@link Filter} that is ordered to run after Spring Security's filter.
34+
*
35+
* @author Andy Wilkinson
36+
*/
37+
public class AfterSecurityFilter implements Filter, Ordered {
38+
39+
@Override
40+
public int getOrder() {
41+
return SecurityProperties.DEFAULT_FILTER_ORDER + 1;
42+
}
43+
44+
@Override
45+
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
46+
throws IOException, ServletException {
47+
Principal principal = ((HttpServletRequest) request).getUserPrincipal();
48+
if (principal == null) {
49+
throw new ServletException("No user principal");
50+
}
51+
response.getWriter().write(principal.getName());
52+
response.getWriter().flush();
53+
}
54+
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2012-2020 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.boot.test.autoconfigure.web.servlet.mockmvc;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
23+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
24+
import org.springframework.context.annotation.Import;
25+
import org.springframework.security.test.context.support.WithMockUser;
26+
import org.springframework.test.web.servlet.MockMvc;
27+
28+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
29+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
31+
32+
/**
33+
* Tests for {@link AutoConfigureMockMvc @AutoConfigureMockMvc} and the ordering of Spring
34+
* Security's filter
35+
*
36+
* @author Andy Wilkinson
37+
*/
38+
@WebMvcTest
39+
@WithMockUser(username = "user", password = "secret")
40+
@Import(AfterSecurityFilter.class)
41+
class AutoConfigureMockMvcSecurityFilterOrderingIntegrationTests {
42+
43+
@Autowired
44+
private MockMvc mvc;
45+
46+
@Test
47+
void afterSecurityFilterShouldFindAUserPrincipal() throws Exception {
48+
this.mvc.perform(get("/one")).andExpect(status().isOk()).andExpect(content().string("user"));
49+
}
50+
51+
}

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/ExampleFilter.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -26,7 +26,9 @@
2626
import javax.servlet.ServletResponse;
2727
import javax.servlet.http.HttpServletResponse;
2828

29+
import org.springframework.boot.autoconfigure.security.SecurityProperties;
2930
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
31+
import org.springframework.core.Ordered;
3032
import org.springframework.stereotype.Component;
3133

3234
/**
@@ -35,7 +37,7 @@
3537
* @author Phillip Webb
3638
*/
3739
@Component
38-
public class ExampleFilter implements Filter {
40+
public class ExampleFilter implements Filter, Ordered {
3941

4042
@Override
4143
public void init(FilterConfig filterConfig) throws ServletException {
@@ -52,4 +54,9 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
5254
public void destroy() {
5355
}
5456

57+
@Override
58+
public int getOrder() {
59+
return SecurityProperties.DEFAULT_FILTER_ORDER - 1;
60+
}
61+
5562
}

0 commit comments

Comments
 (0)