Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure SSL for Jetty embedded developer

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report