Https请求异常:javax.net.ssl.SSLHandshakeException

通过HttpClient对https协议的url发出请求,产生异常:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: basic constraints check failed: pathLenConstraint violated - this cert must be the last cert in the certification path 
是由于Java安全性策略导致,解决方法有导入cert证书和改变安全策略,选择第二种通过代码改变安全性,无条件接受所有证书,加两个类、两行代码:
类一:
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.HttpClientError;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
public class MySecureProtocolSocketFactory implements
  SecureProtocolSocketFactory {
 private SSLContext sslContext = null;
 /**
  * Constructor for MySecureProtocolSocketFactory.
  */
 public MySecureProtocolSocketFactory() {
 }
 /**
  *
  * @return
  */
 private static SSLContext createEasySSLContext() {
  try {
   SSLContext context = SSLContext.getInstance("SSL");
   context.init(null, new TrustManager[] { new MyX509TrustManager() },
     null);
   return context;
  } catch (Exception e) {
   throw new HttpClientError(e.toString());
  }
 }
 /**
  *
  * @return
  */
 private SSLContext getSSLContext() {
  if (this.sslContext == null) {
   this.sslContext = createEasySSLContext();
  }
  return this.sslContext;
 }
 /*
  * (non-Javadoc)
  *
  * @see org.apache.commons.httpclient.protocol.ProtocolSocketFactory#createSocket(java.lang.String,
  *      int, java.net.InetAddress, int)
  */
 public Socket createSocket(String host, int port, InetAddress clientHost,
   int clientPort) throws IOException, UnknownHostException {
  return getSSLContext().getSocketFactory().createSocket(host, port,
    clientHost, clientPort);
 }
 /*
  * (non-Javadoc)
  *
  * @see org.apache.commons.httpclient.protocol.ProtocolSocketFactory#createSocket(java.lang.String,
  *      int, java.net.InetAddress, int,
  *      org.apache.commons.httpclient.params.HttpConnectionParams)
  */
 public Socket createSocket(final String host, final int port,
   final InetAddress localAddress, final int localPort,
   final HttpConnectionParams params) throws IOException,
   UnknownHostException, ConnectTimeoutException {
  if (params == null) {
   throw new IllegalArgumentException("Parameters may not be null");
  }
  int timeout = params.getConnectionTimeout();
  if (timeout == 0) {
   return createSocket(host, port, localAddress, localPort);
  } else {
   return ControllerThreadSocketFactory.createSocket(this, host, port,
     localAddress, localPort, timeout);
  }
 }
 /*
  * (non-Javadoc)
  *
  * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int)
  */
 public Socket createSocket(String host, int port) throws IOException,
   UnknownHostException {
  return getSSLContext().getSocketFactory().createSocket(host, port);
 }
 /*
  * (non-Javadoc)
  *
  * @see SecureProtocolSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean)
  */
 public Socket createSocket(Socket socket, String host, int port,
   boolean autoClose) throws IOException, UnknownHostException {
  return getSSLContext().getSocketFactory().createSocket(socket, host,
    port, autoClose);
 }
}
类二:
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;
public class MyX509TrustManager implements X509TrustManager {
 /*
  * (non-Javadoc)
  *
  * @see javax.net.ssl.X509TrustManager#checkClientTrusted(java.security.cert.X509Certificate[],
  *      java.lang.String)
  */
 public void checkClientTrusted(X509Certificate[] arg0, String arg1)
   throws CertificateException {
 }
 /*
  * (non-Javadoc)
  *
  * @see javax.net.ssl.X509TrustManager#checkServerTrusted(java.security.cert.X509Certificate[],
  *      java.lang.String)
  */
 public void checkServerTrusted(X509Certificate[] arg0, String arg1)
   throws CertificateException {
 }
 /*
  * (non-Javadoc)
  *
  * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()
  */
 public X509Certificate[] getAcceptedIssuers() {
  return null;
 }

 两行代码:
HttpClient请求之前,加
//接受所有的安全证书
  ProtocolSocketFactory fcty = new MySecureProtocolSocketFactory();
  Protocol.registerProtocol("https", new Protocol("https", fcty, 443)); 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用:HTTP Status 500 - javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find ... 引用:Java Spring应用发送数据报如下问题。 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 原因分析: 引用:用httpclient访问https资源时,会出现异常,与环境也有关系,有些机器请求正常。 解决方案: 在HTTPS通信中,当Java程序尝试与服务端建立安全连接时,会进行SSL握手过程。如果在握手过程中出现异常javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException,可能是由于证书验证失败导致的。这种异常通常有两种原因:一是服务端证书不被信任,二是客户端无法找到合适的证书链。 解决这个问题的方法有以下几种: 1. 信任自签名证书:可以通过自定义TrustManager来信任自签名的证书。但这种方法存在安全风险,因为所有的自签名证书都会被信任。 2. 导入服务端证书:可以将服务端的证书导入到Java的信任证书库中,以确保它被信任。可以使用keytool工具将证书导入到Java信任库中。 3. 禁用证书验证:在开发环境中,可以禁用证书验证来避免这个问题。但在生产环境中不建议这样做,因为会降低通信的安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值