Merge pull request #12120 from Marcos Trejo Munguia

* gh-12120:
  Polish "Set host when creating Jetty SSL connector"
  Set host when creating Jetty SSL connector
pull/11991/merge
Andy Wilkinson 7 years ago
commit 83fc4b3246

@ -180,7 +180,7 @@ public class JettyEmbeddedServletContainerFactory
SslContextFactory sslContextFactory = new SslContextFactory(); SslContextFactory sslContextFactory = new SslContextFactory();
configureSsl(sslContextFactory, getSsl()); configureSsl(sslContextFactory, getSsl());
AbstractConnector connector = getSslServerConnectorFactory() AbstractConnector connector = getSslServerConnectorFactory()
.getConnector(server, sslContextFactory, port); .createConnector(server, sslContextFactory, address);
server.setConnectors(new Connector[] { connector }); server.setConnectors(new Connector[] { connector });
} }
for (JettyServerCustomizer customizer : getServerCustomizers()) { for (JettyServerCustomizer customizer : getServerCustomizers()) {
@ -700,8 +700,8 @@ public class JettyEmbeddedServletContainerFactory
*/ */
private interface SslServerConnectorFactory { private interface SslServerConnectorFactory {
AbstractConnector getConnector(Server server, SslContextFactory sslContextFactory, AbstractConnector createConnector(Server server,
int port); SslContextFactory sslContextFactory, InetSocketAddress address);
} }
@ -712,8 +712,8 @@ public class JettyEmbeddedServletContainerFactory
implements SslServerConnectorFactory { implements SslServerConnectorFactory {
@Override @Override
public ServerConnector getConnector(Server server, public ServerConnector createConnector(Server server,
SslContextFactory sslContextFactory, int port) { SslContextFactory sslContextFactory, InetSocketAddress address) {
HttpConfiguration config = new HttpConfiguration(); HttpConfiguration config = new HttpConfiguration();
config.setSendServerVersion(false); config.setSendServerVersion(false);
config.addCustomizer(new SecureRequestCustomizer()); config.addCustomizer(new SecureRequestCustomizer());
@ -722,7 +722,8 @@ public class JettyEmbeddedServletContainerFactory
sslContextFactory, HttpVersion.HTTP_1_1.asString()); sslContextFactory, HttpVersion.HTTP_1_1.asString());
ServerConnector serverConnector = new ServerConnector(server, ServerConnector serverConnector = new ServerConnector(server,
sslConnectionFactory, connectionFactory); sslConnectionFactory, connectionFactory);
serverConnector.setPort(port); serverConnector.setPort(address.getPort());
serverConnector.setHost(address.getHostString());
return serverConnector; return serverConnector;
} }
@ -735,8 +736,8 @@ public class JettyEmbeddedServletContainerFactory
implements SslServerConnectorFactory { implements SslServerConnectorFactory {
@Override @Override
public AbstractConnector getConnector(Server server, public AbstractConnector createConnector(Server server,
SslContextFactory sslContextFactory, int port) { SslContextFactory sslContextFactory, InetSocketAddress address) {
try { try {
Class<?> connectorClass = Class Class<?> connectorClass = Class
.forName("org.eclipse.jetty.server.ssl.SslSocketConnector"); .forName("org.eclipse.jetty.server.ssl.SslSocketConnector");
@ -744,7 +745,9 @@ public class JettyEmbeddedServletContainerFactory
.getConstructor(SslContextFactory.class) .getConstructor(SslContextFactory.class)
.newInstance(sslContextFactory); .newInstance(sslContextFactory);
connector.getClass().getMethod("setPort", int.class).invoke(connector, connector.getClass().getMethod("setPort", int.class).invoke(connector,
port); address.getPort());
connector.getClass().getMethod("setHost", String.class).invoke(connector,
address.getHostString());
return connector; return connector;
} }
catch (Exception ex) { catch (Exception ex) {

@ -244,6 +244,27 @@ public class JettyEmbeddedServletContainerFactoryTests
.isEqualTo(new String[] { "TLSv1.1" }); .isEqualTo(new String[] { "TLSv1.1" });
} }
@Test
public void sslEnabledSpecificIPAddress() throws Exception {
Ssl ssl = new Ssl();
ssl.setKeyStore("src/test/resources/test.jks");
ssl.setKeyStorePassword("secret");
ssl.setKeyPassword("password");
JettyEmbeddedServletContainerFactory factory = getFactory();
factory.setSsl(ssl);
factory.setAddress(
InetAddress.getByAddress(InetAddress.getLocalHost().getAddress()));
this.container = factory.getEmbeddedServletContainer();
this.container.start();
JettyEmbeddedServletContainer jettyContainer = (JettyEmbeddedServletContainer) this.container;
ServerConnector connector = (ServerConnector) jettyContainer.getServer()
.getConnectors()[0];
assertThat(connector.getHost()).isEqualTo(factory.getAddress().getHostAddress());
}
private void assertTimeout(JettyEmbeddedServletContainerFactory factory, private void assertTimeout(JettyEmbeddedServletContainerFactory factory,
int expected) { int expected) {
this.container = factory.getEmbeddedServletContainer(); this.container = factory.getEmbeddedServletContainer();

Loading…
Cancel
Save