Skip to content

Commit ff5e463

Browse files
committed
Add MockMvc-based integration tests for management.security.enabled
See gh-3997
1 parent a76e84a commit ff5e463

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

spring-boot-actuator/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,5 +312,10 @@
312312
<artifactId>spring-data-rest-webmvc</artifactId>
313313
<scope>test</scope>
314314
</dependency>
315+
<dependency>
316+
<groupId>org.springframework.security</groupId>
317+
<artifactId>spring-security-test</artifactId>
318+
<scope>test</scope>
319+
</dependency>
315320
</dependencies>
316321
</project>

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/MvcEndpointIntegrationTests.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,30 @@
2020
import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration;
2121
import org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration;
2222
import org.springframework.boot.actuate.autoconfigure.ManagementServerPropertiesAutoConfiguration;
23+
import org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration;
2324
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
2425
import org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration;
2526
import org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration;
2627
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
28+
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
2729
import org.springframework.boot.autoconfigure.test.ImportAutoConfiguration;
2830
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
2931
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
3032
import org.springframework.boot.test.EnvironmentTestUtils;
33+
import org.springframework.context.annotation.Import;
3134
import org.springframework.mock.web.MockServletContext;
3235
import org.springframework.test.web.servlet.MockMvc;
36+
import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder;
3337
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
38+
import org.springframework.test.web.servlet.setup.MockMvcConfigurer;
3439
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
3540

3641
import static org.hamcrest.Matchers.startsWith;
42+
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
3743
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
44+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
3845
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
46+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3947

4048
/**
4149
* Integration tests for the Actuator's MVC endpoints.
@@ -73,6 +81,24 @@ public void jsonResponsesCanBeIndentedWhenSpringDataRestIsAutoConfigured()
7381
assertIndentedJsonResponse(SpringDataRestConfiguration.class);
7482
}
7583

84+
@Test
85+
public void endpointsAreSecureByDefault() throws Exception {
86+
this.context = new AnnotationConfigWebApplicationContext();
87+
this.context.register(SecureConfiguration.class);
88+
MockMvc mockMvc = createSecureMockMvc();
89+
mockMvc.perform(get("/beans")).andExpect(status().isUnauthorized());
90+
}
91+
92+
@Test
93+
public void endpointSecurityCanBeDisabled() throws Exception {
94+
this.context = new AnnotationConfigWebApplicationContext();
95+
this.context.register(SecureConfiguration.class);
96+
EnvironmentTestUtils.addEnvironment(this.context,
97+
"management.security.enabled:false");
98+
MockMvc mockMvc = createSecureMockMvc();
99+
mockMvc.perform(get("/beans")).andDo(print()).andExpect(status().isOk());
100+
}
101+
76102
private void assertIndentedJsonResponse(Class<?> configuration) throws Exception {
77103
this.context = new AnnotationConfigWebApplicationContext();
78104
this.context.register(configuration);
@@ -84,9 +110,21 @@ private void assertIndentedJsonResponse(Class<?> configuration) throws Exception
84110
}
85111

86112
private MockMvc createMockMvc() {
113+
return doCreateMockMvc();
114+
}
115+
116+
private MockMvc createSecureMockMvc() {
117+
return doCreateMockMvc(springSecurity());
118+
}
119+
120+
private MockMvc doCreateMockMvc(MockMvcConfigurer... configurers) {
87121
this.context.setServletContext(new MockServletContext());
88122
this.context.refresh();
89-
return MockMvcBuilders.webAppContextSetup(this.context).build();
123+
DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.context);
124+
for (MockMvcConfigurer configurer : configurers) {
125+
builder.apply(configurer);
126+
}
127+
return builder.build();
90128
}
91129

92130
@ImportAutoConfiguration({ JacksonAutoConfiguration.class,
@@ -117,4 +155,11 @@ static class SpringDataRestConfiguration {
117155

118156
}
119157

158+
@Import(DefaultConfiguration.class)
159+
@ImportAutoConfiguration({ SecurityAutoConfiguration.class,
160+
ManagementWebSecurityAutoConfiguration.class })
161+
static class SecureConfiguration {
162+
163+
}
164+
120165
}

0 commit comments

Comments
 (0)