13

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

この StackOverflow の投稿を読んだ後、次のように認証資格情報をリクエスト コンテキストに追加しようとしました。

@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 サービスに対して認証するにはどうすればよいですか?

4

2 に答える 2

27

JAX-WS (Client) を使用した HTTP 基本認証によると、次のようになります。

2. サービス クラスの作成

サービス オブジェクトのコンストラクターには、WSDL へのアクセスが必要です。また、そのままでは基本認証をサポートしていません。wsdl ファイルをダウンロードしてローカルで使用するオプションがあります。もう 1 つのオプションは、デフォルトのオーセンティケーターを使用することです。

Authenticator.setDefault(new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(
            USER_NAME,
            PASSWORD.toCharArray());
    }
});
于 2010-06-14T12:48:44.050 に答える
5

CXF を使用していないことに注意してください。JDK に組み込まれている JAX-WS 参照実装を使用しています。

CXF を使用すると、Spring 構成をすべてに使用でき、デフォルトのオーセンティケーターを設定する必要はありません。次のページを参照してください。

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

http-conf:authorization の設定について。

于 2010-06-15T13:18:53.337 に答える