https在weblogic下访问报错,jboss和tomcat下好使

来源:互联网 发布:淘宝美工和ui设计师 编辑:程序博客网 时间:2024/06/05 04:45

转发之前,先发表两句感慨。https访问好麻烦,跟http还是有很大区别的。另外在weblogic、tomcat、jboss上用https也是有差异的,和容器有关。

代码如下:

[java] view plain copy
  1. public static JSONObjects httpsRequest(String requestUrl, String requestMethod, String outputStr) {  
  2.         JSONObject jsonObject = null;  
  3.         try {  
  4.             // 创建SSLContext对象,并使用我们指定的信任管理器初始化  
  5.             TrustManager[] tm = { new MyX509TrustManager() };  
  6.             SSLContext sslContext = SSLContext.getInstance("SSL""SunJSSE");  
  7.             sslContext.init(null, tm, new java.security.SecureRandom());  
  8.             // 从上述SSLContext对象中得到SSLSocketFactory对象  
  9.             SSLSocketFactory ssf = sslContext.getSocketFactory();  
  10.             log.info("Common=========== ");  
  11.             URL url = new URL(requestUrl);      
  12.             log.info("Common=========== ");    
  13.             HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();  
  14.             conn.setSSLSocketFactory(ssf);  
  15.               
  16.             conn.setDoOutput(true);  
  17.             conn.setDoInput(true);  
  18.             conn.setUseCaches(false);  
  19.             // 设置请求方式(GET/POST)  
  20.             conn.setRequestMethod(requestMethod);  


1、程序在tomcat和jboss上没问题,在weblogic上跑报错,日志错误为:

[java] view plain copy
  1. ####<2015-12-11 上午093221秒 CST> <Error> <com.n22.msg.weixin.util.CommonUtil> <WIN-CBCCPK0SHE8> <Server-1> <[ACTIVE] ExecuteThread: '14' for queue: 'weblogic.kernel.Default (self-tuning)'> <<anonymous>> <> <> <1449797541041> <BEA-000000> <https请求异常:{}  
  2. java.lang.ClassCastException: weblogic.net.http.SOAPHttpsURLConnection cannot be cast to javax.net.ssl.HttpsURLConnection  
  3.     at com.nnn.msg.weixin.util.CommonUtil.httpsRequest(CommonUtil.java:54)  
  4.     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  
  5.     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)  
  6.     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
  7.     at java.lang.reflect.Method.invoke(Method.java:606)  
  8.     at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)  
  9.     at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)  
  10.     at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111)  
  11.     at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806)  
  12.     at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729)  
  13.     at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)  
  14.     at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)  
  15.     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)  
  16.     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)  
  17.     at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)  
  18.     at javax.servlet.http.HttpServlet.service(HttpServlet.java:751)  
  19.     at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)  
  20.     at javax.servlet.http.HttpServlet.service(HttpServlet.java:844)  
  21.     at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)  
  22.     at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)  
  23.     at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)  
  24.     at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:346)  
  25.     at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:25)  
  26.     at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)  
  27.     at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)  
  28.     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)  
  29.     at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)  
  30.     at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3436)  
  31.     at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3402)  
  32.     at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)  
  33.     at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)  
  34.     at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)  
  35.     at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2285)  
  36.     at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2201)  
  37.     at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2179)  
  38.     at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1572)  
  39.     at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:255)  
  40.     at weblogic.work.ExecuteThread.execute(ExecuteThread.java:311)  
  41.     at weblogic.work.ExecuteThread.run(ExecuteThread.java:263)  
  42. >  


2、问题原因:

服务器环境的原因导致了报错,查看相关资料。

①在WEBLOGIC没有对使用的SSL实现类进行配置的情况下,在程序中如果正常使用Java.NET.URL的不带 URLStreamHandler 参数的构造方法new 一个URL对象的话(URL url = new URL(requestUrl);),url.openConnection()默认是返回SOAPHttpsURLConnection类型对象,导致转换失败。


3、解决办法:

①看到网上的解决办法将URL url = new URL(requestUrl);改成URL url = new URL(null,requestUrl,new com.sun.Net.ssl.internal.www.protocol.https.Handler());

尝试了下,在本地Tomact上都不通过,后来在服务器weblogic上测试还是失败。


②将URL url = new URL(requestUrl);改成URL url = new URL(null,requestUrl,new sun.net.www.protocol.https.Handler());本地tomcat测试没问题,服务器weblogic上测试还是失败。


从Weblogic配置的层面上解决报错的问题。对于单机的Weblogic配置,可以在启动脚本中(比如startWebLogic.sh)在JAVA_OPTIONS增加 -DUseSunHttpHandler=true ,例如

[html] view plain copy
  1. <span style="font-size:18px;">set JAVA_OPTIONS=%JAVA_OPTIONS% %JAVA_PROPERTIES% -DUseSunHttpHandler=true -Dwlw.iterativeDev=%iterativeDevFlag% -  
  2.  Dwlw.testConsole=%testConsoleFlag% -Dwlw.logErrorsToConsole=% logErrorsToConsoleFlag%</span>  

如果是配置了集群,则不必在启动脚本中增加参数,可以在weblogic控制台中针对特定server配置此参数

这个参数的目的就是告诉WebLogic使用Sun的HttpHandler而不要使用WebLogic自己的。这样配置后使用url.openConnection()就会返回HttpsURLConnection类型对象了。

4、如果用的是jrockit,启动脚本中用的是USER_MEM_ARGS,可以在这后面加上DUseSunHttpHandler=true

如下:


原创粉丝点击