[英]Jetty ssl factory using http2 doesn't work
我有一個使用碼頭的簡單 spring 引導項目。
我在端口 443 上運行並使用 http1.1 就好了
@Bean(name = "containerCustomizer")
public WebServerFactoryCustomizer containerCustomizer()
{
return (container -> ((JettyServletWebServerFactory) container).addServerCustomizers(server -> {
// HTTPS
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
sslContextFactory.setKeyStoreResource(Resource.newResource(
getClass().getClassLoader().getResource("keystore.jks")));
sslContextFactory.setKeyStorePassword("password");
sslContextFactory.setProtocol("TLS");
sslContextFactory.setKeyStoreType("JKS");
HttpConfiguration https = new HttpConfiguration();
https.addCustomizer(new SecureRequestCustomizer());
SslConnectionFactory sslFactory = new SslConnectionFactory(sslContextFactory,
HttpVersion.HTTP_1_1.asString());
ServerConnector sslConnector = new ServerConnector(server, sslFactory, new HttpConnectionFactory(https));
sslConnector.setPort(443);
server.setConnectors(new Connector[] {sslConnector});
}));
}
但是,一旦我通過將HttpVersion.HTTP_1_1.asString()
替換為 HttpVersion.HTTP_2.asString() 來更改為HttpVersion.HTTP_2.asString()
,我就會遇到異常
Caused by: java.lang.IllegalStateException: No protocol factory for SSL next protocol: 'HTTP/2.0' in ServerConnector@61e45f87{SSL, (ssl, http/1.1)}{0.0.0.0:443}
at org.eclipse.jetty.server.AbstractConnector.doStart(AbstractConnector.java:317) ~[jetty-server-9.4.35.v20201120.jar:9.4.35.v20201120]
at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:81) ~[jetty-server-9.4.35.v20201120.jar:9.4.35.v20201120]
at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:234) ~[jetty-server-9.4.35.v20201120.jar:9.4.35.v20201120]
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:73) ~[jetty-util-9.4.35.v20201120.jar:9.4.35.v20201120]
at org.springframework.boot.web.embedded.jetty.JettyWebServer.start(JettyWebServer.java:174) ~[spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE]
... 16 common frames omitted
僅更改版本字符串是不夠的。
你還需要...
ALPNServerConnectionFactory
- 用於處理選擇 HTTP/2 協議的 TLS 層(稱為 ALPN)HTTP2ServerConnectionFactory
- 用於使用 HTTP/2 協議關鍵是將連接類型鏈接在一起,從 ALPN 到 HTTP2 到 HTTPS。
// SSL Context Factory for HTTPS and HTTP/2
SslContextFactory sslContextFactory = new SslContextFactory.Server();
// configure sslContextFactory
// HTTPS Configuration
HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig);
httpsConfig.addCustomizer(new SecureRequestCustomizer());
// HTTP/2 Connection Factory
HTTP2ServerConnectionFactory h2 = new HTTP2ServerConnectionFactory(httpsConfig);
ALPNServerConnectionFactory alpn = new ALPNServerConnectionFactory();
alpn.setDefaultProtocol(http.getDefaultProtocol());
// SSL Connection Factory
SslConnectionFactory ssl = new SslConnectionFactory(sslContextFactory, alpn.getProtocol());
// HTTP/2 Connector
ServerConnector http2Connector =
new ServerConnector(server, ssl, alpn, h2, new HttpConnectionFactory(httpsConfig));
http2Connector.setPort(securePort);
server.addConnector(http2Connector);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.