Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple Jetty SSL test
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloHandler extends AbstractHandler {
final String greeting;
final String body;
public HelloHandler() {
this("Hello World");
}
public HelloHandler(String greeting) {
this(greeting, null);
}
public HelloHandler(String greeting, String body) {
this.greeting = greeting;
this.body = body;
}
public void handle(String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response) throws IOException,
ServletException {
response.setContentType("text/html; charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
PrintWriter out = response.getWriter();
out.println("<h1>" + greeting + "</h1>");
if (body != null) {
out.println(body);
}
baseRequest.setHandled(true);
}
}
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.ssl.SslContextFactory;
public class Main {
public static void main(String[] args) throws Exception {
System.setProperty("javax.net.debug", "ssl");
Server server = new Server();
server.setHandler(new HelloHandler());
HttpConfiguration httpConfig = new HttpConfiguration();
httpConfig.setSecureScheme("https");
HttpConnectionFactory connectionFactory = new HttpConnectionFactory(httpConfig);
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath("/Users/jdeppe/projects/jetty-ssl/trusted.keystore");
sslContextFactory.setKeyStorePassword("password");
// sslContextFactory.setProtocol("TLSv1.1");
// sslContextFactory.setExcludeProtocols("TLSv1.2");
// sslContextFactory.setIncludeCipherSuites("TLS_ECDHE_RSA_.*");
// sslContextFactory.setExcludeCipherSuites();
System.err.println(sslContextFactory.dump());
ServerConnector connector = new ServerConnector(server, sslContextFactory, connectionFactory);
connector.setPort(8081);
server.setConnectors(new Connector[] {connector});
server.start();
server.join();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment