diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/SslServerCustomizer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/SslServerCustomizer.java index 9d1004f3ae..496d5cdb10 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/SslServerCustomizer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/SslServerCustomizer.java @@ -124,7 +124,6 @@ class SslServerCustomizer implements JettyServerCustomizer { HttpConfiguration config, SslContextFactory sslContextFactory) { HTTP2ServerConnectionFactory h2 = new HTTP2ServerConnectionFactory(config); ALPNServerConnectionFactory alpn = new ALPNServerConnectionFactory(); - alpn.setDefaultProtocol("h2"); sslContextFactory.setCipherComparator(HTTP2Cipher.COMPARATOR); sslContextFactory.setProvider("Conscrypt"); SslConnectionFactory ssl = new SslConnectionFactory(sslContextFactory, diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/jetty/SslServerCustomizerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/jetty/SslServerCustomizerTests.java new file mode 100644 index 0000000000..a89a8e41ee --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/jetty/SslServerCustomizerTests.java @@ -0,0 +1,98 @@ +/* + * Copyright 2012-2018 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.boot.web.embedded.jetty; + +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory; +import org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory; +import org.eclipse.jetty.server.ConnectionFactory; +import org.eclipse.jetty.server.HttpConnectionFactory; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.SslConnectionFactory; +import org.junit.Test; + +import org.springframework.boot.web.server.Http2; +import org.springframework.boot.web.server.Ssl; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link SslServerCustomizer}. + * + * @author Andy Wilkinson + */ +public class SslServerCustomizerTests { + + @Test + @SuppressWarnings("rawtypes") + public void whenHttp2IsNotEnabledServerConnectorHasSslAndHttpConnectionFactories() { + Server server = createCustomizedServer(); + assertThat(server.getConnectors()).hasSize(1); + List factories = new ArrayList<>( + server.getConnectors()[0].getConnectionFactories()); + assertThat(factories).extracting((factory) -> (Class) factory.getClass()) + .containsExactly(SslConnectionFactory.class, HttpConnectionFactory.class); + } + + @Test + @SuppressWarnings("rawtypes") + public void whenHttp2IsEnabledServerConnectorsHasSslAlpnH2AndHttpConnectionFactories() { + Http2 http2 = new Http2(); + http2.setEnabled(true); + Server server = createCustomizedServer(http2); + assertThat(server.getConnectors()).hasSize(1); + List factories = new ArrayList<>( + server.getConnectors()[0].getConnectionFactories()); + assertThat(factories).extracting((factory) -> (Class) factory.getClass()) + .containsExactly(SslConnectionFactory.class, + ALPNServerConnectionFactory.class, + HTTP2ServerConnectionFactory.class, HttpConnectionFactory.class); + } + + @Test + public void alpnConnectionFactoryHasNullDefaultProtocolToAllowNegotiationToHttp11() { + Http2 http2 = new Http2(); + http2.setEnabled(true); + Server server = createCustomizedServer(http2); + assertThat(server.getConnectors()).hasSize(1); + List factories = new ArrayList<>( + server.getConnectors()[0].getConnectionFactories()); + assertThat(((ALPNServerConnectionFactory) factories.get(1)).getDefaultProtocol()) + .isNull(); + } + + private Server createCustomizedServer() { + return createCustomizedServer(new Http2()); + } + + private Server createCustomizedServer(Http2 http2) { + Ssl ssl = new Ssl(); + ssl.setKeyStore("classpath:test.jks"); + return createCustomizedServer(ssl, http2); + } + + private Server createCustomizedServer(Ssl ssl, Http2 http2) { + Server server = new Server(); + new SslServerCustomizer(new InetSocketAddress(0), ssl, null, http2) + .customize(server); + return server; + } + +}