Reputation: 47
I have a simple WSDL for a webservice. With wsimport I generated the java wrappers. And I have implemented the generated interface in a class "MyService". Then I publish this webservice with the built-in features of Java6:
MyService myService = new MyService(); Endpoint endpoint = Endpoint.publish("http://localhost:80/servicetest/services/MyServiceINSoap", myService);
When I let this code run, everything works perfect - I am able to connect to the service with a soapclient and can execute the methods.
Now I want the built-in webserver of the javavm to use basic authentication - so the client has to provide an userid and a password. How do I accomplish this? Is this possible? Or do I need an application server (tomcat, jboss, whatever...)?
I have searched for some time but only found solutions for webservice which are hosted in an application server.
Many thanks in advance!
erwrock
Upvotes: 1
Views: 2458
Reputation: 101
As I found your answer I was quite relieved because I searched hours for a solution. But as I tried to follow your link I noticed that the page is not available any longer. Fortunately, I found the old version with the help of the Wayback Machine (https://archive.org/web/). Although, this article is great I had some problems with getting the basic auth to work at the client side. In the end I got it working with the help of java.net.Authenticator. Here are the code snippets which I hope to be useful for others.
Server:
javax.xml.ws.Endpoint serverEndpoint = Endpoint.create(new MyService());
com.sun.net.httpserver.HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
com.sun.net.httpserver.HttpContext httpContext = server.createContext("/myservice");
server.start();
AuthenticatorServer authenticatorServer = new AuthenticatorServer("myRealm", "myName", "myPwd");
httpContext.setAuthenticator(authenticatorServer);
serverEndpoint.publish(httpContext);
Where AuthenticatorServer is:
private static class AuthenticatorServer extends com.sun.net.httpserver.BasicAuthenticator {
private final String user;
private final String pwd;
public AuthenticatorServer(String realm, String user, String pwd) {
super(realm);
this.user = user;
this.pwd = pwd;
}
@Override
public boolean checkCredentials(String userNameInput, String passwordInput) {
return StringUtils.equals(user, userNameInput) && StringUtils.equals(pwd, passwordInput);
}
}
And on the Client side:
AuthenticatorClient authenticatorClient = new AuthenticatorClient("myName", "myPwd");
Authenticator.setDefault(authenticatorClient);
javax.xml.namespace.QName qName = new QName(namespaceURI, WS_SERVICE_NAME);
java.net.URL wsdlAddress = new URL(namespaceURI + WS_SERVICE_NAME + "?wsdl");
MyService service =(Service.create(wsdlAddress, qName)).getPort(MyService.class);
Where AuthenticatorClient is:
private static class AuthenticatorClient extends java.net.Authenticator {
private final String user;
private final String pwd;
public AuthenticatorClient(String user, String pwd) {
this.user = user;
this.pwd = pwd;
}
@Override
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication(user, pwd == null ? null : pwd.toCharArray()));
}
}
Upvotes: 1
Reputation: 47
Found a solution here:
https://today.java.net/pub/a/today/2007/07/03/jax-ws-web-services-without-ee-containers.html
after hours of trial-and-error finally the right searchwords in google :)
Upvotes: 0