JaxWS: Webservice with Basic Authentication

久未见 提交于 2019-12-13 12:18:59

问题


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:


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.



来源:https://stackoverflow.com/questions/9296636/jaxws-webservice-with-basic-authentication

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