如何忽略 org.springframework.ws.transport.http.HttpUrlConnection 中的 ssl 证书

How to ignore ssl certificates in org.springframework.ws.transport.http.HttpUrlConnection

我有一个 spring 网络应用程序,它具有以下 http header 拦截器。

import org.springframework.ws.transport.context.TransportContext;
import org.springframework.ws.transport.context.TransportContextHolder;
import org.springframework.ws.transport.http.HttpUrlConnection;

public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
    TransportContext context = TransportContextHolder.getTransportContext();
    HttpUrlConnection connection = (HttpUrlConnection) context
            .getConnection();

    String userCredentials = username + ":" + password;
    String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));

    connection.getConnection().addRequestProperty("Authorization", basicAuth);

    return true;
}

如何修改我的代码以忽略 ssl 证书?

显然我找到了解决方案。我正在使用 WebServiceTemplate 来 marshalSendAndReceive 响应。这是我在代码中所做的:-

HttpsUrlConnectionMessageSender sender = new HttpsUrlConnectionMessageSender();
sender.setTrustManagers(new TrustManager[] { new UnTrustworthyTrustManager() });
webServiceTemplate.setMessageSender(sender);
response = webServiceTemplate.marshalSendAndReceive(object);

遵循 UnTrustworthyTrustManager 函数

class UnTrustworthyTrustManager
        implements X509TrustManager
{
    public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}
    public void checkServerTrusted(X509Certificate[] arg0, String arg1)throws CertificateException {}
    public X509Certificate[] getAcceptedIssuers() { return null; }
}

由此,SSL 证书被忽略,我摆脱了错误 PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径