標籤:java https
使用JDK6+內建的發布ws服務Endpoint.publish
1、普通發布
import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebService;import javax.jws.soap.SOAPBinding;@WebService(serviceName="SendIdcManageService")@SOAPBinding(style=SOAPBinding.Style.RPC)public class SendIdcManageService {@WebMethodpublic String addHostRoomFromIdc(@WebParam(name="xml")String xml){System.out.println("success");return "ok";}}
import javax.xml.messaging.Endpoint;public class TestHttpWs{ public static void main(String[] args) { Endpoint.publish("http://localhost:8222/hello", new SendIdcManageService()); }}
頁面直接存取http://localhost:8222/hello介面
2、https類型發布
首先要產生keystore
keytool -genkey -v -alias jifubao -keyalg RSA -keystore D:\jifubao.keystore -validity 365002
在cmd命令下執行 輸入密碼後都執行斷行符號,在最後出現的位置寫Y介面
即:
CN=Unknown, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown 正確嗎?
[否]: Y
把jifubao.keystore檔案放到項目中
下面樣本中加入輸入密碼為123456
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.net.InetSocketAddress;import java.security.KeyManagementException;import java.security.KeyStore;import java.security.KeyStoreException;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import java.security.UnrecoverableKeyException;import java.security.cert.CertificateException;import javax.jws.WebService;import javax.net.ssl.KeyManager;import javax.net.ssl.KeyManagerFactory;import javax.net.ssl.SSLContext;import javax.net.ssl.TrustManager;import javax.net.ssl.TrustManagerFactory;import javax.xml.ws.Endpoint;import com.sun.net.httpserver.HttpContext;import com.sun.net.httpserver.HttpsConfigurator;import com.sun.net.httpserver.HttpsServer;@WebService(targetNamespace="http://hello/")public class HttpsWsHelloWorld {public String sayHi(){return "Hello World!";}public static void main(String[] args) throws CertificateException, FileNotFoundException, IOException, UnrecoverableKeyException { try {SSLContext ssl = SSLContext.getInstance("TLS");KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());KeyStore store = KeyStore.getInstance("JKS");String keystoreFile="d:\\11\\jifubao.keystore";String keyPass="123456";store.load(new FileInputStream(keystoreFile),keyPass.toCharArray()); keyFactory.init(store, keyPass.toCharArray()); TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());trustFactory.init(store);KeyManager[] keyManagers=new KeyManager[1];keyManagers=keyFactory.getKeyManagers();TrustManager[] trustManagers=trustFactory.getTrustManagers();ssl.init(keyManagers, trustManagers, new SecureRandom());HttpsConfigurator configurator = new HttpsConfigurator(ssl); HttpsServer httpsServer = HttpsServer.create(new InetSocketAddress("localhost", 8441),8441);httpsServer.setHttpsConfigurator(configurator); HttpContext httpContext = httpsServer.createContext("/Hello"); httpsServer.start(); Endpoint endpoint = Endpoint.create(new HttpsWsHelloWorld());endpoint.publish(httpContext); } catch (NoSuchAlgorithmException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} catch (KeyStoreException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (KeyManagementException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
頁面直接存取https://localhost:8441/hello即可
發布https類型的WS服務