Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to access a web service for a project i'm working on. I'm using JAX-WS and the app is deployed on weblogic. When i'm trying to access the WS, i get the following exception:

javax.portlet.PortletException: javax.xml.ws.WebServiceException: Failed to access the WSDL at: http://xxx.xxxx.ro:40000/idm/ws/cup?wsdl. It failed with: 
Response: '401: Unauthorized' for url: 'http://xxx.xxxx.ro:40000/idm/ws/cup?wsdl'.
at com.bea.portlet.container.PortletStub.processAction(PortletStub.java:346)
at com.bea.portlet.container.AppContainer.invokeProcessAction(AppContainer.java:678)
........

I read through a lot of posts regarding issue and i tried different types of auth. I tried to use BindingProvider, basicHTTPAuth in different usecases, disable HostnameVerifier etc. but still with no result.

Below is a snippet of my code, as the last tried version:

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


    ComputeUserProfileImplService computeUserProfileImplService = new ComputeUserProfileImplService(
    new URL(null, endpointURL, new sun.net.www.protocol.http.Handler()),
    new QName("http://xxx.xx.xxxxx.xxxxx.com/",
            "ComputeUserProfileImplService"));
    ComputeUserProfileImpl computeUserProfile = computeUserProfileImplService
    .getComputeUserProfileImplPort();

The ComputeUserProfileImplService code looks like :

private final static URL COMPUTEUSERPROFILEIMPLSERVICE_WSDL_LOCATION;
private final static Logger logger = Logger.getLogger(com.xxxxx.xxxxx.xx.xxxxx.cup.ComputeUserProfileImplService.class.getName());

static {
    URL url = null;
    try {
        URL baseUrl;
        baseUrl = com.xxxxx.xxxxx.xx.xxxxx.xxx.ComputeUserProfileImplService.class.getResource("");
        url = new URL(baseUrl, "http://xxxx.xxxxx.ro:40000/idm/ws/cup?wsdl");

    } catch (MalformedURLException e) {
        logger.warning("Failed to create URL for the wsdl Location: 'xxxxxxxxxxxxxxxx', retrying as a local file");
        logger.warning(e.getMessage());
    }
    COMPUTEUSERPROFILEIMPLSERVICE_WSDL_LOCATION = url;
}

Sorry for replacing the links, but i'm not authorised to post them since it's a pretty known organisation.. If you can help me with some suggestions, i will be grateful. I keep searching for solutions but i'm stuck.. i can't figure it out. It should be a workaround for this weblogic issue that applies to me... but i just can't find it. If you need it, i'll post some other snippets. Hope i was pretty clear with this.

Thanks in advance !!

like image 653
Adrian Zaharia Avatar asked Nov 22 '25 16:11

Adrian Zaharia


2 Answers

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
like image 88
Bas de Roos Avatar answered Nov 25 '25 06:11

Bas de Roos


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


    }
}
like image 27
Dobromir Gospodinov Avatar answered Nov 25 '25 06:11

Dobromir Gospodinov