title: Spring ws Unauthorized [401]
body: <p>I'm trying to consume a soap service. The service has no certificate, but receives as a property of the request username and password.
  but I'm getting the following error:</p>
  
  <pre><code>org.springframework.ws.client.WebServiceTransportException: Unauthorized [401]
  </code></pre>
  
  <p>I tried in several ways to pass the username and password.
  Using the soapui I was able to make the requisition. In soapui I pass the username and password through request properties.</p>
  
  <pre><code> @Bean
      Jaxb2Marshaller jaxb2Marshaller() {
          Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
          jaxb2Marshaller.setPackagesToScan("ce.gov.s2gpr.compras.licita.business.model.service.bean");
          return jaxb2Marshaller;
      }
  
  
      @Bean
      public WebServiceTemplate webServiceTemplate() {
          WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
          webServiceTemplate.setMarshaller(jaxb2Marshaller());
          webServiceTemplate.setUnmarshaller(jaxb2Marshaller());
          webServiceTemplate.setDefaultUri(defaultUri);
  
          HttpsUrlConnectionMessageSender sender = new HttpsUrlConnectionMessageSender();
          sender.setTrustManagers(new TrustManager[] {
                  new X509TrustManager() {
                      public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                          return null;
                      }
                      public void checkClientTrusted(
                              java.security.cert.X509Certificate[] certs, String authType) {
                      }
                      public void checkServerTrusted(
                              java.security.cert.X509Certificate[] certs, String authType) {
                      }
                  }
          });
  
          webServiceTemplate.setMessageSenders(new WebServiceMessageSender[]{sender, httpComponentsMessageSender()});
  
          return webServiceTemplate;
      }
  
  
      @Bean
      public HttpComponentsMessageSender httpComponentsMessageSender() {
          HttpComponentsMessageSender httpComponentsMessageSender = new HttpComponentsMessageSender();
          httpComponentsMessageSender.setConnectionTimeout(timeout);
          httpComponentsMessageSender.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS);
          httpComponentsMessageSender.setReadTimeout(timeout);
          httpComponentsMessageSender.setCredentials(new UsernamePasswordCredentials(userName, userPassword));
  
          return httpComponentsMessageSender;
      }
  </code></pre>
  
  <p>I tried in several ways to pass the username and password, but I received the same error.</p>
  
  <pre><code>@SuppressWarnings("unchecked")
      public AtualizacaoItensGruposResponse testeSoap(PgeTO dto) {
  
      ObjectFactory factory = new ObjectFactory();
  
      AtualizacaoItensGrupos itens = factory.createAtualizacaoItensGrupos();
  
      itens.setArg0(dto);
      JAXBElement&lt;AtualizacaoItensGrupos&gt; request =  factory.createAtualizacaoItensGrupos(itens);
  
      try {
  
          WebServiceMessageCallback wsCallback = message -&gt; {
              TransportContext context = TransportContextHolder.getTransportContext();
              WebServiceConnection connection = context.getConnection();
              HttpUrlConnection conn = (HttpUrlConnection) connection;
  
              conn.getConnection().setRequestProperty("Username", "");
              conn.getConnection().setRequestProperty("Password", "");
              conn.getConnection().setRequestProperty("username", "");
              conn.getConnection().setRequestProperty("password", "");
  
              conn.getConnection().addRequestProperty("Username", "");
              conn.getConnection().addRequestProperty("Password", "");
              conn.getConnection().addRequestProperty("username", "");
              conn.getConnection().addRequestProperty("password", "");
              conn.getConnection().addRequestProperty("s2gpr.integracao.cotacao.item_licitacao.webservice.user", "");
              conn.getConnection().addRequestProperty("s2gpr.integracao.cotacao.item_licitacao.webservice.password", "");
          };
  
          webServiceTemplate.marshalSendAndReceive(request, wsCallback);
          return null;
      } catch (Exception e) {
          log.error(e.getMessage());
          throw new ClientLicitaWebException("Ocorreu um erro ao enviar as informações para o sistema LicitaWeb");
      }
  
  }
  </code></pre>