DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Aggregating REST APIs Calls Using Apache Camel
  • How to Develop Microservices With Spring Cloud and Netflix Discovery
  • Jakarta Security and REST in the Cloud: Part 2 Getting to Know the Basics
  • Jakarta Security and REST in the Cloud Part 1: Hello World

Trending

  • Enhancing Security With ZTNA in Hybrid and Multi-Cloud Deployments
  • How to Format Articles for DZone
  • How AI Agents Are Transforming Enterprise Automation Architecture
  • 5 Subtle Indicators Your Development Environment Is Under Siege
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Adding SSL Support to an Embedded Jetty Server

Adding SSL Support to an Embedded Jetty Server

With these changes, we can access the REST API equally well fromhttp://:9999 and https://:9998.

By 
Alan Hohn user avatar
Alan Hohn
·
Oct. 14, 13 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
54.1K Views

Join the DZone community and get the full member experience.

Join For Free

As I discussed in a series of four posts (see Part 1, Part 2, Part 3, and Part 4), I recently taught a class on Spring WebMVC and how it can be used to REST-enable a standalone Java application. As part of that discussion, I talked about using Jetty as an embedded servlet container, which let us create and access servlets without having to package our existing application as a WAR.

The embedded Jetty example I gave was HTTP only. However, many production applications that expose REST interface are going to want to secure those with some kind of authentication and protect the exchanged information using HTTP/S. I’ll visit the authentication sometime in the future as I get time to work it, but I’d like to talk about what’s required to get HTTP/S working with embedded Jetty.

The first thing we’ll need is a server-side certificate. This contains the public key that the client will use to encrypt its initial communication with the server, in order to establish the session key that will be used to encrypt the regular web traffic.

In a production system, the server’s certificate will need to be signed by an authority the client will trust. If both server and client are in the same organization, this can be accomplished by just putting the server certificate in the client’s keystore. Otherwise, the whole process of getting a certificate signed by a signing authority (Thawte, Verisign, etc.) is involved. This process is exactly the same for Java servers as it is for other web servers, so there are lots of posts on the subject.

For this example, we’ll use a self-signed certificate. We want to keep the certificate with our application so we don’t have to worry about adding it to the default Java keystore if we run the server on a new machine. This is easy; just specify a new keystore file when we generate the key using the keytoolutility that ships with the JDK. The command is:

keytool -genkey -alias sitename -keyalg RSA -keystore keystore.jks -keysize 2048
keystore.jks

The required changes to our EmbeddedServer class are minimal. Jetty has a lot more options, but these are the set we need to make it happen.

  Server server = new Server();

  ServerConnector connector = new ServerConnector(server);
  connector.setPort(9999);

  HttpConfiguration https = new HttpConfiguration();
  https.addCustomizer(new SecureRequestCustomizer());

  SslContextFactory sslContextFactory = new SslContextFactory();
 sslContextFactory.setKeyStorePath(EmbeddedServer.class.getResource(
         "/keystore.jks").toExternalForm());
 sslContextFactory.setKeyStorePassword("123456");
 sslContextFactory.setKeyManagerPassword("123456");

 ServerConnector sslConnector = new ServerConnector(server,
         new SslConnectionFactory(sslContextFactory, "http/1.1"),
         new HttpConnectionFactory(https));
 sslConnector.setPort(9998);

 server.setConnectors(new Connector[] { connector, sslConnector });

We keep the previous connector on port 9999 so we can support both HTTP and HTTP/S. Of course, we could force the use of HTTP/S by just removing the HTTP connector.

The HttpConfiguration and HttpConnectionFactory are essential to making this work. The SslConnectionFactory handles only the SSL part of the job; it requires a regular HTTP configuration to hand off the decrypted request.

One other important point is the way we look up the keystore. This method of getting the URL to a classpath resource will work whether the application is being run from a JAR, WAR, or just classes on the disk. This lets us run equally well inside an IDE like Eclipse and in the production environment. It also avoids the extra install step of adding the server’s certificate to the default Java keystore.

With these changes, we can access the REST API equally well fromhttp://<host>:9999 and https://<host>:9998.

Jetty (web server) application Java (programming language) Production (computer science) POST (HTTP) authentication WAR (file format) REST Web Protocols

Published at DZone with permission of Alan Hohn, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Aggregating REST APIs Calls Using Apache Camel
  • How to Develop Microservices With Spring Cloud and Netflix Discovery
  • Jakarta Security and REST in the Cloud: Part 2 Getting to Know the Basics
  • Jakarta Security and REST in the Cloud Part 1: Hello World

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!