20
20
import org .springframework .boot .actuate .autoconfigure .EndpointAutoConfiguration ;
21
21
import org .springframework .boot .actuate .autoconfigure .EndpointWebMvcAutoConfiguration ;
22
22
import org .springframework .boot .actuate .autoconfigure .ManagementServerPropertiesAutoConfiguration ;
23
+ import org .springframework .boot .actuate .autoconfigure .ManagementWebSecurityAutoConfiguration ;
23
24
import org .springframework .boot .autoconfigure .PropertyPlaceholderAutoConfiguration ;
24
25
import org .springframework .boot .autoconfigure .data .rest .RepositoryRestMvcAutoConfiguration ;
25
26
import org .springframework .boot .autoconfigure .hateoas .HypermediaAutoConfiguration ;
26
27
import org .springframework .boot .autoconfigure .jackson .JacksonAutoConfiguration ;
28
+ import org .springframework .boot .autoconfigure .security .SecurityAutoConfiguration ;
27
29
import org .springframework .boot .autoconfigure .test .ImportAutoConfiguration ;
28
30
import org .springframework .boot .autoconfigure .web .HttpMessageConvertersAutoConfiguration ;
29
31
import org .springframework .boot .autoconfigure .web .WebMvcAutoConfiguration ;
30
32
import org .springframework .boot .test .EnvironmentTestUtils ;
33
+ import org .springframework .context .annotation .Import ;
31
34
import org .springframework .mock .web .MockServletContext ;
32
35
import org .springframework .test .web .servlet .MockMvc ;
36
+ import org .springframework .test .web .servlet .setup .DefaultMockMvcBuilder ;
33
37
import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
38
+ import org .springframework .test .web .servlet .setup .MockMvcConfigurer ;
34
39
import org .springframework .web .context .support .AnnotationConfigWebApplicationContext ;
35
40
36
41
import static org .hamcrest .Matchers .startsWith ;
42
+ import static org .springframework .security .test .web .servlet .setup .SecurityMockMvcConfigurers .springSecurity ;
37
43
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
44
+ import static org .springframework .test .web .servlet .result .MockMvcResultHandlers .print ;
38
45
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
46
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
39
47
40
48
/**
41
49
* Integration tests for the Actuator's MVC endpoints.
@@ -73,6 +81,24 @@ public void jsonResponsesCanBeIndentedWhenSpringDataRestIsAutoConfigured()
73
81
assertIndentedJsonResponse (SpringDataRestConfiguration .class );
74
82
}
75
83
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
+
76
102
private void assertIndentedJsonResponse (Class <?> configuration ) throws Exception {
77
103
this .context = new AnnotationConfigWebApplicationContext ();
78
104
this .context .register (configuration );
@@ -84,9 +110,21 @@ private void assertIndentedJsonResponse(Class<?> configuration) throws Exception
84
110
}
85
111
86
112
private MockMvc createMockMvc () {
113
+ return doCreateMockMvc ();
114
+ }
115
+
116
+ private MockMvc createSecureMockMvc () {
117
+ return doCreateMockMvc (springSecurity ());
118
+ }
119
+
120
+ private MockMvc doCreateMockMvc (MockMvcConfigurer ... configurers ) {
87
121
this .context .setServletContext (new MockServletContext ());
88
122
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 ();
90
128
}
91
129
92
130
@ ImportAutoConfiguration ({ JacksonAutoConfiguration .class ,
@@ -117,4 +155,11 @@ static class SpringDataRestConfiguration {
117
155
118
156
}
119
157
158
+ @ Import (DefaultConfiguration .class )
159
+ @ ImportAutoConfiguration ({ SecurityAutoConfiguration .class ,
160
+ ManagementWebSecurityAutoConfiguration .class })
161
+ static class SecureConfiguration {
162
+
163
+ }
164
+
120
165
}
0 commit comments