15
15
*/
16
16
package org .springframework .boot .actuate .autoconfigure .security .servlet ;
17
17
18
- import java .util .Arrays ;
19
- import java .util .Collection ;
20
- import java .util .Collections ;
21
- import java .util .HashSet ;
22
- import java .util .List ;
23
-
24
18
import org .glassfish .jersey .server .ResourceConfig ;
25
- import org .glassfish .jersey .server .model .Resource ;
26
19
import org .junit .Test ;
27
20
28
21
import org .springframework .boot .actuate .autoconfigure .endpoint .web .WebEndpointProperties ;
29
- import org .springframework .boot .actuate .endpoint .http .ActuatorMediaType ;
30
- import org .springframework .boot .actuate .endpoint .invoke .convert .ConversionServiceParameterValueMapper ;
31
- import org .springframework .boot .actuate .endpoint .web .EndpointLinksResolver ;
32
- import org .springframework .boot .actuate .endpoint .web .EndpointMapping ;
33
- import org .springframework .boot .actuate .endpoint .web .EndpointMediaTypes ;
34
- import org .springframework .boot .actuate .endpoint .web .annotation .WebEndpointDiscoverer ;
35
- import org .springframework .boot .actuate .endpoint .web .jersey .JerseyEndpointResourceFactory ;
36
22
import org .springframework .boot .autoconfigure .AutoConfigurations ;
37
- import org .springframework .boot .autoconfigure .jackson .JacksonAutoConfiguration ;
38
23
import org .springframework .boot .autoconfigure .jersey .JerseyAutoConfiguration ;
39
- import org .springframework .boot .autoconfigure .jersey .ResourceConfigCustomizer ;
40
- import org .springframework .boot .autoconfigure .security .servlet .SecurityAutoConfiguration ;
41
- import org .springframework .boot .autoconfigure .security .servlet .SecurityRequestMatcherProviderAutoConfiguration ;
42
- import org .springframework .boot .autoconfigure .security .servlet .UserDetailsServiceAutoConfiguration ;
43
24
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
44
25
import org .springframework .boot .test .context .FilteredClassLoader ;
45
26
import org .springframework .boot .test .context .runner .WebApplicationContextRunner ;
46
27
import org .springframework .boot .web .embedded .tomcat .TomcatServletWebServerFactory ;
47
28
import org .springframework .boot .web .servlet .context .AnnotationConfigServletWebServerApplicationContext ;
48
- import org .springframework .context .ApplicationContext ;
49
29
import org .springframework .context .annotation .Bean ;
50
30
import org .springframework .context .annotation .Configuration ;
51
31
import org .springframework .test .web .reactive .server .WebTestClient ;
57
37
*/
58
38
public class JerseyEndpointRequestIntegrationTests extends AbstractEndpointRequestIntegrationTests {
59
39
60
- @ Override
61
- protected WebApplicationContextRunner getContextRunner () {
62
- return new WebApplicationContextRunner (AnnotationConfigServletWebServerApplicationContext ::new )
63
- .withClassLoader (new FilteredClassLoader ("org.springframework.web.servlet.DispatcherServlet" ))
64
- .withUserConfiguration (JerseyEndpointConfiguration .class , SecurityConfiguration .class ,
65
- BaseConfiguration .class )
66
- .withConfiguration (AutoConfigurations .of (SecurityAutoConfiguration .class ,
67
- UserDetailsServiceAutoConfiguration .class ,
68
- SecurityRequestMatcherProviderAutoConfiguration .class , JacksonAutoConfiguration .class ,
69
- JerseyAutoConfiguration .class ));
70
- }
71
-
72
40
@ Test
73
41
public void toLinksWhenApplicationPathSetShouldMatch () {
74
42
getContextRunner ().withPropertyValues ("spring.jersey.application-path=/admin" ).run ((context ) -> {
@@ -98,16 +66,47 @@ public void toAnyEndpointWhenApplicationPathSetShouldMatch() {
98
66
});
99
67
}
100
68
69
+ @ Test
70
+ public void toAnyEndpointShouldMatchServletEndpoint () {
71
+ getContextRunner ().withPropertyValues ("spring.security.user.password=password" ,
72
+ "management.endpoints.web.exposure.include=se1" ).run ((context ) -> {
73
+ WebTestClient webTestClient = getWebTestClient (context );
74
+ webTestClient .get ().uri ("/actuator/se1" ).exchange ().expectStatus ().isUnauthorized ();
75
+ webTestClient .get ().uri ("/actuator/se1" ).header ("Authorization" , getBasicAuth ()).exchange ()
76
+ .expectStatus ().isOk ();
77
+ webTestClient .get ().uri ("/actuator/se1/list" ).exchange ().expectStatus ().isUnauthorized ();
78
+ webTestClient .get ().uri ("/actuator/se1/list" ).header ("Authorization" , getBasicAuth ()).exchange ()
79
+ .expectStatus ().isOk ();
80
+ });
81
+ }
82
+
83
+ @ Test
84
+ public void toAnyEndpointWhenApplicationPathSetShouldMatchServletEndpoint () {
85
+ getContextRunner ().withPropertyValues ("spring.jersey.application-path=/admin" ,
86
+ "spring.security.user.password=password" , "management.endpoints.web.exposure.include=se1" )
87
+ .run ((context ) -> {
88
+ WebTestClient webTestClient = getWebTestClient (context );
89
+ webTestClient .get ().uri ("/admin/actuator/se1" ).exchange ().expectStatus ().isUnauthorized ();
90
+ webTestClient .get ().uri ("/admin/actuator/se1" ).header ("Authorization" , getBasicAuth ()).exchange ()
91
+ .expectStatus ().isOk ();
92
+ webTestClient .get ().uri ("/admin/actuator/se1/list" ).exchange ().expectStatus ().isUnauthorized ();
93
+ webTestClient .get ().uri ("/admin/actuator/se1/list" ).header ("Authorization" , getBasicAuth ())
94
+ .exchange ().expectStatus ().isOk ();
95
+ });
96
+ }
97
+
98
+ @ Override
99
+ protected WebApplicationContextRunner createContextRunner () {
100
+ return new WebApplicationContextRunner (AnnotationConfigServletWebServerApplicationContext ::new )
101
+ .withClassLoader (new FilteredClassLoader ("org.springframework.web.servlet.DispatcherServlet" ))
102
+ .withUserConfiguration (JerseyEndpointConfiguration .class )
103
+ .withConfiguration (AutoConfigurations .of (JerseyAutoConfiguration .class ));
104
+ }
105
+
101
106
@ Configuration
102
107
@ EnableConfigurationProperties (WebEndpointProperties .class )
103
108
static class JerseyEndpointConfiguration {
104
109
105
- private final ApplicationContext applicationContext ;
106
-
107
- JerseyEndpointConfiguration (ApplicationContext applicationContext ) {
108
- this .applicationContext = applicationContext ;
109
- }
110
-
111
110
@ Bean
112
111
public TomcatServletWebServerFactory tomcat () {
113
112
return new TomcatServletWebServerFactory (0 );
@@ -118,24 +117,6 @@ public ResourceConfig resourceConfig() {
118
117
return new ResourceConfig ();
119
118
}
120
119
121
- @ Bean
122
- public ResourceConfigCustomizer webEndpointRegistrar () {
123
- return this ::customize ;
124
- }
125
-
126
- private void customize (ResourceConfig config ) {
127
- List <String > mediaTypes = Arrays .asList (javax .ws .rs .core .MediaType .APPLICATION_JSON ,
128
- ActuatorMediaType .V2_JSON );
129
- EndpointMediaTypes endpointMediaTypes = new EndpointMediaTypes (mediaTypes , mediaTypes );
130
- WebEndpointDiscoverer discoverer = new WebEndpointDiscoverer (this .applicationContext ,
131
- new ConversionServiceParameterValueMapper (), endpointMediaTypes ,
132
- Arrays .asList ((id ) -> id .toString ()), Collections .emptyList (), Collections .emptyList ());
133
- Collection <Resource > resources = new JerseyEndpointResourceFactory ().createEndpointResources (
134
- new EndpointMapping ("/actuator" ), discoverer .getEndpoints (), endpointMediaTypes ,
135
- new EndpointLinksResolver (discoverer .getEndpoints ()));
136
- config .registerResources (new HashSet <>(resources ));
137
- }
138
-
139
120
}
140
121
141
122
}
0 commit comments