Skip to content

Instantly share code, notes, and snippets.

@songrgg
Created November 18, 2017 16:04
Show Gist options
  • Save songrgg/39abe12f6a14f95424e576b6ed1e636c to your computer and use it in GitHub Desktop.
Save songrgg/39abe12f6a14f95424e576b6ed1e636c to your computer and use it in GitHub Desktop.
Jetty HTTP2

生成jks文件

# 准备好CA的key,crt文件,生成pfx文件
$ openssl pkcs12 -export -out ~/Desktop/wallstreetcn.pfx -inkey ~/Desktop/2_wallstreetcn.com.key -in ~/Desktop/1_wallstreetcn.com_bundle.crt
# 利用生成好的pfx文件,生成jks文件
$ keytool -importkeystore -srckeystore ~/Desktop/wallstreetcn.pfx -destkeystore wallstreetcn.jks -srcstoretype PKCS12 -deststoretype JKS

启动Java时,将alpn-boot.jar放到bootclasspath上

$ java -Xbootclasspath/p:lib/alpn-boot-8.1.11.v20170118.jar taotie.jar
package com.wallstreetcn.flume.source.http;
import org.eclipse.jetty.alpn.ALPN;
import org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory;
import org.eclipse.jetty.http2.HTTP2Cipher;
import org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import static org.eclipse.jetty.util.resource.Resource.newClassPathResource;
public class Main {
public static void main(String... args) throws Exception {
Server server = new Server();
ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
context.addServlet(new ServletHolder(new Servlet()), "/");
server.setHandler(context);
// HTTP Configuration
HttpConfiguration httpConfig = new HttpConfiguration();
httpConfig.setSecureScheme("https");
httpConfig.setSecurePort(8443);
// SSL Context Factory for HTTPS and HTTP/2
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStoreResource(newClassPathResource("wallstreetcn.jks"));
sslContextFactory.setKeyStorePassword("123456");
sslContextFactory.setKeyManagerPassword("123456");
sslContextFactory.setCipherComparator(HTTP2Cipher.COMPARATOR);
// HTTPS Configuration
httpConfig.addCustomizer(new SecureRequestCustomizer());
// HTTP/2 Connection Factory
HTTP2ServerConnectionFactory h2 = new HTTP2ServerConnectionFactory(httpConfig);
NegotiatingServerConnectionFactory.checkProtocolNegotiationAvailable();
ALPNServerConnectionFactory alpn = new ALPNServerConnectionFactory();
alpn.setDefaultProtocol("h2");
// SSL Connection Factory
SslConnectionFactory ssl = new SslConnectionFactory(sslContextFactory, alpn.getProtocol());
// HTTP/2 Connector
ServerConnector http2Connector =
new ServerConnector(server, ssl, alpn, h2, new HttpConnectionFactory(httpConfig));
http2Connector.setPort(8443);
server.addConnector(http2Connector);
ALPN.debug = false;
server.start();
server.join();
}
}
class Servlet extends HttpServlet {
private static final long serialVersionUID = 6515190237751968426L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/plain");
resp.getWriter().write("Hello, World!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment