JaxWS: Webservice with Basic Authentication

2019-04-13 02:42发布

I'm building a client for a SOAP webservice. I auto-generated most of the client code with IntelliJ IDEA, by telling it to build a JaxWS webservice client from a WSDL.

The webservice runs on different URLs (test, integration, production), so I need to be able to configure the service URL in my client. My code looks like this:

String urlString = props.getProperty(URL);
service = new RequestMultiTransportService(new URL(urlString),
              new QName("http://some.uri.com/",
                        "RequestMultiTransportService"));
Boolean useBasicAuth = Boolean.parseBoolean(props.getProperty(BASICAUTH));
RequestMultiTransport rmt = service.getRequestMultiTransportPort();
if (useBasicAuth) {
    String user = props.getProperty(AUTHUSER);
    String pw   = props.getProperty(AUTHPW);
    Map requestContext = ((BindingProvider)rmt).getRequestContext();
    requestContext.put(BindingProvider.USERNAME_PROPERTY, user);
    requestContext.put(BindingProvider.PASSWORD_PROPERTY, pw);
}
ProvisioningResponse response = rmt.send("some", "params", "...");

As you can see, the service might need basic authentication. And here's the problem: While I can configure basic authentication for the actual request, I cannot configure it for loading the WSDL file (which happens in the constructor of RequestMultiTransportService). RequestMultiTransportService is autogenerated by IDEA, and its constructor just calls its super constructor, being the one of javax.xml.ws.Service.

So whereever the webservice requires basic authentication, my code fails, because it does not provide a user / password for fetching the WSDL file located at urlString. A possible workaround I thought of is to store the WSDL file locally and point to it with a file:// URL. But this does not fulfill my requirements, because the service location defined in the WSDL file varies, and I don't seem to be able to change the service URL in the service object which has been loaded from the WSDL file.

Has anyone a solution for getting the WSDL file with basic authentication?

1条回答
forever°为你锁心
2楼-- · 2019-04-13 03:10

Create a new constructor to your WebServiceClient and pass the username, the password and the endpoint location (that varies) as parameter to it. After this put

requestContext.put(BindingProvider.USERNAME_PROPERTY, user);
requestContext.put(BindingProvider.PASSWORD_PROPERTY, pw);
requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointUrl);

into the requestContext and make sure you will not access the defaultConstructor.

查看更多
登录 后发表回答