« Return to Thread: How to pass authetication to a web service...

Re: How to pass authetication to a web service...

by Jonathan Lister :: Rate this Message:

Reply to Author | View in Thread

a) add a class like this (note this one assumes we are using https and doesn't transmit passwords unless encrypted)

public class BasicHTTPAuthenticator extends Authenticator {

    private String userName;
    private String password;
    Logger log = Logger.getLogger(BasicHTTPAuthenticator.class.getName());
   
    public BasicHTTPAuthenticator(String userName, String password) {
        this.userName = userName;
        this.password = password;
    }

    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        log.fine("getPasswordAuthenticator, protocol is " + this.getRequestingProtocol()
                + " userName is " + userName);
        // only return username / password if using https
        // because we don't want to show them in clear text
        if (this.getRequestingProtocol().equalsIgnoreCase("https")) {
            return new PasswordAuthentication(userName,
                    password.toCharArray());
        } else {
            return null;
        }
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}


b) use the new class before calling the secure web service ..
        String HTTPuserName = "someUser";
        String HTTPpassword = "herPassword";

        Authenticator.setDefault(new BasicHTTPAuthenticator(HTTPuserName, HTTPpassword));

try { // Call Web Service Operation
   com.smed.imaging._240.ItkInterface service = new com.smed.imaging._240.ItkInterface();
   com.smed.imaging._240.ItkInterfaceSoap port = service.getItkInterfaceSoap();
   


krish1315 wrote:
Hello folks,,,

I am accessing a Externally deployed services through "Web Service Client wizard" of NetBeans...
These service require HTTP authetication -- User name and password...

I created the JAX-WS artifacts thrught wizard, Now when i am accessing these services through my Java code...

How should i pass authethication parameters...


following is the code to access the service

try { // Call Web Service Operation
   com.smed.imaging._240.ItkInterface service = new com.smed.imaging._240.ItkInterface();
   com.smed.imaging._240.ItkInterfaceSoap port = service.getItkInterfaceSoap();
   
    // TODO initialize WS operation arguments here
        java.lang.String inputXml = "<ServiceParms><OwnerList><Owner OwnerTypeName='MEDREC'><MedRecMRN>297368</MedRecMRN></Owner></OwnerList></ServiceParms>";
   // TODO process result here
   java.lang.String result = port.ikmSvcListRecords(inputXml);
   System.out.println("Result = "+result);
} catch (Exception ex) {
    ex.printStackTrace();
   // TODO handle custom exceptions here
}




It is throwing following exception

Failed to access the WSDL at: http://blrkf4aa/c9sl/html/IkmSvcInterface.asmx?wsdl. It failed with:
        Server returned HTTP response code: 401 for URL: http://blrkf4aa/c9sl/html/IkmSvcInterface.asmx?wsdl.
        at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.tryWithMex(RuntimeWSDLParser.java:162)
        at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:144)
        at com.sun.xml.ws.client.WSServiceDelegate.parseWSDL(WSServiceDelegate.java:254)
        at com.sun.xml.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:217)
        at com.sun.xml.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:174)
        at com.sun.xml.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:104)
        at javax.xml.ws.Service.<init>(Service.java:57)
        at com.smed.imaging._240.ItkInterface.<init>(ItkInterface.java:48)
        at EDMClient.main(EDMClient.java:20)
Caused by: java.io.IOException: Server returned HTTP response code: 401 for URL: http://blrkf4aa/c9sl/html/IkmSvcInterface.asmx?wsdl
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1245)
        at java.net.URL.openStream(URL.java:1009)
        at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.createReader(RuntimeWSDLParser.java:804)
        at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.resolveWSDL(RuntimeWSDLParser.java:261)
        at com.sun.xml.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:129)
        ... 7 more



I tried with inserting following before invoking service,....

   BindingProvider bp = (BindingProvider)port;
    Map<String,Object> map = bp.getRequestContext();
    map.put(BindingProvider.USERNAME_PROPERTY, "userName");
    map.put(BindingProvider.PASSWORD_PROPERTY,"password");


Still it is throwing the same excpetion....



Thanks,
Krishna

   


 « Return to Thread: How to pass authetication to a web service...

LightInTheBox - Buy quality products at wholesale price