Skip to content

Commit 29ab9fd

Browse files
committed
Merge branch '2.4.x'
Closes gh-25351
2 parents df07bc5 + b5e1787 commit 29ab9fd

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/RemoteDevtoolsSecurityConfiguration.java

+15-17
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
package org.springframework.boot.devtools.autoconfigure;
1818

1919
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
20+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2021
import org.springframework.boot.autoconfigure.security.SecurityProperties;
2122
import org.springframework.boot.autoconfigure.web.ServerProperties;
2223
import org.springframework.context.annotation.Bean;
2324
import org.springframework.context.annotation.Configuration;
2425
import org.springframework.core.annotation.Order;
2526
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
27+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
2628
import org.springframework.security.web.SecurityFilterChain;
2729
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
2830

@@ -36,25 +38,21 @@
3638
@Configuration(proxyBeanMethods = false)
3739
class RemoteDevtoolsSecurityConfiguration {
3840

39-
@Configuration
40-
static class SecurityConfiguration {
41+
private final String url;
4142

42-
private final String url;
43-
44-
SecurityConfiguration(DevToolsProperties devToolsProperties, ServerProperties serverProperties) {
45-
ServerProperties.Servlet servlet = serverProperties.getServlet();
46-
String servletContextPath = (servlet.getContextPath() != null) ? servlet.getContextPath() : "";
47-
this.url = servletContextPath + devToolsProperties.getRemote().getContextPath() + "/restart";
48-
}
49-
50-
@Bean
51-
@Order(SecurityProperties.BASIC_AUTH_ORDER - 1)
52-
SecurityFilterChain configure(HttpSecurity http) throws Exception {
53-
http.requestMatcher(new AntPathRequestMatcher(this.url)).authorizeRequests().anyRequest().anonymous().and()
54-
.csrf().disable();
55-
return http.build();
56-
}
43+
RemoteDevtoolsSecurityConfiguration(DevToolsProperties devToolsProperties, ServerProperties serverProperties) {
44+
ServerProperties.Servlet servlet = serverProperties.getServlet();
45+
String servletContextPath = (servlet.getContextPath() != null) ? servlet.getContextPath() : "";
46+
this.url = servletContextPath + devToolsProperties.getRemote().getContextPath() + "/restart";
47+
}
5748

49+
@Bean
50+
@Order(SecurityProperties.BASIC_AUTH_ORDER - 1)
51+
@ConditionalOnMissingBean(WebSecurityConfigurerAdapter.class)
52+
SecurityFilterChain devtoolsSecurityFilterChain(HttpSecurity http) throws Exception {
53+
http.requestMatcher(new AntPathRequestMatcher(this.url)).authorizeRequests().anyRequest().anonymous().and()
54+
.csrf().disable();
55+
return http.build();
5856
}
5957

6058
}

spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/RemoteDevToolsAutoConfigurationTests.java

+32
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
import org.springframework.mock.web.MockHttpServletResponse;
4646
import org.springframework.mock.web.MockServletContext;
4747
import org.springframework.security.config.BeanIds;
48+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
49+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
4850
import org.springframework.test.web.servlet.MockMvc;
4951
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
5052
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
@@ -157,6 +159,7 @@ void securityConfigurationShouldAllowAccess() throws Exception {
157159
mockMvc.perform(MockMvcRequestBuilders.get(DEFAULT_CONTEXT_PATH + "/restart").header(DEFAULT_SECRET_HEADER_NAME,
158160
"supersecret")).andExpect(status().isOk());
159161
assertRestartInvoked(true);
162+
assertThat(this.context.containsBean("devtoolsSecurityFilterChain")).isTrue();
160163
}
161164

162165
@Test
@@ -182,6 +185,25 @@ void securityConfigurationDoesNotAffectOtherPaths() throws Exception {
182185
mockMvc.perform(MockMvcRequestBuilders.get("/my-path")).andExpect(status().isUnauthorized());
183186
}
184187

188+
@Test
189+
void securityConfigurationWhenWebSecurityConfigurerAdapterIsFound2() throws Exception {
190+
this.context = getContext(() -> {
191+
AnnotationConfigServletWebApplicationContext context = new AnnotationConfigServletWebApplicationContext();
192+
context.setServletContext(new MockServletContext());
193+
context.register(Config.class, PropertyPlaceholderAutoConfiguration.class,
194+
TestWebSecurityConfigurerAdapter.class);
195+
TestPropertyValues.of("spring.devtools.remote.secret:supersecret").applyTo(context);
196+
context.refresh();
197+
return context;
198+
});
199+
DispatcherFilter filter = this.context.getBean(DispatcherFilter.class);
200+
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context).apply(springSecurity()).addFilter(filter)
201+
.build();
202+
mockMvc.perform(MockMvcRequestBuilders.get(DEFAULT_CONTEXT_PATH + "/restart").header(DEFAULT_SECRET_HEADER_NAME,
203+
"supersecret")).andExpect(status().isOk());
204+
assertRestartInvoked(true);
205+
}
206+
185207
@Test
186208
void disableRestart() throws Exception {
187209
this.context = getContext(() -> loadContext("spring.devtools.remote.secret:supersecret",
@@ -250,6 +272,16 @@ HttpRestartServer remoteRestartHttpRestartServer() {
250272

251273
}
252274

275+
@Configuration(proxyBeanMethods = false)
276+
static class TestWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
277+
278+
@Override
279+
protected void configure(HttpSecurity http) throws Exception {
280+
http.antMatcher("/foo/**").authorizeRequests().anyRequest().authenticated().and().httpBasic();
281+
}
282+
283+
}
284+
253285
/**
254286
* Mock {@link HttpRestartServer} implementation.
255287
*/

0 commit comments

Comments
 (0)