Enable HTTPS support with self-signed certificate for embedded Jetty 9

Alex Cheng
Dec 12, 2017 · 1 min read

Sometimes you may want to test HTTPS support for embedded Jetty, a self-signed certificate is enough for testing. This post shows how to enable HTTP support with self-signed certificate for embedded Jetty 9.

Generate the keystore

The first step is to generate a keystore using keytool. -validity 3650 means the certificate is valid for 3650 days.

Configure Jetty

We can choose to support both HTTP and HTTPS by adding two ServerConnectors in Jetty. In the code below, we create a Server and add a ServerConnector for HTTP on port httpPort.

Then we create a SslContextFactory which loads the keystore file in path keyStorePath. A new ServerConnector is created for HTTPS on port httpsPort.

Obfuscated password

The keystore’s password can be obfuscated using org.eclipse.jetty.util.security.Password. The command below shows how to obfuscate the password password. The output should be used as the password. The obfuscated passwords start with OBF:.

HTTPS only

If you only want to enable HTTPS, you can either remove the ServerConnector for HTTP or use org.eclipse.jetty.server.handler.SecuredRedirectHandler to redirect HTTP to HTTPS. SecuredRedirectHandler should be put before other handlers in the handlers chain.

High-quality IT articles

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store