How to configure SSL for Jetty embedded developer
This article focuses on "Jetty embedded development how to configure SSL", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to configure SSL for Jetty embedded development.
Generate a certificate using keytool, a tool that comes with java
Keytool-genkey-alias server-keypass 123456-storepass 123456-storetype PKCS12-keyalg RSA-keysize 2048-keystore keystore.p12-validity 3650-dname
2 java program
Import org.eclipse.jetty.http.HttpVersion;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.server.SslConnectionFactory;import org.eclipse.jetty.util.ssl.SslContextFactory;public class JeetSslTest {public static void main (String [] args) {Server server = new Server (); HttpConfiguration https_config = new HttpConfiguration (); https_config.setSecureScheme ("https") SslContextFactory.Server sslContextFactory = new SslContextFactory.Server (); sslContextFactory.setKeyStoreType ("PKCS12"); sslContextFactory.setKeyStorePath ("e:/temp/key/keystore.p12"); sslContextFactory.setTrustStorePath ("e:/temp/key/keystore.p12"); sslContextFactory.setKeyStorePassword ("123456"); sslContextFactory.setKeyManagerPassword ("123456"); try {ServerConnector httpsConnector = new ServerConnector (server, new SslConnectionFactory (sslContextFactory,HttpVersion.HTTP_1_1.asString ()), new HttpConnectionFactory (https_config)); httpsConnector.setPort (8443) Server.addConnector (httpsConnector); server.setHandler (new HelloHandler ()); / / processing logic server.start (); server.join ();} catch (Exception e) {e.printStackTrace ();} import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.eclipse.jetty.server.Request;import org.eclipse.jetty.server.handler.AbstractHandler Public class HelloHandler extends AbstractHandler {@ Overridepublic void handle (String target, Request baseRequest,HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {System.out.println (target); / / sets the character set to avoid garbled response.setContentType ("text/html;charset=utf-8"); / / outputs the greeting response.getWriter (). Println ("hello jetty") that we want to display; / / indicates that the request processing is completed baseRequest.setHandled (true);}}
Use a browser to access the https://127.0.0.1:8443/ test after a successful run
At this point, I believe that everyone on the "Jetty embedded development how to configure SSL" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!