javax.xml.ws.WebServiceException: Failed to access the WSDL. Response: '401: Unauthorized'

大憨熊 提交于 2019-12-06 13:17:00

Unfortunately Weblogic has a different approach, it uses its own URLStreamHandler that for some reason ignores authentication.

Try to use Sun's implementation:

instead of

 url = new URL(address); // use WebLogic handler

use

 url = new URL(null, address, new sun.net.www.protocol.http.Handler()); // use Sun's handler
Dobromir Gospodinov

I had the same issue in Weblogic. I can confirm for you that the solution suggested by Paulius Matulionis worked for me in Weblogic 11:

import my.MyPortType;

import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import javax.xml.ws.handler.MessageContext;
import java.net.Authenticator;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyWebServiceClient {

    public static void main(String[] args) {
        URL url = null;
        try {
            url = new URL("http://host:10001/mycontext/ws?WSDL");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        try {
            final String username = "some";
            final String password = "other";
            Authenticator.setDefault(new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(
                            username,
                            password.toCharArray());
                }
            });
            QName qname = new QName("http://whatevertheqname/", "whatevertheservicename");
            Service service = Service.create(url, qname);
            MyPortType proxy = service.getPort(MyPortType.class);
            Map<String, Object> requestContext = ((BindingProvider) proxy).getRequestContext();
            requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url.toString());
            requestContext.put(BindingProvider.USERNAME_PROPERTY, username);
            requestContext.put(BindingProvider.PASSWORD_PROPERTY, password);
            Map<String, List<String>> headers = new HashMap<String, List<String>>();
            headers.put("Timeout", Collections.singletonList("10000"));
            requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
            String result = proxy.myMethod23");

            System.out.println("Result is: " + result);

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Error occurred in operations web service client initialization", e);
        }


    }
}

I did no try this in WebLogic, but on Tomcat, Glassfish, Apache Karaf, the cliet call to web service which needs the basic authentication this works just great:

/**
 * Proxy.
 */
private OperationsService proxy;

/**
 * Operations wrapper constructor.
 *
 * @throws SystemException if error occurs
 */
public OperationsWrapper()
        throws SystemException {
    try {
        final String username = getBundle().getString("wswrappers.operations.username");
        final String password = getBundle().getString("wswrappers.operations.password");
        Authenticator.setDefault(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(
                        username,
                        password.toCharArray());
            }
        });
        URL url = new URL(getBundle().getString("wswrappers.operations.url"));
        QName qname = new QName("http://hltech.lt/ws/operations", "Operations");
        Service service = Service.create(url, qname);
        proxy = service.getPort(OperationsService.class);
        Map<String, Object> requestContext = ((BindingProvider) proxy).getRequestContext();
        requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url.toString());
        requestContext.put(BindingProvider.USERNAME_PROPERTY, username);
        requestContext.put(BindingProvider.PASSWORD_PROPERTY, password);
        Map<String, List<String>> headers = new HashMap<String, List<String>>();
        headers.put("Timeout", Collections.singletonList(getBundle().getString("wswrappers.operations.timeout")));
        requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
    } catch (Exception e) {
        LOGGER.error("Error occurred in operations web service client initialization", e);
        throw new SystemException("Error occurred in operations web service client initialization", e);
    }
}

I do not think that WebLogic has something different. This should work.

Dheeraj Pure

The following are suggestions that could solve your issue:

1) Change in the WEBLOGIC script setDomainEnv.sh to force WEBLOGIC to use SunHttpHandler.

or

2) Call the webservice from the command prompt using a shell script/batch file.

or

3) Try this url object in your webservice new URL(null, "http://...", new sun.net.www.protocol.http.Handler());

or

4) Write your own sun http handler.

or

5) Try updating your weblogic-webservice.xml by adding your newly created webservice in it.

or

6) Try using jax-rpc instead of jax-ws.

Hopefully any of the first two will resolve your issue, however the other options are also helpful.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!