JavaXT
|
|
SSL SupportThe javaxt-server library makes it easy to host HTTP applications over SSL/TLS. In fact, you can run both SSL and non-SSL applications on the same port. Here's an example:package com.example; import javaxt.http.servlet.*; public class SSLTest extends HttpServlet { //Constructor public SSLTest() throws Exception { setKeyStore(new java.io.File("/temp/keystore.jks"), "password"); setTrustStore(new java.io.File("/temp/truststore.jks"), "password"); } //Request Processor public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { String protocol = request.getURL().getProtocol(); if (protocol.equals("https")){ response.write("SSL Request --> " + request.getURL()); //https://localhost:8080 } else{ response.write("Non-SSL Request --> " + request.getURL()); //http://localhost:8080 } } }Note that in this example, the servlet constructor sets the location and password for both a keystore and a truststore. This is the only requirement for the JavaXT HTTP Server to run over SSL. How to Create a KeyStore and TrustStoreThere are pleanty of really nice tutorials out on the web that describe how to create a keystore and truststore. Here's a really simplified, crash course on how to create a self-signed certificate you can use to test the JavaXT HTTP Server. For this tutorial, you will need the keytool command line app found in the "bin" directory of your Java installation. Step 1: Generate a KeyStorekeytool -genkeypair -alias certificatekey -keyalg RSA -keysize 2048 -keystore /temp/keystore.jksStep 2: Create a Self-Signed SSL Certificate keytool -export -alias certificatekey -keystore /temp/keystore.jks -rfc -file /temp/selfsignedcert.cerStep 3: Create a TrustStore and Import the SSL Certificate keytool -import -alias certificatekey -file /temp/selfsignedcert.cer -keystore /temp/truststore.jks |