Я кодирую клиента для веб-службы SOAP с базовой аутентификацией, и я использовал Spring WebServiceGatewaySupport. Все работало нормально, и я получил ответ, как и ожидалось.
Ниже приведены соответствующие классы.
Классы конфигурации
@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());
}
}
Класс вызывающего клиента
@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;
}
}
Зависимости в 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>
Однако, когда я проверял фактический запрос с помощью http-прокси-инструмента, я обнаружил, что это было два почтовых запроса, которые отправлялись изклиент а не один. Первым был запрос без заголовка аутентификации, а затем в ответ он получал обратно 401, а затем был запрос мыла с базовой аутентификацией, и все работало нормально.
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
Мне любопытно узнать опричина, по которой два запроса были отправлены, и если мне не хватает некоторых конфигураций.