2

I am coding a client for a SOAP webservice with basic authentication,and I used the Spring WebServiceGatewaySupport. Everything worked fine and I did get a response back as expected.

Below are the respective classes.

The Configuration Classes

@ConfigurationProperties(prefix = "ws.api")
@Data
public class ApiConfiguration  {    

    @NonNull
    private String endPointAddress;  

    @NonNull
    private String userName;    

    @NonNull
    private String password;    

}


@Configuration
@AllArgsConstructor
public class ApiBeanConfiguration {

    private final ApiConfiguration config;

    @Bean
    public Jaxb2Marshaller marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("com.model");
        return marshaller;
    }

    @Bean
    public ClientTransport soapConnector(Jaxb2Marshaller marshaller) {
        ClientTransportImpl client = new ClientTransportImpl(config);
        client.setDefaultUri(config.getEndPointAddress());
        client.setMarshaller(marshaller);
        client.setUnmarshaller(marshaller);
        client.setMessageSender(httpComponentsMessageSender());
        return client;
    }

    @Bean
    public HttpComponentsMessageSender httpComponentsMessageSender() {
      HttpComponentsMessageSender httpComponentsMessageSender = new HttpComponentsMessageSender();
      httpComponentsMessageSender.setCredentials(usernamePasswordCredentials());
      return httpComponentsMessageSender;
    }

    @Bean
    public UsernamePasswordCredentials usernamePasswordCredentials() {
      return new UsernamePasswordCredentials(config.getUserName(), config.getPassword());
    }

}

The Calling Client Class

@AllArgsConstructor
public class ClientTransportImpl  extends WebServiceGatewaySupport implements ClientTransport { 

    private final ApiConfiguration config;

    @SuppressWarnings("unchecked")
    @Override
    public SemanticResponse exchangeWithEndpoint(@NonNull Request request) {
        JAXBElement<GetQuoteRequest> requestElement =
                new JAXBElement<Request>( new QName(config.getSoapAction()
                        ,config.getSoapMethod()),
                        Request.class,
                        request);
        JAXBElement<GetQuoteResponse> response =  (JAXBElement<Response>) 
                                    getWebServiceTemplate()
                                    .marshalSendAndReceive(requestElement);     

        return response;
    }


}

The dependencies in pom

         <parent>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-parent</artifactId>
             <version>2.1.4.RELEASE</version>
         </parent>
        ...
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
        <dependency>
          <groupId>org.apache.httpcomponents</groupId>
          <artifactId>httpclient</artifactId>
        </dependency>

However on intospection with http proxy tool to observe the actual request , I found that it was two post request that was getting sent from the client and not one. The first was a request without the authentication header and then in response it was getting back a 401 , and then there was a soap request with basic authentication and everything worked fine .

127.0.0.1:63399: POST https://cloud.com/ws/connectors.soap:SubmitService
              << 401 [ISS.0088.9164] Access to WSDescriptor connectors.soap:SubmitService denied. 375b
127.0.0.1:63399: clientdisconnect
127.0.0.1:63477: clientconnect
127.0.0.1:63477: POST https://cloud.com/ws/connectors.soap:SubmitService
              << 200 OK 306b

I am curious to know about the reason why two request got sent and if I am missing some configurations .

Joydeep
  • 401
  • 2
  • 6
  • 15
  • 1
    That is how basic authentication works. You can enable pre-emptive authentication so that the header will be send with each request, instead of first waiting for a 401. See https://stackoverflow.com/questions/24609751/webservicetemplate-with-basic-auth-using-httpcomponentsmessagesender – M. Deinum Oct 07 '19 at 05:59

0 Answers0