下面列出了org.springframework.boot.context.embedded.jetty.JettyServerCustomizer#org.eclipse.jetty.http2.server.HTTP2CServerConnectionFactory 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static Server startHttp2() throws Exception {
final Server server = new Server();
// Common HTTP configuration.
final HttpConfiguration config = new HttpConfiguration();
// HTTP/1.1 support.
final HttpConnectionFactory http1 = new HttpConnectionFactory(config);
// HTTP/2 cleartext support.
final HTTP2CServerConnectionFactory http2c = new HTTP2CServerConnectionFactory(config);
// Add the connector.
final ServerConnector connector = new ServerConnector(server, http1, http2c);
connector.setPort(0);
server.addConnector(connector);
// Add the servlet.
final ServletHandler handler = new ServletHandler();
handler.addServletWithMapping(newServletHolder(thriftServlet), TSERVLET_PATH);
server.setHandler(handler);
// Start the server.
server.start();
return server;
}
public Server build() throws Exception {
Server server = new Server();
// Register servlets
ServletContextHandler context = new ServletContextHandler(server, contextUrn, ServletContextHandler.SESSIONS);
servletHandlers.forEach((uri, servletHolder) -> { context.addServlet(servletHolder, uri);});
// Register servlet filters
filters.forEach((urn, filterHolder) -> { context.addFilter(filterHolder, urn,
EnumSet.of(DispatcherType.REQUEST)); });
// Register EventListener instances
sessionEventListeners.forEach( listener -> { context.getSessionHandler().addEventListener(listener); });
// Register jersey rest services
ServletContainer restServletContainer = new ServletContainer(resourceConfig);
ServletHolder restServletHolder = new ServletHolder(restServletContainer);
context.addServlet(restServletHolder, restUriPrefix);
// Register static resources (html pages, images, javascripts, ...)
String externalResource = this.getClass().getResource(staticResourceBasePath).toExternalForm();
DefaultServlet defaultServlet = new DefaultServlet();
ServletHolder holderPwd = new ServletHolder("default", defaultServlet);
holderPwd.setInitParameter("resourceBase", externalResource);
context.addServlet(holderPwd, staticResourceBaseUrn);
server.setHandler(context);
// HTTP Configuration
HttpConfiguration httpConfig = new HttpConfiguration();
httpConfig.setSecureScheme("https");
httpConfig.setSecurePort(secureHttpPort);
httpConfig.setSendXPoweredBy(true);
httpConfig.setSendServerVersion(true);
// HTTP Connector
HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory(httpConfig);
HTTP2CServerConnectionFactory http2CServerConnectionFactory = new HTTP2CServerConnectionFactory(httpConfig);
ServerConnector http = new ServerConnector(server, httpConnectionFactory, http2CServerConnectionFactory);
http.setPort(httpPort);
server.addConnector(http);
// SSL Context Factory for HTTPS and HTTP/2
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setTrustStore(keyStore);
sslContextFactory.setTrustStorePassword(keyStorePassword);
sslContextFactory.setKeyStore(keyStore);
sslContextFactory.setKeyStorePassword(keyStorePassword);
sslContextFactory.setCipherComparator(HTTP2Cipher.COMPARATOR);
// HTTPS Configuration
HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig);
httpsConfig.addCustomizer(new SecureRequestCustomizer());
// HTTP/2 Connection Factory
ServerConnectionFactory h2 = new ServerConnectionFactory(httpsConfig, streamProcessors);
//NegotiatingServerConnectionFactory.checkProtocolNegotiationAvailable();
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(secureHttpPort);
server.addConnector(http2Connector);
ALPN.debug=false;
return server;
}
@Override
public ConnectionFactory setup(final HttpConfiguration config) {
return new HTTP2CServerConnectionFactory(config);
}
/**
* Inits the.
*
* @throws Exception the exception
*/
public final void init() throws Exception {
initialised = true;
server = new Server();
Security.addProvider(new BouncyCastleProvider());
// Setup JMX
final MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
server.addBean(mbContainer);
final org.eclipse.jetty.webapp.Configurations classlist = org.eclipse.jetty.webapp.Configurations.setServerDefault(server);
final HttpConfiguration http_config = new HttpConfiguration();
http_config.setSecureScheme("https");
http_config.setSecurePort(28443);
http_config.setSendServerVersion(false);
final HttpConfiguration https_config = new HttpConfiguration(http_config);
https_config.addCustomizer(new SecureRequestCustomizer());
final SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
sslContextFactory.setKeyStoreType("PKCS12");
sslContextFactory.setKeyStorePath("target/keystore.p12");
sslContextFactory.setTrustStorePath("target/keystore.p12");
sslContextFactory.setTrustStoreType("PKCS12");
sslContextFactory.setKeyStorePassword("changeit");
sslContextFactory.setTrustStorePassword("changeit");
sslContextFactory.setKeyManagerPassword("changeit");
sslContextFactory.setCertAlias("jetty");
sslContextFactory.setIncludeCipherSuites("TLS_AES_256_GCM_SHA384", "TLS_CHACHA20_POLY1305_SHA256",
"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
"TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256");
sslContextFactory.setExcludeProtocols("SSL", "SSLv2", "SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.1");
sslContextFactory.setIncludeProtocols("TLSv1.2", "TLSv1.3");
final ServerConnector sslConnector = new ServerConnector(server,
new SslConnectionFactory(sslContextFactory, "http/1.1"), new HttpConnectionFactory(https_config),
new HTTP2CServerConnectionFactory(https_config));
sslConnector.setPort(PORT);
server.setConnectors(new ServerConnector[] { sslConnector });
final WebAppContext handler = new WebAppContext("src/main/webapp", "/");
handler.setExtraClasspath("target/classes");
handler.setParentLoaderPriority(true);
handler.setConfigurationDiscovered(true);
handler.setClassLoader(Thread.currentThread().getContextClassLoader());
final HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { handler, new DefaultHandler() });
server.setHandler(handlers);
}