2009-08-25 104 views
11

我有一个需要访问Web服务的Java应用程序(不是小程序)。 Web服务的代理已经使用JAX-WS生成,似乎工作正常。在一种情况下,它需要通过Web代理服务器(实际上是Squid 3.0)进行通信,该服务器需要NTLM身份验证。Java 6 NTLM代理身份验证和HTTPS - 任何人都可以使用它?

运行在Sun的JRE 1.6.0_14,一切工作正常访问HTTP的URL,而无需任何修改:内置的NTLM认证踢,这一切工作的无缝。但是,如果Web服务URL是HTTPS URL,Web服务调用Sun的代码深处失败:

com.sun.xml.internal.ws.client.ClientTransportException: HTTP transport error: java.lang.NullPointerException 
     at com.sun.xml.internal.ws.transport.http.client.HttpClientTransport.getOutput(HttpClientTransport.java:121) 
     at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.process(HttpTransportPipe.java:142) 
     at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.processRequest(HttpTransportPipe.java:83) 
     at com.sun.xml.internal.ws.transport.DeferredTransportPipe.processRequest(DeferredTransportPipe.java:105) 
     at com.sun.xml.internal.ws.api.pipe.Fiber.__doRun(Fiber.java:587) 
     at com.sun.xml.internal.ws.api.pipe.Fiber._doRun(Fiber.java:546) 
     at com.sun.xml.internal.ws.api.pipe.Fiber.doRun(Fiber.java:531) 
     at com.sun.xml.internal.ws.api.pipe.Fiber.runSync(Fiber.java:428) 
     at com.sun.xml.internal.ws.client.Stub.process(Stub.java:211) 
     at com.sun.xml.internal.ws.client.sei.SEIStub.doProcess(SEIStub.java:124) 
     at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:98) 
     at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:78) 
     at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:107) 
     ... our web service call ... 
Caused by: java.lang.NullPointerException 
     at sun.net.www.protocol.http.NTLMAuthentication.setHeaders(NTLMAuthentication.java:175) 
     at sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:1487) 
     at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:164) 
     at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:896) 
     at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:230) 
     at com.sun.xml.internal.ws.transport.http.client.HttpClientTransport.getOutput(HttpClientTransport.java:109) 
     ... 16 more 

展望Sun的bug数据库变成了在这样的类有一些例外,但他们似乎已经修复。有没有人遇到过这样的事情?有没有人得到这个工作?

+0

请提供您的进口呢? – itro 2012-01-24 13:02:54

回答

4

你是娶了JAX-WS?我使用Apache Axis2,它使用公共httpclient并具有内置的NTLM身份验证。

实施例:

//Configure SOAP HTTP client to authenticate to server using NTLM 
HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator(); 

//TODO make report server credentials configurable 
auth.setUsername("jdoe"); 
auth.setPassword("strongpass"); 
auth.setDomain("WINDOWSDOMAIN"); 
auth.setHost("host.mydomain.com"); 
auth.setPort(443); 

Options o = new Options(); 
o.setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE,auth); 
myWebServiceStub._getServiceClient().setOptions(o); 
+2

这个项目对我来说太近了一点,因为我很乐意将JAX-WS取代并用一些东西代替它,但是谢谢你的建议。 – DavidK 2009-08-26 07:37:30

10

一些调试之后,这似乎是在JRE类库中的缺陷,特别是在sun.net.www.protocol.http.HttpURLConnection

在HTTP和HTTPS端点的情况下研究HTTP请求和响应表明,在成功的HTTP情况下,请求的标头为Proxy-Connection=keep-alive,这在失败的HTTPS情况下缺失。更一般地阅读,对于是应该使用“代理连接”还是仅使用“连接”,似乎存在一些混淆...

无论如何,值得注意的是,在HTTP情况下,代码经过HttpURLConnection.writeRequests() ,其中包含下面的代码片段

/* 
    * For HTTP/1.1 the default behavior is to keep connections alive. 
    * However, we may be talking to a 1.0 server so we should set 
    * keep-alive just in case, except if we have encountered an error 
    * or if keep alive is disabled via a system property 
    */ 

    // Try keep-alive only on first attempt 
    if (!failedOnce && http.getHttpKeepAliveSet()) { 
    if (http.usingProxy) { 
     requests.setIfNotSet("Proxy-Connection", "keep-alive"); 
    } else { 
     requests.setIfNotSet("Connection", "keep-alive"); 
    } 

创建通过代理服务器进行HTTPS,从而导致squid NTLM身份验证对话期间生气的隧道时,有没有这样的代码。

要解决这个问题,在HttpURLConnection.sendCONNECTRequest(),我只是

setPreemptiveProxyAuthentication(requests); 
http.writeRequests(requests, null); 

我注入前加入

if (http.getHttpKeepAliveSet()) { 
    if (http.usingProxy) { 
     requests.setIfNotSet("Proxy-Connection", "keep-alive"); 
    } 
} 

我修改HttpURLConnection.class到JRE使用 “-Xbootclasspath/P” 标志,而现在有用!不完全优雅,但我们是。

+1

我遇到同样的问题。我发现了一个JDK错误恰好说明了同样的问题:http://bugs.sun.com/view_bug.do?bug_id=6206466 奇怪的是,它被标记为重复/固定的,虽然其他错误是完全不同的。 – sereda 2010-01-16 11:11:28

+2

是的,有时我不确定Sun的错误数据库是否让我想笑或哭。可能他们太忙担心,如果拉里·埃里森是要解雇他们或不检查错误是否真正复制... – DavidK 2010-01-21 21:58:21

+1

BigZig请你过你提供进口? – itro 2012-01-24 13:06:42