如何在org.springframework.ws.transport.http.HttpUrlConnection中忽略SSL证书

3
我有一个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证书?


为什么需要忽略证书?它是无效/不受信任的吗?也许这可以帮助你:如何使用Spring RestTemplate禁用SSL证书检查? - undefined
曾试图访问一个只能在VPN内部访问的API。证书并不存在,但使用**-k**参数的curl命令却可以正常工作。现在正在寻找在代码(WebServiceTemplate)中实现同样效果的方法。 - undefined
1个回答

3

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

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

下面是关于不可信任的TrustManager函数的内容:

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:无法找到所请求的目标的有效认证路径


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接