一  使用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/ 测试