發布https類型的WS服務

來源:互聯網
上載者:User

標籤: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服務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.