首页 > 解决方案 > 为什么使用基本身份验证通过 WebServiceGatewaySupport 发送两个 SOAP 请求

问题描述

我正在为具有基本身份验证的 SOAP Web 服务编写客户端,并且我使用了 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 代理工具检查实际请求时,我发现这是从客户端发送的两个 post 请求,而不是一个。第一个是没有身份验证标头的请求,然后作为响应,它返回了 401,然后有一个带有基本身份验证的 soap 请求,一切正常。

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

我很想知道发送两个请求的原因以及我是否缺少一些配置。

标签: javaspringweb-servicessoap

解决方案


推荐阅读