0

I am trying to call a SOAP endpoint which has set up a basic authentication with username/password from a project with Java 11 / Spring / Gradle. I managed to generate with wsimport using a JDK 8 the classes and found a couple of approaches, but none of them work.

1st approach was from here: https://mkyong.com/webservices/jax-ws/application-authentication-with-jax-ws/ but I get an java.lang.IllegalArgumentException: *Port referenced from a method is not visible from class loader when I try

Service service = Service.create(url, qname);
HelloWorld hello = service.getPort(HelloWorld.class);

I tried both with the Service class and the HelloWorld class being the classes generated by wsimport or being written by me, as it is done in this example: https://www.baeldung.com/spring-soap-web-service#1-generate-client-code

The second approach for setting the auth headers was with

BindingProvider prov = (BindingProvider)tilwsPort;
prov.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "username");
prov.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "pass");

I also tried to get the Interface by calling the generated method for the service, like:

HelloWorld hello = service.getHelloWorldPort();

Any advice which should be the right approach? Thank you!

amnine
  • 13
  • 4

2 Answers2

0

You can try setting the authentication through the HTTP_REQUEST_HEADERS property and not using the user and password directly in the RequestContext. In the first link that you provided you have the example

Map<String, Object> req_ctx = ((BindingProvider)hello).getRequestContext();
    req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WS_URL);

    Map<String, List<String>> headers = new HashMap<String, List<String>>();
    headers.put("Username", Collections.singletonList("mkyong"));
    headers.put("Password", Collections.singletonList("password"));
    req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
Tavo Sanchez
  • 141
  • 6
  • The problem is that my code doesn't get till there, I get the java.lang.IllegalArgumentException: *Port referenced from a method is not visible from class loader when I want to access the interface for the endpoint. Here: HelloWorld hello = service.getHelloWorldPort(); (or in the other approach HelloWorld hello = service.getPort(HelloWorld.class);) – amnine Feb 25 '21 at 10:17
  • So, apparently this was a Spring Devtools issue, which I bypassed with this solution: https://stackoverflow.com/questions/50465170/java-lang-illegalargumentexception-referenced-from-a-method-is-not-visible-from Now I am getting another exception, which is more auth related: com.sun.xml.ws.client.ClientTransportException: HTTP transport error: javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target – amnine Feb 25 '21 at 10:32
  • Right, you have to disable the SSL check on your client – Tavo Sanchez Feb 25 '21 at 10:59
  • 1
    Yes, that was the fix I applied at least locally so I can test the endpoints, as described here: https://stackoverflow.com/a/47050878/8745761 – amnine Feb 25 '21 at 12:00
0

The solution in my case was a complex one:

  1. Had to create a .properties file for Spring Dev-tools (see comments above) so that the generated files are ignored.
  2. Set the authentication using the solution with BindingProvider.
  3. Disable the SSL check for local tests (see comment above).
amnine
  • 13
  • 4