内置Jetty HTTPS启动

此次咱们来搭建一个支持安全协议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

相关文章
相关标签/搜索