如何通过Web服务实现"简单SSL"?

raz*_*zor 5 java ssl web-services

我知道如何使用证书保护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);
Run Code Online (Sandbox Code Playgroud)

现在,如何制作"简单的SSL"?如何在不在客户端存储证书的情况下建立SSL连接.(就像在浏览器中通过HTTPS连接一样)

hel*_*ios 5

Java运行时环境在cacerts文件中附带了许多(使用最广泛的)证书颁发机构.如果您用于保护服务的证书由其中一个根CA签名,则您无需担心与客户端共享任何证书.

但是,如果您使用自签名证书,并且您不想在信任库中传递/导入证书,则可以实现自定义X509TrustManager并为您的连接创建自定义SSLContext.此博客中的更多详细信息.

自签名证书对开发和测试环境很有用,但您确实应该考虑从Verisign,Thwate等公认的证书颁发机构签署服务器证书.