使用CXF使用HTTP基本身份验证使用Web服务时出现401错误

时间:2010-06-14 12:36:34

标签: java web-services client-side cxf basic-authentication

我正在尝试使用在JUnit测试中使用Apache CXF的HTTP基本身份验证的远程Web服务。

我得到的错误是:

javax.xml.ws.WebServiceException: Failed to access the WSDL at: http://localhost:8080/services/MyService?wsdl. It failed with: 
    Server returned HTTP response code: 401 for URL: http://localhost:8080/services/MyService?wsdl.
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.tryWithMex(RuntimeWSDLParser.java:151)
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:133)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(WSServiceDelegate.java:254)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:217)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:165)
    at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:93)
    at javax.xml.ws.Service.<init>(Service.java:76)
    at com.wave2.marketplace.importer.impl.adportal.ws.MyServiceService.<init>(MyServiceService.java:37)
    at com.wave2.marketplace.importer.impl.adportal.MyWSTest.testConsumingTheWS(MyWSTest.java:22)
    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:616)
    at junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:232)
    at junit.framework.TestSuite.run(TestSuite.java:227)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
    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: Server returned HTTP response code: 401 for URL: http://localhost:8080/services/MyService?wsdl
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1269)
    at java.net.URL.openStream(URL.java:1029)
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.createReader(RuntimeWSDLParser.java:793)
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.resolveWSDL(RuntimeWSDLParser.java:251)
    at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:118)
    ... 26 more

阅读this StackOverflow post后,我尝试将身份验证凭据添加到我的请求上下文中,如下所示:

@Test
public void testConsumingTheWS() throws Exception {
    URL wsdl = new URL("http://localhost:8080/services/MyService?wsdl");

    MyServiceService provider = new MyServiceService(wsdl); // <-- Error occurs here
    MyService service = provider.getMyService();

    BindingProvider binding = (BindingProvider)service;
    binding.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "username");
    binding.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "password");

    Ping out = service.getPing();
    assertNotNull(out);
}

但是,正如我的内联注释所示,错误发生在到达BindingProvider代码之前,因此错误保持不变。

我确实读过这个article及其follow-up,但到目前为止,我已经无法确定如何在不使用Spring的情况下添加拦截器代码(这是为了JUnit测试)。

我该如何针对此Web服务进行身份验证?

2 个答案:

答案 0 :(得分:25)

根据HTTP basic authentication with JAX-WS (Client)

  

2。服务类创建

     

服务对象的构造函数   需要访问WSDL。然后再次   它不支持基本的   开箱即用的身份验证。您   有一个下载wsdl的选项   文件并在本地使用它。另一个   选项是使用默认值   认证器:

Authenticator.setDefault(new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(
            USER_NAME,
            PASSWORD.toCharArray());
    }
});

答案 1 :(得分:5)

请注意,您没有使用CXF。您正在使用JDK中内置的JAX-WS参考实现。

使用CXF,您可以将Spring配置用于所有内容,而不是设置默认的Authenticator。请参阅以下页面:

http://cxf.apache.org/docs/client-http-transport-including-ssl-support.html

关于http-conf:授权设置。