Thursday, 2 May 2013

Application authentication via JAX-WS

Application authentication via JAX-WS


To handle the authentication at application level, follows the following steps
Step 1 : Create a test hello application
At webservice server side
*****************************************************************
package com.test.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService
@SOAPBinding(style = Style.RPC)
public interface TestHello {
@WebMethod
public String sayHello();

}

********************************************************************
package com.test.ws;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.jws.WebService;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
@WebService(endpointInterface = "com.test.ws.TestHello")
public class TestHelloImpl implements TestHello{
@Resource
WebServiceContext wsctx;
@Override
public String sayHello() {
MessageContext mctx = wsctx.getMessageContext();
//get detail from request headers
Map http_headers = (Map) mctx.get(MessageContext.HTTP_REQUEST_HEADERS);
List userList = (List) http_headers.get("username");
List passList = (List) http_headers.get("password");

String username = "";
String password = "";

if(userList!=null){
//get username
username = userList.get(0).toString();
}
if(passList!=null){
//get password
password = passList.get(0).toString();
}
//Should validate username and password
if (username.equals("wakil") && password.equals("ahmad")){
return "Hello - Congrates valid user";
}else{
return "Invalid user";
}

}
}

Step 2 : EndPoint Publisher
Create an endpoint publisher to deploy web service at URL : “http://localhost:8090/ws/test”
********************************************************************************
package com.test.endpoint;
import javax.xml.ws.Endpoint;
import com.test.ws.TestHelloImpl;
public class TestPublisher{
public static void main(String[] args) {
Endpoint.publish("http://localhost:8090/ws/test", new TestHelloImpl());
}
}
******************************************************************************
Step 3 : Web Service Client

package com.test.client;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;
import javax.xml.ws.handler.MessageContext;
import com.test.ws.TestHello;
public class TestClient {
private static final String WS_URL = "http://localhost:8090/ws/test?wsdl";
public static void main(String[] args) throws Exception {
URL url = new URL(WS_URL);
QName qname = new QName("http://ws.test.com/", "TestHelloImplService");
Service service = Service.create(url, qname);
TestHello test = service.getPort(TestHello.class);
/** UserName & Password */
Map req_ctx = ((BindingProvider)test).getRequestContext();
req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WS_URL);
Map> headers = new HashMap>();
headers.put("username", Collections.singletonList("wakil"));
headers.put("password", Collections.singletonList("ahmad"));
req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
System.out.println(test.sayHello());
}
}

When you run the above client, you can find the following output as below:
Output
Hello - Congrates valid user

Tracing soap traffic
How soap envelope flows between client and server
1:Client send request, the username “wakil” and password “ahmad” are included in the soap envelope.

2. Server send back a response as.

No comments:

Post a Comment