How to create SOAP authentication service using Java

Creating SOAP authentication service using Java

In this example, We will show you how to create SOAP authentication service using Java programming language. This example has been tested and shared in the same post.

Interface (AuthenticationService.java)

package com.dineshkrish.soap;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

@WebService
@SOAPBinding(style = Style.RPC)
public interface AuthenticationService {
	
	public boolean authenticate(String userName, String password);

}

Implementation (AuthenticationServiceImpl.java)

package com.dineshkrish.soap;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

@WebService(endpointInterface = "com.dineshkrish.soap.AuthenticationService")
@SOAPBinding(style = Style.RPC)
public class AuthenticationServiceImpl implements AuthenticationService {

	@Override
	public boolean authenticate(String userName, String password) {
		
		
		if(userName != null && password != null) {
			
			if(userName.equals("admin") && password.equals("password")) {
				
				return true;
			}
			
		}
		
		return false;
	}

}

Service Publisher (Publisher.java)

package com.dineshkrish.soap;

import javax.xml.ws.Endpoint;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class Publisher {

	public static void main(String[] args) {
		
		final String serviceURL = "http://localhost:8080/auth";
		
		Endpoint endpoint = Endpoint.publish(serviceURL, new AuthenticationServiceImpl());
		
		if(endpoint.isPublished()) {
			
			System.out.println("Authentication service is running at "+serviceURL);
		} else {
			
			System.out.println("Service Error");
		}
		
	}
	
}

Publisher Output

Authentication service is running at http://localhost:8080/auth

How to create SOAP authentication service using Java

Client Implementation (LoginClient.java)

package com.dineshkrish.soap.client;

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import com.dineshkrish.soap.AuthenticationService;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class LoginClient {

	public static void main(String[] args) {

		final String SERVICE_ADDRESS = "http://localhost:8080/auth?WSDL";
		final String NAMESPACE = "http://soap.dineshkrish.com/";
		final String SERVICE_NAME = "AuthenticationServiceImplService";
		
		String userName = "admin";
		String password = "password";

		try {

			URL url = new URL(SERVICE_ADDRESS);

			QName qName = new QName(NAMESPACE, SERVICE_NAME);

			Service service = Service.create(url, qName);

			AuthenticationService authService = service.getPort(AuthenticationService.class);

			boolean isValidUser = authService.authenticate(userName, password);
			
			if(isValidUser)  {
				
				System.out.println("You are logged in successfully...");
			} else {
				
				System.out.println("You are not a authorized user!");
			}

			
		} catch (Exception ex) {

			System.out.println(ex.getMessage());
		}

	}

}

Client Output

You are logged in successfully...

Download Code

You can download the code here

Hello, folks, I am a founder of dineshkrish.com. I love Java and Open source technologies, If you find my tutorials are useful, please consider making donations to these charities.