JWS-Exception

1、javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching localhost found

使用localhost作为域名,SSL校验时报错,允许localhost认证,加入以下静态代码块:

static {
        //for localhost testing only
        javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
                new javax.net.ssl.HostnameVerifier(){

                    public boolean verify(String hostname,
                                          javax.net.ssl.SSLSession sslSession) {
                        if (hostname.equals("localhost")) {
                            return true;
                        }
                        return false;
                    }
                });
    }
参考: https://www.mkyong.com/webservices/jax-ws/java-security-cert-certificateexception-no-name-matching-localhost-found/

2、Caused by: java.net.SocketException: Unexpected end of file from server

由于JWS服务使用https协议,但是客户端调用时使用时,URL中配置的非https协议,服务器端没响应任何数据,而是直接和客户端进行挥手,服务器端主动断开链接。

解决:URL的链接使用https


3、Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

代码方式:客户端无法校验单向的https服务器安全性,需要客户端主动设置服务器端https校验的keystore.

public static void loadTrustStore() {
        System.setProperty("javax.net.ssl.trustStore", "E:/client.keystore"); //(truststore)
        System.setProperty("javax.net.ssl.trustStorePassword", "12345678"); //(truststore 密码 )
        System.setProperty("javax.net.ssl.trustStoreType", "JKS"); //(truststore 类型 )
    }

命令行方式:WsImport导出客户端wsdl

java -classpath "E:\programs\java\jdk1.8.0_151\lib\tools.jar" -Djavax.net.ssl.trustStore="E:/client.keystore" -Djavax.net.ssl.trustStorePassword=12345678 com.sun.tools.internal.ws.WsImport https://localhost:9090/service/sayHi?wsdl -s . -p com.shu.jwsclient.say -XdisableSSLHostnameVerification

参考:https://stackoverflow.com/questions/36316716/wsimport-unable-to-find-imported-certificate

示例:客户端调用https服务器的jws服务:

package com.shu.jwsclient.say;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

/**
 * 客户端调用服务器端方法
 *
 * @author: jiangshubian
 * @Description:
 * @Date: Create in 2018-01-14 15:26
 * @Version: 1.0.0
 */
public class JWSClientInvoker {

    static {
        //for localhost testing only
        javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
                new javax.net.ssl.HostnameVerifier(){

                    public boolean verify(String hostname,
                                          javax.net.ssl.SSLSession sslSession) {
                        if ("localhost".equals(hostname)) {
                            return true;
                        }
                        return false;
                    }
                });
    }

    public static void main(String[] args) throws DatatypeConfigurationException {
        loadTrustStore();
//        runerTest();
        runnerWithoutWSFile();
    }

    private static void runerTest() throws DatatypeConfigurationException {
        SayHiService sayHiService = new SayHiServiceImpService().getSayHiServiceImpPort();
        //invoke sayNothing method
        sayHiService.sayNothing();

        //invoke saySomething method
        sayHiService.saySomething("Something...");

        //invoke checkTime method
        GregorianCalendar calender = new GregorianCalendar();
        calender.setTime(new java.util.Date(System.currentTimeMillis()));
        XMLGregorianCalendar xmldate = DatatypeFactory.newInstance().newXMLGregorianCalendar(calender);
        System.out.println(sayHiService.checkTime(xmldate));
    }

    private static void runnerWithoutWSFile() {
        URL url = null;
        try {
            url = new URL("https://localhost:9090/service/sayHi?wsdl");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        QName qname = new QName("http://say.jws.shu.com/", "SayHiServiceImpService");

        Service service = Service.create(url, qname);
        SayHiService sayHi = service.getPort(SayHiService.class);

        sayHi.sayNothing();

        sayHi.saySomething("runnerWithoutWSFile");
    }

    public static void loadTrustStore() {
        System.setProperty("javax.net.ssl.trustStore", "E:/client.keystore"); //(truststore)
        System.setProperty("javax.net.ssl.trustStorePassword", "12345678"); //(truststore 密码 )
        System.setProperty("javax.net.ssl.trustStoreType", "JKS"); //(truststore 类型 )
    }

}



以下是一个使用 JAX-WS 打印 SOAP 头信息的示例代码: ```java import javax.xml.namespace.QName; import javax.xml.soap.SOAPHeader; import javax.xml.ws.handler.MessageContext; import javax.xml.ws.handler.soap.SOAPHandler; import javax.xml.ws.handler.soap.SOAPMessageContext; public class MySoapHandler implements SOAPHandler<SOAPMessageContext> { public boolean handleMessage(SOAPMessageContext context) { Boolean isResponse = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); if (!isResponse) { try { SOAPHeader header = context.getMessage().getSOAPHeader(); if (header != null) { QName qname = new QName("http://example.com/soap/header", "MyHeader"); String value = header.getAttributeValue(qname); System.out.println("MyHeader value: " + value); } } catch (Exception e) { System.err.println("Error occurred while processing SOAP header: " + e.getMessage()); } } return true; } public boolean handleFault(SOAPMessageContext context) { return true; } public void close(MessageContext context) { } public Set<QName> getHeaders() { return null; } } ``` 在此示例中,我们实现了 `SOAPHandler` 接口,并重写了 `handleMessage` 方法。在该方法中,我们首先检查消息是否为响应消息。如果不是响应消息,则获取 SOAP 头信息并打印其中名为 "MyHeader" 的属性的值。 要使用此处理程序,请按照以下步骤操作: 1. 创建一个 `SOAPHandler` 实现类,如上所示。 2. 在您的 Web 服务客户端代码中,创建一个 `HandlerResolver` 实例,并将其添加到您的服务端口中。 3. 在 `HandlerResolver` 实例中,添加您创建的 `SOAPHandler` 实例。 以下是一个示例 `HandlerResolver` 实现类: ```java import java.util.ArrayList; import java.util.List; import javax.xml.ws.handler.Handler; import javax.xml.ws.handler.HandlerResolver; import javax.xml.ws.handler.PortInfo; public class MyHandlerResolver implements HandlerResolver { public List<Handler> getHandlerChain(PortInfo portInfo) { List<Handler> handlerChain = new ArrayList<Handler>(); handlerChain.add(new MySoapHandler()); return handlerChain; } } ``` 在此示例中,我们创建了一个 `HandlerResolver` 类,并重写了 `getHandlerChain` 方法。在该方法中,我们添加了我们创建的 `SOAPHandler` 实例。 要将此处理程序添加到您的 Web 服务客户端代码中,请按照以下步骤操作: ```java MyWebService service = new MyWebService(); MyWebServicePortType port = service.getMyWebServicePort(); BindingProvider bindingProvider = (BindingProvider) port; bindingProvider.getBinding().setHandlerResolver(new MyHandlerResolver()); ``` 在此示例中,我们首先创建了一个 `MyWebService` 实例,并获取其端口。然后,我们将其转换为 `BindingProvider`,并设置一个新的 `HandlerResolver` 实例,该实例包含我们创建的 `SOAPHandler`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值