如何在org.springframework.ws.transport.http.HttpUrlConnection

时间:2017-10-24 06:28:16

标签: java spring soap ssl-certificate

我有一个spring web应用程序,它有以下http标头拦截器。

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证书?

1 个答案:

答案 0 :(得分:2)

所以显然我找到了一个解决方案。我正在使用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:无法找到有效的证书路径到请求的目标