您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Jetty嵌入開發(fā)怎么配置SSL”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Jetty嵌入開發(fā)怎么配置SSL”吧!
一 使用java 自帶工具 keytool 生成證書
keytool -genkey -alias server -keypass 123456 -storepass 123456 -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650 -dname "C=CN,ST=BJ,L=BJ,O=ca.wbq.com,OU=ca.wbq.com,CN=ca.wbq.com"
2 java程序
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());//處理邏輯 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{ @Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException { System.out.println(target); // 設置字符集,避免亂碼 response.setContentType("text/html;charset=utf-8"); // 輸出我們想要顯示的問候語 response.getWriter().println("hello jetty"); // 表示請求處理完成 baseRequest.setHandled(true); } }
運行成功后使用瀏覽器訪問 https://127.0.0.1:8443/ 測試
到此,相信大家對“Jetty嵌入開發(fā)怎么配置SSL”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關內(nèi)容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。