Netbeans Basic Http Auth Jax-WS
Asked Answered
X

2

5

how can I access a webservice through a basic http authentification? I am using the netbeans built in webservice client features. But when I try to access the webservice, I get an exception with a 401 auth failed error message.

How can I pass the right username and password?

Thank you!

Xylophone answered 18/12, 2009 at 14:40 Comment(0)
X
4

You could use BindingProvider or WSBindingProvider class to access a Web Service through a basic http authentification. The code is as follows.

XxxService service = new XxxService();
Xxx port = service.getXxxPort();

Map<String, Object> reqContext = ((BindingProvider)port).getRequestContext();
reqContext.put(BindingProvider.USERNAME_PROPERTY, "username");
reqContext.put(BindingProvider.PASSWORD_PROPERTY, "password");
Xenogenesis answered 20/1, 2010 at 21:5 Comment(2)
This will work if the WSDL is not protected himself by Basic Http AuthenticationOfficeholder
why it doesn't work for me? I have exactly that AImpl aImplPort = new AImplService().getAImplPort(); BindingProvider prov = (BindingProvider) aImplPort; prov.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "fred"); prov.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "fred"); String b = aImplPort.b(); System.out.println(b); it isn't sending expected Authorization http headerHeap
D
3

You can also provide your own Authenticator. That way it will work even if the WDSL itself is protected by basic HTTP authentication.

@WebServiceRef(wsdlLocation = "https://laka/sito?wsdl")
static XxxService service;

public static void main(String[] args) {

    Authenticator.setDefault(new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("user", "password".toCharArray());
        }
    });

    service = new XxxService();
    Xxx port = service.getXxxPort();

    // invoke webservice and print response
    XxxResponse resp = port.foo();
    System.out.println(resp.toString());

}
Deceit answered 19/4, 2011 at 10:48 Comment(2)
Didn't know that java contains this feature. Very helpful. Thanks!Breed
why it works for me and the reqContext.put(BindingProvider.USERNAME_PROPERTY, "username"); approach doesn't is it related to my java version?Heap

© 2022 - 2024 — McMap. All rights reserved.