【发布时间】:2014-03-14 14:35:22
【问题描述】:
我正在尝试在标准 JAVA 7 JAX WS 工具的帮助下使用安全(HTTPS 模式)Web 服务。 此网络服务需要身份验证。
我已经成功地将证书添加到我的本地 java 密钥库。
我在 wsimport 工具的帮助下从 WSDL 文件生成了所有需要的类。
现在我正在尝试使用以下调用来运行一个简单的测试:
public class ReportingWebServiceTest {
static ReportingServiceService service;
static ReportingService port;
@BeforeClass
public static void setUpBeforeClass(){
service = new ReportingServiceService();
port = service.getReportingServicePort();
Map<String, Object> rContext = ((BindingProvider) port).getRequestContext();
Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("Authorization", Collections.singletonList("Basic YWRtaW5AYWRhcHRsb2dpYy5jb206MTIxMjE****="));
// headers.put("Username", Collections.singletonList("*****@******.com"));
// headers.put("Password", Collections.singletonList("********"));
rContext.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
// rContext.put(BindingProvider.USERNAME_PROPERTY, "*****@******.com");
// rContext.put(BindingProvider.PASSWORD_PROPERTY, "********");
}
@Test
public void test() {
WEBCAMPAIGNROW row = port.getCampaignRowById(14081);
toConsole(row.toString());
}
protected static void toConsole(String msg) {
System.out.println(msg);
}
}
当我运行测试时,它给了我以下异常:
javax.xml.ws.WebServiceException: Failed to access the WSDL at: https://reporting-stage.admp.mtv3.adtlgc.com/admp/ReportingService?wsdl. It failed with:
Got Server returned HTTP response code: 401 for URL: https://reporting-stage.admp.mtv3.adtlgc.com/admp/ReportingService?wsdl while opening stream from https://reporting-stage.admp.mtv3.adtlgc.com/admp/ReportingService?wsdl.
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.tryWithMex(RuntimeWSDLParser.java:173)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:155)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:120)
at com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(WSServiceDelegate.java:257)
at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:220)
at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:168)
at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:96)
at javax.xml.ws.Service.<init>(Service.java:77)
at com.enreach.admp.reporting.ReportingServiceService.<init>(ReportingServiceService.java:42)
at me.enreach.automation.mtv3.ReportingWebServiceTest.setUpBeforeClass(ReportingWebServiceTest.java:26)
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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.io.IOException: Got Server returned HTTP response code: 401 for URL: https://reporting-stage.admp.mtv3.adtlgc.com/admp/ReportingService?wsdl while opening stream from https://reporting-stage.admp.mtv3.adtlgc.com/admp/ReportingService?wsdl
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.createReader(RuntimeWSDLParser.java:842)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.resolveWSDL(RuntimeWSDLParser.java:283)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:140)
... 23 more
Caused by: java.io.IOException: Server returned HTTP response code: 401 for URL: https://reporting-stage.admp.mtv3.adtlgc.com/admp/ReportingService?wsdl
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1626)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at java.net.URL.openStream(URL.java:1037)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.createReader(RuntimeWSDLParser.java:827)
... 25 more
如您所见,我尝试应用三种不同的身份验证技术,但没有任何运气。例外在所有三种情况下都是相同的。 我做错了什么?
附:如果我尝试在浏览器中访问 WSDL,我在代码中使用的凭据可以正常工作。
【问题讨论】:
-
我也尝试过 Authenticator.setDefault(.......) 方法,它也对我没有帮助。
-
你知道需要什么样的认证吗?
-
@user1902288 不,我不确定需要哪种身份验证。但正如你所看到的,我已经尝试使用我目前找到的所有类型。
-
你能试试这两个建议中的任何一个吗?
-
认证必须是HTTP Basic认证。
标签: java web-services authentication jax-ws webservice-client