[bs-97] Support adding management endpoints to a different network

* ManagementProperties and ServerProperties now support an address property
* For example set management.port=9001,management.address=127.0.0.1 to listen
on port 9001 but only for connections from the localhost

[Fixes #49395783]
This commit is contained in:
Dave Syer
2013-05-08 07:06:27 +01:00
parent a7118c2ff2
commit 4a292bd93f
13 changed files with 313 additions and 69 deletions

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2012-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.bootstrap.bind;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorSupport;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @author Dave Syer
*
*/
public class InetAddressEditor extends PropertyEditorSupport implements PropertyEditor {
@Override
public String getAsText() {
return ((InetAddress) getValue()).getHostAddress();
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
try {
setValue(InetAddress.getByName(text));
} catch (UnknownHostException e) {
throw new IllegalArgumentException("Cannot locate host", e);
}
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.bootstrap.bind;
import java.net.InetAddress;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -68,6 +69,9 @@ public class RelaxedDataBinder extends DataBinder {
@Override
protected void doBind(MutablePropertyValues propertyValues) {
propertyValues = modifyProperties(propertyValues, getTarget());
// Harmless additional property editor comes in very handy sometimes...
getPropertyEditorRegistry().registerCustomEditor(InetAddress.class,
new InetAddressEditor());
super.doBind(propertyValues);
}

View File

@@ -17,6 +17,7 @@
package org.springframework.bootstrap.context.embedded;
import java.io.File;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
@@ -56,6 +57,8 @@ public abstract class AbstractEmbeddedServletContainerFactory implements
private Set<ErrorPage> errorPages = new LinkedHashSet<ErrorPage>();
private InetAddress address;
/**
* Create a new {@link AbstractEmbeddedServletContainerFactory} instance.
*/
@@ -132,6 +135,22 @@ public abstract class AbstractEmbeddedServletContainerFactory implements
return this.port;
}
/**
* If you need the server to bind to a specific network address, provide one here.
*
* @param address the address to set (defaults to null)
*/
public void setAddress(InetAddress address) {
this.address = address;
}
/**
* @return the address the embedded container binds to
*/
public InetAddress getAddress() {
return this.address;
}
/**
* Sets {@link ServletContextInitializer} that should be applied in addition to
* {@link #getEmbdeddedServletContainer(ServletContextInitializer...)} parameters.

View File

@@ -17,6 +17,7 @@
package org.springframework.bootstrap.context.embedded.jetty;
import java.io.File;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -98,7 +99,7 @@ public class JettyEmbeddedServletContainerFactory extends
if (getPort() == 0) {
return new EmptyEmbeddedServletContainer();
}
Server server = new Server(getPort());
Server server = new Server(new InetSocketAddress(getAddress(), getPort()));
WebAppContext context = new WebAppContext();
if (this.resourceLoader != null) {

View File

@@ -36,6 +36,7 @@ import org.apache.catalina.core.StandardHost;
import org.apache.catalina.core.StandardService;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.Tomcat.FixContextListener;
import org.apache.coyote.AbstractProtocol;
import org.springframework.bootstrap.context.embedded.AbstractEmbeddedServletContainerFactory;
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainer;
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerException;
@@ -121,7 +122,7 @@ public class TomcatEmbeddedServletContainerFactory extends
} else {
Connector connector = new Connector(
"org.apache.coyote.http11.Http11NioProtocol");
connector.setPort(getPort());
customizeConnector(connector);
this.tomcat.getService().addConnector(connector);
this.tomcat.setConnector(connector);
}
@@ -181,6 +182,14 @@ public class TomcatEmbeddedServletContainerFactory extends
context.addServletMapping("*.jspx", "jsp");
}
private void customizeConnector(Connector connector) {
connector.setPort(getPort());
if (connector.getProtocolHandler() instanceof AbstractProtocol
&& getAddress() != null) {
((AbstractProtocol) connector.getProtocolHandler()).setAddress(getAddress());
}
}
/**
* Configure the Tomcat {@link Context}.
* @param context the Tomcat context
@@ -339,8 +348,9 @@ public class TomcatEmbeddedServletContainerFactory extends
}
StandardService service = new StandardService();
service.setName(name);
Connector connector = new Connector("HTTP/1.1");
connector.setPort(getPort());
Connector connector = new Connector(
"org.apache.coyote.http11.Http11NioProtocol");
customizeConnector(connector);
service.addConnector(connector);
StandardEngine engine = new StandardEngine();
engine.setName(name);