1515 */
1616package org .springframework .boot .actuate .autoconfigure .security .servlet ;
1717
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-
2418import org .glassfish .jersey .server .ResourceConfig ;
25- import org .glassfish .jersey .server .model .Resource ;
2619import org .junit .jupiter .api .Test ;
2720
2821import org .springframework .boot .actuate .autoconfigure .endpoint .web .WebEndpointProperties ;
29- import org .springframework .boot .actuate .endpoint .EndpointId ;
30- import org .springframework .boot .actuate .endpoint .http .ActuatorMediaType ;
31- import org .springframework .boot .actuate .endpoint .invoke .convert .ConversionServiceParameterValueMapper ;
32- import org .springframework .boot .actuate .endpoint .web .EndpointLinksResolver ;
33- import org .springframework .boot .actuate .endpoint .web .EndpointMapping ;
34- import org .springframework .boot .actuate .endpoint .web .EndpointMediaTypes ;
35- import org .springframework .boot .actuate .endpoint .web .annotation .WebEndpointDiscoverer ;
36- import org .springframework .boot .actuate .endpoint .web .jersey .JerseyEndpointResourceFactory ;
3722import org .springframework .boot .autoconfigure .AutoConfigurations ;
38- import org .springframework .boot .autoconfigure .jackson .JacksonAutoConfiguration ;
3923import org .springframework .boot .autoconfigure .jersey .JerseyAutoConfiguration ;
40- import org .springframework .boot .autoconfigure .jersey .ResourceConfigCustomizer ;
41- import org .springframework .boot .autoconfigure .security .servlet .SecurityAutoConfiguration ;
42- import org .springframework .boot .autoconfigure .security .servlet .SecurityRequestMatcherProviderAutoConfiguration ;
43- import org .springframework .boot .autoconfigure .security .servlet .UserDetailsServiceAutoConfiguration ;
4424import org .springframework .boot .context .properties .EnableConfigurationProperties ;
4525import org .springframework .boot .test .context .FilteredClassLoader ;
4626import org .springframework .boot .test .context .runner .WebApplicationContextRunner ;
4727import org .springframework .boot .web .embedded .tomcat .TomcatServletWebServerFactory ;
4828import org .springframework .boot .web .servlet .context .AnnotationConfigServletWebServerApplicationContext ;
49- import org .springframework .context .ApplicationContext ;
5029import org .springframework .context .annotation .Bean ;
5130import org .springframework .context .annotation .Configuration ;
5231import org .springframework .test .web .reactive .server .WebTestClient ;
5837 */
5938class JerseyEndpointRequestIntegrationTests extends AbstractEndpointRequestIntegrationTests {
6039
61- @ Override
62- protected WebApplicationContextRunner getContextRunner () {
63- return new WebApplicationContextRunner (AnnotationConfigServletWebServerApplicationContext ::new )
64- .withClassLoader (new FilteredClassLoader ("org.springframework.web.servlet.DispatcherServlet" ))
65- .withUserConfiguration (JerseyEndpointConfiguration .class , SecurityConfiguration .class ,
66- BaseConfiguration .class )
67- .withConfiguration (AutoConfigurations .of (SecurityAutoConfiguration .class ,
68- UserDetailsServiceAutoConfiguration .class ,
69- SecurityRequestMatcherProviderAutoConfiguration .class , JacksonAutoConfiguration .class ,
70- JerseyAutoConfiguration .class ));
71- }
72-
7340 @ Test
7441 void toLinksWhenApplicationPathSetShouldMatch () {
7542 getContextRunner ().withPropertyValues ("spring.jersey.application-path=/admin" ).run ((context ) -> {
@@ -99,15 +66,46 @@ void toAnyEndpointWhenApplicationPathSetShouldMatch() {
9966 });
10067 }
10168
102- @ Configuration (proxyBeanMethods = false )
103- @ EnableConfigurationProperties (WebEndpointProperties .class )
104- static class JerseyEndpointConfiguration {
69+ @ Test
70+ 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+ }
10582
106- private final ApplicationContext applicationContext ;
83+ @ Test
84+ 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+ }
10797
108- JerseyEndpointConfiguration (ApplicationContext applicationContext ) {
109- this .applicationContext = applicationContext ;
110- }
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+
106+ @ Configuration
107+ @ EnableConfigurationProperties (WebEndpointProperties .class )
108+ static class JerseyEndpointConfiguration {
111109
112110 @ Bean
113111 TomcatServletWebServerFactory tomcat () {
@@ -119,24 +117,6 @@ ResourceConfig resourceConfig() {
119117 return new ResourceConfig ();
120118 }
121119
122- @ Bean
123- ResourceConfigCustomizer webEndpointRegistrar () {
124- return this ::customize ;
125- }
126-
127- private void customize (ResourceConfig config ) {
128- List <String > mediaTypes = Arrays .asList (javax .ws .rs .core .MediaType .APPLICATION_JSON ,
129- ActuatorMediaType .V2_JSON );
130- EndpointMediaTypes endpointMediaTypes = new EndpointMediaTypes (mediaTypes , mediaTypes );
131- WebEndpointDiscoverer discoverer = new WebEndpointDiscoverer (this .applicationContext ,
132- new ConversionServiceParameterValueMapper (), endpointMediaTypes ,
133- Arrays .asList (EndpointId ::toString ), Collections .emptyList (), Collections .emptyList ());
134- Collection <Resource > resources = new JerseyEndpointResourceFactory ().createEndpointResources (
135- new EndpointMapping ("/actuator" ), discoverer .getEndpoints (), endpointMediaTypes ,
136- new EndpointLinksResolver (discoverer .getEndpoints ()), true );
137- config .registerResources (new HashSet <>(resources ));
138- }
139-
140120 }
141121
142122}
0 commit comments