内置Jetty HTTPS启动

这次我们来搭建一个支持安全协议SSL的web服务,即https。
我们先来复习两种加密的方式,一种是对称加密,另一种是非对称加密。
对称加密就是解密和加密的秘钥是一样的,代表性的为AES算法。这种传输的效率要高一些,但是保密性较差,因为秘钥的保管十分重要。
非对称加密就是加密的秘钥和解密的秘钥不相等,也就是分为公钥和私钥。这样可以保证安全性,但是传输的效率会低一些,代表性为RSA算法,因此在一般的加密情况下我们采用非对称加密的方式去传输对称加密的秘钥,然后采用AES传输主要的数据。

1、我们首先使用JDK自带的加密工具生成秘钥:

keytool -keystore keystore -alias jetty -genkey -keyalg RSA

输入相应的秘钥,需要输入两个秘钥,,分别输入password1,password2。
2、让后导出证书:

keytool -export -alias jetty -file jetty.crt -keystore keystore

3、生成OBA文件:

java -cp jetty-util-8.1.14.v20131031.jar org.eclipse.jetty.util.security.Password <your password>

PS:jar包对应自己的版本。密码填写自己的密码。

生成之后,将jetty.crt和keystore都放到webapp下面

启动代码

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 提示不安全证书,信任即可访问。

转载于:https://my.oschina.net/u/933928/blog/888307

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值