Tag Archives: jax-ws

Secure wsdl client artifacts using wsimport and jax ws client

Q: How to create client artifacts (value objects and other service class methods etc) of secure resource (WSDL) using wsimport?

Solution: Use following command to generate artifacts in given directory. Make sure to put authFile.txt file at current path.

wsimport -keep -d <DirectoryName> -verbose -Xnocompile -Xauthfile authFile.txt <WSDL_URL>

Content of authFile.txt

http://username:password@hostname/path?WSDL

 


 

Q: How to create JAX WS client where WSDL is secure and API is also secure? 

Solution: There will be authentication at two layers.

1) HTTP Layer
2) Method Layer (API)

Here is the sample.

public class JaxWsClientSample {

	// HTTP Authentication
	static {

		java.net.Authenticator.setDefault(new java.net.Authenticator() {

			@Override
			protected java.net.PasswordAuthentication getPasswordAuthentication() {
				return new java.net.PasswordAuthentication("username",
						"password".toCharArray());
			}
		});
	}

	public static void main(String[] args) {

		// Create service object and get port
		// Note: TestBean_Service is service class that can be found in 
// generated client artifacts and name may be different in your case
		TestBean_Service service = new TestBean_Service();
		TestBean port = service.getTestBeanPort();

		//Use BindingProvider API to provide credentials for API security
		BindingProvider prov = (BindingProvider) port;
		Map<String, Object> reqCtx = prov.getRequestContext();

		// API credentials
		reqCtx.put(BindingProvider.USERNAME_PROPERTY, "username");
		reqCtx.put(BindingProvider.PASSWORD_PROPERTY, "password");
		reqCtx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
				"http://hostname/path/TestBean?");

		// Invoke webservice method through port
		Response response = port.doSomething(request);

	}

}

Resources: