Skip to content

Commit cecc1c8

Browse files
committed
Disable DevTools property defaults in production
Update `DevToolsPropertyDefaultsPostProcessor` so that property defaults are only added at development time. Properties are now added only when `Restarter` is initialize or remote devtools is enabled. Fixes gh-7014
1 parent c6657aa commit cecc1c8

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsPropertyDefaultsPostProcessor.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121
import java.util.Map;
2222

2323
import org.springframework.boot.SpringApplication;
24+
import org.springframework.boot.bind.RelaxedPropertyResolver;
25+
import org.springframework.boot.devtools.restart.Restarter;
2426
import org.springframework.boot.env.EnvironmentPostProcessor;
2527
import org.springframework.core.Ordered;
2628
import org.springframework.core.annotation.Order;
2729
import org.springframework.core.env.ConfigurableEnvironment;
30+
import org.springframework.core.env.Environment;
2831
import org.springframework.core.env.MapPropertySource;
2932
import org.springframework.core.env.PropertySource;
3033

@@ -59,7 +62,7 @@ public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostPro
5962
@Override
6063
public void postProcessEnvironment(ConfigurableEnvironment environment,
6164
SpringApplication application) {
62-
if (isLocalApplication(environment)) {
65+
if (isLocalApplication(environment) && canAddProperties(environment)) {
6366
PropertySource<?> propertySource = new MapPropertySource("refresh",
6467
PROPERTIES);
6568
environment.getPropertySources().addLast(propertySource);
@@ -70,4 +73,24 @@ private boolean isLocalApplication(ConfigurableEnvironment environment) {
7073
return environment.getPropertySources().get("remoteUrl") == null;
7174
}
7275

76+
private boolean canAddProperties(Environment environment) {
77+
return isRestarterInitialized() || isRemoteRestartEnabled(environment);
78+
}
79+
80+
private boolean isRestarterInitialized() {
81+
try {
82+
Restarter restarter = Restarter.getInstance();
83+
return (restarter != null && restarter.getInitialUrls() != null);
84+
}
85+
catch (Exception ex) {
86+
return false;
87+
}
88+
}
89+
90+
private boolean isRemoteRestartEnabled(Environment environment) {
91+
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(environment,
92+
"spring.devtools.remote.");
93+
return resolver.containsProperty("secret");
94+
}
95+
7396
}

spring-boot-devtools/src/test/java/org/springframework/boot/devtools/env/DevToolPropertiesIntegrationTests.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,20 @@
1616

1717
package org.springframework.boot.devtools.env;
1818

19+
import java.net.URL;
20+
import java.util.Collections;
21+
1922
import org.junit.After;
23+
import org.junit.Before;
24+
import org.junit.Rule;
2025
import org.junit.Test;
26+
import org.junit.rules.ExpectedException;
2127

28+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2229
import org.springframework.boot.SpringApplication;
2330
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
31+
import org.springframework.boot.devtools.restart.RestartInitializer;
32+
import org.springframework.boot.devtools.restart.Restarter;
2433
import org.springframework.context.ConfigurableApplicationContext;
2534
import org.springframework.context.annotation.Bean;
2635
import org.springframework.context.annotation.Configuration;
@@ -32,13 +41,22 @@
3241
*/
3342
public class DevToolPropertiesIntegrationTests {
3443

44+
@Rule
45+
public ExpectedException thrown = ExpectedException.none();
46+
3547
private ConfigurableApplicationContext context;
3648

49+
@Before
50+
public void setup() {
51+
Restarter.initialize(new String[] {}, false, new MockInitializer(), false);
52+
}
53+
3754
@After
3855
public void cleanup() {
3956
if (this.context != null) {
4057
this.context.close();
4158
}
59+
Restarter.clearInstance();
4260
}
4361

4462
@Test
@@ -59,6 +77,33 @@ public void beanMethodPropertyConditionIsAffectedByDevToolProperties() {
5977
this.context.getBean(MyBean.class);
6078
}
6179

80+
@Test
81+
public void postProcessWhenRestarterDisabledAndRemoteSecretNotSetShouldNotAddPropertySource()
82+
throws Exception {
83+
Restarter.clearInstance();
84+
Restarter.disable();
85+
SpringApplication application = new SpringApplication(
86+
BeanConditionConfiguration.class);
87+
application.setWebEnvironment(false);
88+
this.context = application.run();
89+
this.thrown.expect(NoSuchBeanDefinitionException.class);
90+
this.context.getBean(MyBean.class);
91+
}
92+
93+
@Test
94+
public void postProcessWhenRestarterDisabledAndRemoteSecretSetShouldAddPropertySource()
95+
throws Exception {
96+
Restarter.clearInstance();
97+
Restarter.disable();
98+
SpringApplication application = new SpringApplication(
99+
BeanConditionConfiguration.class);
100+
application.setWebEnvironment(false);
101+
application.setDefaultProperties(Collections.<String, Object>singletonMap(
102+
"spring.devtools.remote.secret", "donttell"));
103+
this.context = application.run();
104+
this.context.getBean(MyBean.class);
105+
}
106+
62107
@Configuration
63108
@ConditionalOnProperty("spring.h2.console.enabled")
64109
static class ClassConditionConfiguration {
@@ -79,4 +124,12 @@ static class MyBean {
79124

80125
}
81126

127+
static class MockInitializer implements RestartInitializer {
128+
129+
@Override
130+
public URL[] getInitialUrls(Thread thread) {
131+
return new URL[] {};
132+
}
133+
134+
}
82135
}

0 commit comments

Comments
 (0)