https 请求webServic 产生的安全证书问题 jdk版本导致的Received fatal alert: protocol_version异常

Posted 一呆又一天

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了https 请求webServic 产生的安全证书问题 jdk版本导致的Received fatal alert: protocol_version异常相关的知识,希望对你有一定的参考价值。

以下是我在开发中 用https去请求webService服务因为jdk版本产生的异常信息:

在本机的环境是jdk1.8 使用axis.client发送https请求正常,但是放在服务器上jdk版本是1.7 就会产生以下问题:经过一天的请教,终于解决:

http://xml.apache.org/axis/}stackTrace:javax.net.ssl.SSLException: Received fatal alert: protocol_version
at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1979)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1086)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1332)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1359)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1343)
at org.apache.axis.components.net.JSSESocketFactory.create(JSSESocketFactory.java:186)
at org.apache.axis.transport.http.HTTPSender.getSocket(HTTPSender.java:191)
at org.apache.axis.transport.http.HTTPSender.writeToSocket(HTTPSender.java:404)
at org.apache.axis.transport.http.HTTPSender.invoke(HTTPSender.java:138)
at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
at org.apache.axis.client.AxisClient.invoke(AxisClient.java:165)
at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
at org.apache.axis.client.Call.invoke(Call.java:2767)
at org.apache.axis.client.Call.invoke(Call.java:2443)
at org.apache.axis.client.Call.invoke(Call.java:2366)
at org.apache.axis.client.Call.invoke(Call.java:1812)
at util.CxfClient.test2(CxfClient.java:83)
at util.CxfClient.main(CxfClient.java:59)

{http://xml.apache.org/axis/}hostname:WIN7-038

javax.net.ssl.SSLException: Received fatal alert: protocol_version
at org.apache.axis.AxisFault.makeFault(AxisFault.java:101)
at org.apache.axis.transport.http.HTTPSender.invoke(HTTPSender.java:154)
at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
at org.apache.axis.client.AxisClient.invoke(AxisClient.java:165)
at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
at org.apache.axis.client.Call.invoke(Call.java:2767)
at org.apache.axis.client.Call.invoke(Call.java:2443)
at org.apache.axis.client.Call.invoke(Call.java:2366)
at org.apache.axis.client.Call.invoke(Call.java:1812)
at util.CxfClient.test2(CxfClient.java:83)
at util.CxfClient.main(CxfClient.java:59)
Caused by: javax.net.ssl.SSLException: Received fatal alert: protocol_version
at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1979)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1086)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1332)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1359)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1343)
at org.apache.axis.components.net.JSSESocketFactory.create(JSSESocketFactory.java:186)
at org.apache.axis.transport.http.HTTPSender.getSocket(HTTPSender.java:191)
at org.apache.axis.transport.http.HTTPSender.writeToSocket(HTTPSender.java:404)
at org.apache.axis.transport.http.HTTPSender.invoke(HTTPSender.java:138)
... 11 more

 

方法一: 将项目的jdk版本改成1.8可以解决此问题

方法二:调用方法 让所有请求都是安全的内容如下:

 

/**
* 调用第三方服务方法
* @param params :请求参数(注意:Map的key是对方接受参数的String)
* @param url :请求地址
* @param qNameUrl :名称域地址
* @param mothed :请求方法
* @param key :对方提供的appid与appsecret
* @return :返回字符串
* @throws KeyManagementException
* @throws NoSuchAlgorithmException
* @throws ServiceException
* @throws MalformedURLException
* @throws RemoteException
*/
//"https://134.175.33.111/hzs/webservice/Greeting?wsdl" "http://server.sl.com/"
public static String requestClient (Map<String,Object> params, String url, String qNameUrl, String mothed,String key) throws KeyManagementException,   NoSuchAlgorithmException, ServiceException, MalformedURLException, RemoteException{
  logger.info(" 进入请求第三方服务的公共方法,请求参数为:"+params+",请求的地址为:"+url+",请求方法为:"+mothed);

  String result = "";
  javax.net.ssl.TrustManager [] truset = new javax.net.ssl.TrustManager[1];
  javax.net.ssl.TrustManager tm = new miTM();
  truset[0] = tm;

  SSLContext context = SSLContext.getInstance("TLSv1.2");
  context.init(null, truset, null);
  SSLContext.setDefault(context);

  Service service = new Service();
  Call call = null;

  List<Object> object = new ArrayList<Object>();
  //获取连接
  call = (Call) service.createCall();
  call.setTargetEndpointAddress(new java.net.URL(url));
  call.setOperationName(new QName(qNameUrl, mothed));
  for(Entry<String, Object> entry : params.entrySet() ){
  call.addParameter(entry.getKey(), org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
  object.add(entry.getValue());
}
  object.add(key);
  call.addParameter("key", org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
  call.setReturnType(org.apache.axis.Constants.XSD_STRING);
  result = (String) call.invoke(object.toArray());

  return result;

}

/**
* 设置所有的请求都是安全的
*
*/
static class miTM implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
  return null;
}

public boolean isServerTrusted(java.security.cert.X509Certificate[] certs) {
  return true;
}

public boolean isClientTrusted(java.security.cert.X509Certificate[] certs) {
  return true;
}

public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
  return;
}

public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
  return;
}
}

















































































































以上是关于https 请求webServic 产生的安全证书问题 jdk版本导致的Received fatal alert: protocol_version异常的主要内容,如果未能解决你的问题,请参考以下文章

谁知道ssl安全证书哪里下载,怎么获得ssl安全证书?

web安全

2008申请SSL证书,IIS7怎么申请SSL安全证书

https CA自签名证书,并给Webserver颁发证书

如何用C++实现支持HTTPS的RESTful WebServer

post请求https安全证书问题