最近在做一个需求,用到了https进行接口交互,在测试环境报如下错:
10:20:21,667 ERROR [stderr](http-executor-threads - 39) java.lang.IllegalArgumentException: TLSv1.2
10:20:21,667 ERROR [stderr] (http-executor-threads - 39) java.lang.IllegalArgumentException: TLSv1.2
10:20:21,668 ERROR [stderr] (http-executor-threads - 39) at com.sun.net.ssl.internal.ssl.ProtocolVersion.valueOf(ProtocolVersion.java:133)
10:20:21,669 ERROR [stderr] (http-executor-threads - 39) at com.sun.net.ssl.internal.ssl.ProtocolList.(ProtocolList.java:38)
10:20:21,670 ERROR [stderr] (http-executor-threads - 39) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.setEnabledProtocols(SSLSocketImpl.java:2218)
10:20:21,671 ERROR [stderr] (http-executor-threads - 39) at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:418)
10:20:21,671 ERROR [stderr] (http-executor-threads - 39) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:166)
10:20:21,672 ERROR [stderr] (http-executor-threads - 39) at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:133)
10:20:21,676 ERROR [stderr] (http-executor-threads - 39) at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
10:20:21,676 ERROR [stderr] (http-executor-threads - 39) at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
10:20:21,677 ERROR [stderr] (http-executor-threads - 39) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
10:20:21,678 ERROR [stderr] (http-executor-threads - 39) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
10:20:21,679 ERROR [stderr] (http-executor-threads - 39) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
10:20:21,680 ERROR [stderr] (http-executor-threads - 39) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:511)
10:20:21,680 ERROR [stderr] (http-executor-threads - 39) at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
10:20:21,681 ERROR [stderr] (http-executor-threads - 39) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
10:20:21,682 ERROR [stderr] (http-executor-threads - 39) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)
10:20:21,682 ERROR [stderr] (http-executor-threads - 39) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
10:20:21,683 ERROR [stderr] (http-executor-threads - 39) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
10:20:21,684 ERROR [stderr] (http-executor-threads - 39) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
10:20:21,685 ERROR [stderr] (http-executor-threads - 39) at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)
10:20:21,685 ERROR [stderr] (http-executor-threads - 39) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
10:20:21,686 ERROR [stderr] (http-executor-threads - 39) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
10:20:21,687 ERROR [stderr] (http-executor-threads - 39) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:567)
10:20:21,688 ERROR [stderr] (http-executor-threads - 39) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
10:20:21,688 ERROR [stderr] (http-executor-threads - 39) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)
10:20:21,689 ERROR [stderr] (http-executor-threads - 39) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
10:20:21,690 ERROR [stderr] (http-executor-threads - 39) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)
10:20:21,691 ERROR [stderr] (http-executor-threads - 39) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:518)
10:20:21,691 ERROR [stderr] (http-executor-threads - 39) at org.jboss.threads.SimpleDirectExecutor.execute(SimpleDirectExecutor.java:33)
10:20:21,692 ERROR [stderr] (http-executor-threads - 39) at org.jboss.threads.QueueExecutor.runTask(QueueExecutor.java:801)
10:20:21,693 ERROR [stderr] (http-executor-threads - 39) at org.jboss.threads.QueueExecutor.access$100(QueueExecutor.java:45)
10:20:21,693 ERROR [stderr] (http-executor-threads - 39) at org.jboss.threads.QueueExecutor$Worker.run(QueueExecutor.java:821)
10:20:21,694 ERROR [stderr] (http-executor-threads - 39) at java.lang.Thread.run(Thread.java:662)
10:20:21,694 ERROR [stderr] (http-executor-threads - 39) at org.jboss.threads.JBossThread.run(JBossThread.java:122)
IllegalArgumentException非法的参数值,相同代码我再本地试了一下,没有任何问题。我客户端是用jdk里的javax.net.ssl.HttpsURLConnection;写的,没有用httpclient。本地和测试环境唯一不同的就是JDK版本了。我本地用的是JDK1.6.0_11,测试环境是JDK1.6.0_45。我在本地修改代码添加如下:
System.setProperty("https.protocols", "TLSv1.2,SSLv3");这时候本地也会报如上错误.
通过下面这段代码可以查看本地支持的TLS协议。
public static void main(String[] args) throws Exception {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, null, null);
SSLSocketFactory factory = (SSLSocketFactory) context.getSocketFactory();
SSLSocket socket = (SSLSocket) factory.createSocket();
String[] protocols = socket.getSupportedProtocols();
System.out.println("Supported Protocols: " + protocols.length);
for (int i = 0; i < protocols.length; i++) {
System.out.println(" " + protocols[i]);
}
protocols = socket.getEnabledProtocols();
System.out.println("Enabled Protocols: " + protocols.length);
for (int i = 0; i < protocols.length; i++) {
System.out.println(" " + protocols[i]);
}
}
我本地执行完如下图:
测试环境执行结果如下:
跟我本地是一样的,都是只支持TLSv1,所以我在代码里添加System.setProperty("https.protocols", "TLSv1,SSLv3");即指定运行的protocols协议。这样在我本地和测试环境都能正常执行。