上述参数的具体说明如下:
keytool -genkey -alias service(别名) -keypass 123456(别名密码) -keyalg RSA(算法) -keysize 1024(密钥长度) -validity 365(有效期,天单位) -keystore D:/keys/server.jks(指定生成证书的位置和证书名称) -storepass 123456(获取jks信息的密码) 发布的soap接口 /** * @author Eric * @Title: SoapService * @date 2019/7/17 10:42 * @Description: 发布的soap接口,注意注释的使用 */ @WebService public interface SoapService { void sayHello(@WebParam(name = "clientName") String clientName); } 接口的具体实现类 /** * @author Eric * @Title: SoapServiceImpl * @date 2019/7/17 10:42 * @Description: soap接口的具体实现,注意注释的使用 */ @javax.jws.WebService public class SoapServiceImpl implements SoapService { @Override public void sayHello(String clientName) { System.out.println(clientName + "调用接口打了招呼"); } } 发布soap接口 /** * @author Eric * @Title: testTest * @date 2019/7/17 10:45 * @Description: 发布soap接口 */ public class Release { public static void main(String[] args) throws Exception { initSoap(); } //初始化soap接口 public static void initSoap() { try { String host = "0.0.0.0"; int port = 8888; String address = "https://" + host + ":" + port + "/logProcessor"; //发布于https JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean(); sf.setServiceClass(SoapService.class); //发布实现soap的接口类(你自己实现的接口,里面的方法就是发布的soap接口) sf.setAddress(address); SoapServiceImpl soapServiceImpl = new SoapServiceImpl(); //SoapService的实现类 sf.getServiceFactory().setInvoker(new BeanInvoker(soapServiceImpl)); sf = configureSSLOnTheServer(sf, port); Server server = sf.create(); String endpoint = server.getEndpoint().getEndpointInfo().getAddress(); System.out.println("soap发布的地址为:" + endpoint); } catch (Exception e) { System.out.println("Soap初始化失败:"+e); } } //https并且开启客户端认证。如果发布的接口是http,则无需实现该方法。 private static JaxWsServerFactoryBean configureSSLOnTheServer(JaxWsServerFactoryBean sf, int port) throws Exception { TLSServerParameters tlsParams = new TLSServerParameters(); ClientAuthentication clientAuthentication = new ClientAuthentication(); clientAuthentication.setRequired(true); //开启客户端认证 tlsParams.setClientAuthentication(clientAuthentication); KeyStore keyStore = KeyStore.getInstance("JKS"); String password = "123456"; //生成证书时候定义的的密码 File truststore = new File(Paths.get("D:\\keys", "server.jks").toString()); keyStore.load(new FileInputStream(truststore), password.toCharArray()); KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyFactory.init(keyStore, password.toCharArray()); KeyManager[] km = keyFactory.getKeyManagers(); tlsParams.setKeyManagers(km); truststore = new File(Paths.get("D:\\keys", "server.jks").toString()); keyStore.load(new FileInputStream(truststore), password.toCharArray()); TrustManagerFactory trustFactory = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustFactory.init(keyStore); TrustManager[] tm = trustFactory.getTrustManagers(); tlsParams.setTrustManagers(tm); JettyHTTPServerEngineFactory factory = new JettyHTTPServerEngineFactory(); factory.setTLSServerParametersForPort(port, tlsParams); } } 使用soapUI模拟客户端调用soap接口通过上述代码,我们将接口发布在了你自己定义的https://0.0.0.0:8888/logProcessor。如果发布成功,我们想远程调用soap接口,此时打开soapUI
选择左上角File然后选择Preferences。
选择SSL Settings,在KeyStore上导入我们生成的证书。否则无法远程调用(因为我们在代码中开启了客户端认证,如果没有开启可以跳过这一步)
然后返回主界面,创建soap,在弹出的界面中 ‘Project Name’可以自己定义。‘Initial WSDL’中输入你发布的地址加上‘?wsdl’如下:https://0.0.0.0:8888/logProcessor?wsdl
如果创建成功,你就能在左边看到自己项目中发布的接口。选择我们发布的接口sayHello,双击Request。