标签: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 { @WebMethod public 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 block e1.printStackTrace(); } catch (KeyStoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (KeyManagementException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
页面直接访问https://localhost:8441/hello即可
标签:java https
原文地址:http://xuanxy.blog.51cto.com/2357481/1591370