Chrome与Java SSL Server握手时关闭连接

Chrome与Java SSL Server握手时关闭连接,第1张

Chrome与Java SSL Server握手时关闭连接

呜呜!我终于想通了。经过漫长而令人沮丧的搜索Intarwebz之后,我在J2SE 6+中找到了有关此隐藏Java库的文档。

com.sun.net.httpserver

此实现仅协商SSL握手并以纯文本形式返回请求:

import com.sun.net.httpserver.Headers;import com.sun.net.httpserver.HttpExchange;import com.sun.net.httpserver.HttpHandler;import com.sun.net.httpserver.HttpsConfigurator;import com.sun.net.httpserver.HttpsExchange;import com.sun.net.httpserver.HttpsParameters;import com.sun.net.httpserver.HttpsServer;import java.io.FileInputStream;import java.io.InputStream;import java.io.IOException;import java.io.OutputStream;import java.net.InetSocketAddress;import java.security.KeyStore;import java.util.concurrent.Executor;import javax.net.ssl.KeyManagerFactory;import javax.net.ssl.SSLContext;public class HTTPS {  public static void main(String[] args) throws Exception {    KeyStore ks = KeyStore.getInstance("JKS");      ks.load(new FileInputStream("server.jks"), "123456".toCharArray());    KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");    kmf.init(ks, "123456".toCharArray());    SSLContext context = SSLContext.getInstance("TLS");    context.init(kmf.getKeyManagers(), null, null);    final HttpsServer server = HttpsServer.create(new InetSocketAddress("localhost", 8443), 10);    server.createContext("/", new HttpHandler() {      public void handle(HttpExchange xchng) throws IOException {        HttpsExchange exchange = (HttpsExchange) xchng;        String ret = "";        ret += exchange.getRequestMethod() + " " + exchange.getRequestURI() + " " + exchange.getProtocol() + "n";        Headers headers = exchange.getRequestHeaders();        if (!headers.isEmpty()) {          ret += "n";          for (String key : headers.keySet()) { ret += key + ": "; boolean semicolon = false; for (String value : headers.get(key)) {   if (semicolon) {     ret += "; ";   }   ret += value;   semicolon = true; } ret += "n";          }        }        if (headers.get("Content-Length") != null) {          InputStream in = exchange.getRequestBody();          ret += "n";          int i;          while ((i = in.read()) != -1) { ret += String.valueOf((char) i);          }        }        headers = exchange.getResponseHeaders();        headers.set("Content-Type", "text/plain");        exchange.sendResponseHeaders(200, ret.length());        OutputStream out = exchange.getResponseBody();        out.write(ret.getBytes());        exchange.close();      }    });    server.setHttpsConfigurator(new HttpsConfigurator(context) {      public void configure(HttpsParameters params) {      }    });    server.setExecutor(new Executor() {      public void execute(Runnable command) {        new Thread(command).start();      }    });    server.start();        Runtime.getRuntime().addShutdownHook(new Thread() {      public void run() {        System.out.println("Server shutting down.  Waiting 3 seconds for exchanges to complete.");        server.stop(3);      }    });  }}

我在Ubuntu盒子上对此进行了测试,它适用于以下浏览器

  • 火狐浏览器
  • 歌剧
  • 移动Safari(iPhone4)
  • 苹果浏览器
  • IE浏览器


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5057173.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存