我正在尝试用Java实现HTTPS(SSL)服务器,我想利用com.sun.net.httpserver.HttpsServer。
我已经能够将来自不同地方的一些代码拼凑起来,但是我希望能够指定我的HTTPS服务器愿意支持哪些协议,例如SSLv3,TLSv!等,但我无法弄清楚如何做到这一点。
我发布了目前为止的代码,并且想知道是否有人能告诉我如何添加指定协议的能力?
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsServer;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsParameters;
public class Test {
static private String PROGVERSION = "V1.00";
static private String keystoreFile = "";
static private int listenPort = 0;
public static void main(String[] args) throws Exception {
System.out.println("JavaHttpsServer " + PROGVERSION);
keystoreFile = args[0];
listenPort = Integer.parseInt(args[1]);
System.out.println("keystoreFile=[" + keystoreFile + "]");
System.out.println("listenPort=[" + listenPort + "]");
SSLContext ssl = SSLContext.getInstance("SSLv3");
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType());
//Load the JKS file (located, in this case, at D:\keystore.jks, with password 'test'
//store.load(new FileInputStream("C:\\Users\\Eclipse-workspaces\\Test\\keystore.jks"), "changeit".toCharArray());
store.load(new FileInputStream(keystoreFile), "changeit".toCharArray());
//init the key store, along with the password 'changeit'
kmf.init(store, "changeit".toCharArray());
KeyManager[] keyManagers = new KeyManager[1];
keyManagers = kmf.getKeyManagers();
// Init the trust manager factory
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
// It will reference the same key store as the key managers
tmf.init(store);
TrustManager[] trustManagers = tmf.getTrustManagers();
ssl.init(keyManagers, trustManagers, new SecureRandom());
// Init a configuration with our SSL context
HttpsConfigurator configurator = new HttpsConfigurator(ssl);
//configurator.configure(hparams);
// Create a new HTTPS Server instance, listening on port 8000
HttpsServer server = HttpsServer.create(new InetSocketAddress(listenPort), 0);
server.setHttpsConfigurator(configurator);
server.createContext("/test", new MyHandler());
server.setExecutor(null); // creates a default executor
server.start();
}
static class MyHandler implements HttpHandler {
@Override
public void handle(HttpExchange t) throws IOException {
String x = t.getRemoteAddress().getHostString();
System.out.println("In handle: Request from (getHostString) = [" + x + "]");
x = t.getRequestURI().toASCIIString();
System.out.println("In handle: getRequestURI = [" + x + "]");
if (x.equalsIgnoreCase("/test?stop")) {
System.out.println("In handle: Received request to exit, so will exit now...");
System.exit(0);
}
System.out.println("In handle: About to send response...");
String response = "This is the response";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
System.out.println("In handle: Finished sending response...");
os.close();
}
}
}
答案 0 :(得分:0)
我不确定我是否正确理解了这个问题,因为您的代码已经显示,如何设置协议。您在SSLContext的getInstance()方法中提供协议。在您的示例中,您使用SSLv3初始化SSLContext。您可以使用此处描述的其中一个字符串:https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#SSLContext
要验证服务器使用的协议,您可以使用" s_client"命令OpenSSL:openssl s_client -connect localhost:8443