我知道如何使用证书保护 Web 服务。那是我的客户代码:
SSLContext ssl = SSLContext.getInstance("SSLv3");
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType());
String password = Configuration.getConfig("keyStorePassword");
store.load(new FileInputStream(new File(Configuration.getConfig("keyStore"))), password.toCharArray());
kmf.init(store, password.toCharArray());
KeyManager[] keyManagers = new KeyManager[1];
keyManagers = kmf.getKeyManagers();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(store);
TrustManager[] trustManagers = tmf.getTrustManagers();
ssl.init(keyManagers, trustManagers, new SecureRandom());
HttpsConfigurator configurator = new HttpsConfigurator(ssl);
Integer port = Integer.parseInt(Configuration.getConfig("port"));
HttpsServer httpsServer = HttpsServer.create(new InetSocketAddress(Configuration.getConfig("host"), port), 0);
httpsServer.setHttpsConfigurator(configurator);
Implementor implementor = new Implementor(); // class with @WebService etc.
HttpContext context = (HttpContext) httpsServer.createContext("/EventWebService");
Endpoint endpoint = Endpoint.create( implementor );
endpoint.publish(context);
现在,如何制作“简单 SSL”?如何在不在客户端存储证书的情况下建立 SSL 连接。 (就像在浏览器中通过 HTTPS 连接一样)
最佳答案
Java Runtime Environment 确实在 cacerts 文件中附带了许多(使用最广泛的)证书颁发机构。如果您用于保护服务的证书是由这些根 CA 之一签署的,那么您无需担心与客户端共享任何证书。
但是,如果您使用自签名证书,并且不想在信任库中传递/导入证书,那么您可以实现自定义 X509TrustManager 并为您的连接创建自定义 SSLContext。更多详情在此 blog .
自签名证书对于开发和测试环境很有用,但您确实应该考虑让您的服务器证书由公认的证书颁发机构(如 Verisign、Thwate 等)签署。
关于java - 如何通过 Web 服务生成 'simple SSL'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6810634/