此次咱們來搭建一個支持安全協議SSL的web服務,即https。
咱們先來複習兩種加密的方式,一種是對稱加密,另外一種是非對稱加密。
對稱加密就是解密和加密的祕鑰是同樣的,表明性的爲AES算法。這種傳輸的效率要高一些,可是保密性較差,由於祕鑰的保管十分重要。
非對稱加密就是加密的祕鑰和解密的祕鑰不相等,也就是分爲公鑰和私鑰。這樣能夠保證安全性,可是傳輸的效率會低一些,表明性爲RSA算法,所以在通常的加密狀況下咱們採用非對稱加密的方式去傳輸對稱加密的祕鑰,而後採用AES傳輸主要的數據。java
一、咱們首先使用JDK自帶的加密工具生成祕鑰:web
keytool -keystore keystore -alias jetty -genkey -keyalg RSA
輸入相應的祕鑰,須要輸入兩個祕鑰,,分別輸入password1,password2。
二、讓後導出證書:算法
keytool -export -alias jetty -file jetty.crt -keystore keystore
三、生成OBA文件:windows
java -cp jetty-util-8.1.14.v20131031.jar org.eclipse.jetty.util.security.Password <your password>
PS:jar包對應本身的版本。密碼填寫本身的密碼。安全
生成以後,將jetty.crt和keystore都放到webapp下面app
啓動代碼eclipse
package quickstart; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ssl.SslSelectChannelConnector; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.eclipse.jetty.webapp.WebAppClassLoader; import org.eclipse.jetty.webapp.WebAppContext; /** * * 使用Jetty運行調試Web應用, 在Console輸入回車快速從新加載應用. * @author */ public class zuleServer { private static final String DEFAULT_WEBAPP_PATH_WIN = "src/main/webapp"; private static final String WINDOWS_WEBDEFAULT_PATH = "src/test/resources/jetty/webdefault-windows.xml"; public static final int PORT = 8180; public static final String CONTEXT = "/zule"; public static void main(String[] args) throws Exception { try { Server server = createServerInSource(PORT, CONTEXT);// 啓動Jetty server.start(); System.out.println("[INFO] Server running at https://localhost:" + PORT + CONTEXT); System.out.println("[HINT] Hit Enter to reload the application quickly"); // 等待用戶輸入回車重載應用. while (true) { char c = (char) System.in.read(); if (c == '\n') { reloadContext(server); } } } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } /** * 建立用於開發運行調試的Jetty Server, 以src/main/webapp爲Web應用目錄. */ private static Server createServerInSource(int port, String contextPath) { Server server = new Server(); QueuedThreadPool threadPool = new QueuedThreadPool(); threadPool.setMaxThreads(3000); server.setThreadPool(threadPool); // 設置在JVM退出時關閉Jetty的鉤子。 server.setStopAtShutdown(true); SslContextFactory sslContextFactory = new SslContextFactory(); sslContextFactory.setKeyStorePath(DEFAULT_WEBAPP_PATH_WIN+"/keystore"); // 私鑰 sslContextFactory.setKeyStorePassword("123456"); // 公鑰 sslContextFactory.setKeyManagerPassword("123456"); SslSelectChannelConnector httpsConnector = new SslSelectChannelConnector(sslContextFactory); httpsConnector.setPort(PORT);// 設置訪問端口 httpsConnector.setReuseAddress(false); server.addConnector(httpsConnector); WebAppContext webContext = new WebAppContext(DEFAULT_WEBAPP_PATH_WIN, contextPath); // 修改webdefault.xml,解決Windows下Jetty Lock住靜態文件的問題. webContext.setDefaultsDescriptor(WINDOWS_WEBDEFAULT_PATH); server.setHandler(webContext); return server; } /** * 快速從新啓動application,重載target/classes與target/test-classes. */ private static void reloadContext(Server server) throws Exception { WebAppContext context = (WebAppContext) server.getHandler(); System.out.println("[INFO] Application reloading"); context.stop(); WebAppClassLoader classLoader = new WebAppClassLoader(context); classLoader.addClassPath("target/classes"); classLoader.addClassPath("target/test-classes"); context.setClassLoader(classLoader); context.start(); System.out.println("[INFO] Application reloaded"); System.out.println("[INFO] Server running at http://localhost:" + PORT + CONTEXT); System.out.println("[HINT] Hit Enter to reload the application quickly"); } }
啓動訪問:https://localhost:818/zule 提示不安全證書,信任便可訪問。webapp