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

发布于 2024-09-05 18:19:03 字数 4185 浏览 3 评论 0原文

我正在尝试在 JUnit 测试中使用使用 HTTP 基本身份验证(使用 Apache CXF)的远程 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

阅读

@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 代码之前,所以错误保持不变。

我确实读过这篇文章及其后续,但到目前为止,我无法确定如何在不使用 Spring 的情况下添加拦截器代码(这是用于 JUnit 测试)。

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

I'm trying to consume a remote Web service that uses HTTP basic authentication, using Apache CXF, within a JUnit test.

The error I am getting is:

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

Having read this StackOverflow post, I have attempted to add the auth credentials to my request context, as follows:

@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);
}

However, as my in-line comment indicates, the error is occurring before the BindingProvider code is reached, so the error remains the same.

I did have a read of this article and its follow-up, but so far I've had trouble determining how to go about adding the interceptor code without the use of Spring (this is for a JUnit test).

How might I go about authenticating against this Web service?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘然心甜 2024-09-12 18:19:03

根据 使用 JAX-WS 的 HTTP 基本身份验证(客户端)

2。服务类创建

服务对象的构造函数
需要访问 WSDL。又来了
它不支持基本的
开箱即用的身份验证。你
可以选择下载 wsdl
文件并在本地使用。其他
选项是使用默认值
验证器:

Authenticator.setDefault(new Authenticator() {
    @覆盖
    受保护的密码验证 getPasswordAuthentication() {
        返回新的密码验证(
            USER_NAME,
            PASSWORD.toCharArray());
    }
});

According to HTTP basic authentication with JAX-WS (Client):

2. The service class creation

A constructor of the service object
requires access to the WSDL. And again
it does not support basic
authentication out of the box. You
have an option to download the wsdl
file and use it locally. Another
option is to use the default
authenticator:

Authenticator.setDefault(new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(
            USER_NAME,
            PASSWORD.toCharArray());
    }
});
我还不会笑 2024-09-12 18:19:03

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

使用 CXF,您可以使用 Spring 配置进行所有操作,而无需设置默认的身份验证器。请参阅以下页面:

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

有关 http-conf:authorization 设置的信息。

Just a point of note, you aren't using CXF. You are using the JAX-WS reference implementation built into the JDK.

With CXF, you CAN use Spring config for everything and not set the default Authenticator. See the page at:

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

about the http-conf:authorization settings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文