【问题标题】:How to ignore ssl certificates in org.springframework.ws.transport.http.HttpUrlConnection如何忽略 org.springframework.ws.transport.http.HttpUrlConnection 中的 ssl 证书
【发布时间】:2017-10-24 06:28:16
【问题描述】:

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

【问题讨论】:

  • 为什么需要忽略证书?它是无效/不受信任的吗?也许这有助于How to disable SSL certificate checking with Spring RestTemplate?
  • 正在尝试访问可在 VPN 中访问的 API。证书不存在,并且卷发使用 -k 标志工作正常。正在寻找在代码中执行此操作的方法(WebServiceTemplate)。

标签: java spring soap ssl-certificate


【解决方案1】:

所以显然我想出了一个解决方案。我正在使用 WebServiceTemplate 来编组SendAndReceive 响应。这是我在代码中所做的:-

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

【讨论】:

    猜你喜欢
    • 2019-09-02
    • 2023-03-31
    • 2016-01-28
    • 2021-09-26
    • 2019-09-05
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    相关资源
    最近更新 更多