您的位置:首页 > 理论基础 > 计算机网络

[置顶] https在weblogic下访问报错,jboss和tomcat下好使

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

代码如下:

[java] view
plain copy

public static JSONObjects httpsRequest(String requestUrl, String requestMethod, String outputStr) {  

        JSONObject jsonObject = null;  

        try {  

            // 创建SSLContext对象,并使用我们指定的信任管理器初始化  

            TrustManager[] tm = { new MyX509TrustManager() };  

            SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");  

            sslContext.init(null, tm, new java.security.SecureRandom());  

            // 从上述SSLContext对象中得到SSLSocketFactory对象  

            SSLSocketFactory ssf = sslContext.getSocketFactory();  

            log.info("Common=========== ");  

            URL url = new URL(requestUrl);      

            log.info("Common=========== ");    

            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();  

            conn.setSSLSocketFactory(ssf);  

              

            conn.setDoOutput(true);  

            conn.setDoInput(true);  

            conn.setUseCaches(false);  

            // 设置请求方式(GET/POST)  

            conn.setRequestMethod(requestMethod);  

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

[java] view
plain copy

####<2015-12-11 上午09时32分21秒 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请求异常:{}  

java.lang.ClassCastException: weblogic.net.http.SOAPHttpsURLConnection cannot be cast to javax.net.ssl.HttpsURLConnection  

    at com.nnn.msg.weixin.util.CommonUtil.httpsRequest(CommonUtil.java:54)  

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  

    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)  

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  

    at java.lang.reflect.Method.invoke(Method.java:606)  

    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)  

    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)  

    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111)  

    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806)  

    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729)  

    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)  

    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)  

    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)  

    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)  

    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)  

    at javax.servlet.http.HttpServlet.service(HttpServlet.java:751)  

    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)  

    at javax.servlet.http.HttpServlet.service(HttpServlet.java:844)  

    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)  

    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)  

    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)  

    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:346)  

    at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:25)  

    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)  

    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)  

    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)  

    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:79)  

    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3436)  

    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3402)  

    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)  

    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)  

    at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)  

    at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2285)  

    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2201)  

    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2179)  

    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1572)  

    at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:255)  

    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:311)  

    at weblogic.work.ExecuteThread.run(ExecuteThread.java:263)  

>  

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

<span style="font-size:18px;">set JAVA_OPTIONS=%JAVA_OPTIONS% %JAVA_PROPERTIES% -DUseSunHttpHandler=true -Dwlw.iterativeDev=%iterativeDevFlag% -  

 Dwlw.testConsole=%testConsoleFlag% -Dwlw.logErrorsToConsole=% logErrorsToConsoleFlag%</span>  

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

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

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

如下:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: