mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
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
This commit is contained in:
parent
c6657aaf12
commit
cecc1c8817
@ -21,10 +21,13 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.bind.RelaxedPropertyResolver;
|
||||
import org.springframework.boot.devtools.restart.Restarter;
|
||||
import org.springframework.boot.env.EnvironmentPostProcessor;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
|
||||
@ -59,7 +62,7 @@ public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostPro
|
||||
@Override
|
||||
public void postProcessEnvironment(ConfigurableEnvironment environment,
|
||||
SpringApplication application) {
|
||||
if (isLocalApplication(environment)) {
|
||||
if (isLocalApplication(environment) && canAddProperties(environment)) {
|
||||
PropertySource<?> propertySource = new MapPropertySource("refresh",
|
||||
PROPERTIES);
|
||||
environment.getPropertySources().addLast(propertySource);
|
||||
@ -70,4 +73,24 @@ public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostPro
|
||||
return environment.getPropertySources().get("remoteUrl") == null;
|
||||
}
|
||||
|
||||
private boolean canAddProperties(Environment environment) {
|
||||
return isRestarterInitialized() || isRemoteRestartEnabled(environment);
|
||||
}
|
||||
|
||||
private boolean isRestarterInitialized() {
|
||||
try {
|
||||
Restarter restarter = Restarter.getInstance();
|
||||
return (restarter != null && restarter.getInitialUrls() != null);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isRemoteRestartEnabled(Environment environment) {
|
||||
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(environment,
|
||||
"spring.devtools.remote.");
|
||||
return resolver.containsProperty("secret");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,11 +16,20 @@
|
||||
|
||||
package org.springframework.boot.devtools.env;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import java.net.URL;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.devtools.restart.RestartInitializer;
|
||||
import org.springframework.boot.devtools.restart.Restarter;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@ -32,13 +41,22 @@ import org.springframework.context.annotation.Configuration;
|
||||
*/
|
||||
public class DevToolPropertiesIntegrationTests {
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private ConfigurableApplicationContext context;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
Restarter.initialize(new String[] {}, false, new MockInitializer(), false);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
Restarter.clearInstance();
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -59,6 +77,33 @@ public class DevToolPropertiesIntegrationTests {
|
||||
this.context.getBean(MyBean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postProcessWhenRestarterDisabledAndRemoteSecretNotSetShouldNotAddPropertySource()
|
||||
throws Exception {
|
||||
Restarter.clearInstance();
|
||||
Restarter.disable();
|
||||
SpringApplication application = new SpringApplication(
|
||||
BeanConditionConfiguration.class);
|
||||
application.setWebEnvironment(false);
|
||||
this.context = application.run();
|
||||
this.thrown.expect(NoSuchBeanDefinitionException.class);
|
||||
this.context.getBean(MyBean.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postProcessWhenRestarterDisabledAndRemoteSecretSetShouldAddPropertySource()
|
||||
throws Exception {
|
||||
Restarter.clearInstance();
|
||||
Restarter.disable();
|
||||
SpringApplication application = new SpringApplication(
|
||||
BeanConditionConfiguration.class);
|
||||
application.setWebEnvironment(false);
|
||||
application.setDefaultProperties(Collections.<String, Object>singletonMap(
|
||||
"spring.devtools.remote.secret", "donttell"));
|
||||
this.context = application.run();
|
||||
this.context.getBean(MyBean.class);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnProperty("spring.h2.console.enabled")
|
||||
static class ClassConditionConfiguration {
|
||||
@ -79,4 +124,12 @@ public class DevToolPropertiesIntegrationTests {
|
||||
|
||||
}
|
||||
|
||||
static class MockInitializer implements RestartInitializer {
|
||||
|
||||
@Override
|
||||
public URL[] getInitialUrls(Thread thread) {
|
||||
return new URL[] {};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user