2 Replies Latest reply: Aug 1, 2013 7:00 AM by user10878887 RSS

    JAX-WS setting header HTTP basic authentication

    1025266

      Hello

      How can I set HTTP header vars in JAX-WS?

      I found a lot of questions but no answer works.

      This is my code:

      Service service = Service.create(url, portName);
      OnlineSIUIDef hello = service.getPort(OnlineSIUIDef.class);
      Map<String, Object> contextws = ((BindingProvider)service).getRequestContext();
      contextws.put(BindingProvider.USERNAME_PROPERTY, SIUI_USERNAME);
      contextws.put(BindingProvider.PASSWORD_PROPERTY, SIUI_PASSWORD);
      

       

      All examples I found on the web are based on such a structure.

      Problem is that first is created a new service, then the header vars are changed.

      But this can't work because when the service is created(at line 1) error 401 is already thrown.

       

      What is the equivalent in JAX-WS of the following lines?

      HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
      con.setSSLSocketFactory(factory);
      con.setRequestProperty(varName, val);
      

       

      Can someone help with this problem?

      I'm looking for a solution for over a day and I can't find nothing usefull about this.

      Thank you.

        • 1. Re: JAX-WS setting header HTTP basic authentication
          801188

          Hi

          Seems that the URL you are referring in Service.create() is a basic auth protected URL(wsdlLocation) ie why you are getting 401 error.

          there are different options to resolve this.

          Authenticator.setDefault(new Authenticator() {
           @Override
           protected PasswordAuthentication getPasswordAuthentication() {
             return new PasswordAuthentication(
                SIUI_USERNAME,
                SIUI_PASSWORD.toCharArray());
           }
          });
          

          try to add the above code ,before creating the URL.

          2 Download the WSDL from Endpoint and refer that WSDL locally,and dynamically hook the correct endpoint using

          contextws.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,"ur Endpoint");
          

          Haven't tried this...

          custom SSLSocketFactory come in picture only if you have protected your webservice using one way/2 way SSL.

           

           

          Thanks,

          Paul

          • 2. Re: JAX-WS setting header HTTP basic authentication
            user10878887

            The service is using 2 way SSL.

            I tried to use the JAX with 2 ways handshake and basic authentication but in the end I used the HTTP protocol.

            HTTP implementation in JAVA is very easy to use with 2 way SSL and basic authentication as opposed to JAX(complicated when you need such some customizations).

            In order to test the service with JAX I need a solution for both problems.

             

            Thank you.