This article will help to create WS -Security Client based CXF. It works for Plain password and Password Digest. User have to follow few steps to get one Jar created for the WS Client
Step 1: Create Java project and convert the project to maven based project. Copy your WSDL into the wsdl folder present in the project. provide wsdl path(even URL) in the cxf-codegen-plugin
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${basedir}/src/</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/wsdl/CalculatorImpl.wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
Write the client code like below and pass your callback and username for the WS Security
package com.elan.calc.service.impl;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import javax.xml.ws.BindingProvider;
import com.elan.calc.domain.Input;
public class WSClient {
public static void main(String[] args) throws MalformedURLException {
URL wsdlURL= new URL("http://localhost:8080/Calculator/services/calculator?wsdl");
CalculatorService service = new CalculatorService(wsdlURL);
Calculator port = service.getCalculator();
Map ctx = ((BindingProvider)port).getRequestContext();
ctx.put("ws-security.username", "wsuser");
ctx.put("ws-security.callback-handler", PasswordCallbackHandler.class.getName());
Input input = new Input();
input.setInputA(10);
input.setInputA(20);
int ret = port.add(input);
System.out.println(ret);
}
}
Callback Example:
package com.elan.calc.service.impl;
import java.io.IOException;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import org.apache.ws.security.WSPasswordCallback;
public class PasswordCallbackHandler implements CallbackHandler {
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
System.out.println("identifier: " + pc.getIdentifier());
if (pc.getIdentifier().equals("wsuser")) {
// set the password on the callback. This will later be compared to // the // password which was sent from the client.
pc.setPassword("ws=USER");
}
}
}
Example WS-security soap envelope Plain password
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:impl="http://impl.service.calc.elan.com"
xmlns:dom="http://domain.calc.elan.com">
<soapenv:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/
wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>wsuser</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/
oasis-200401-wss-username-token-profile-1.0
#PasswordText">ws=USER</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<impl:add>
<impl:input>
<dom:inputA>5</dom:inputA>
<dom:inputB>5</dom:inputB>
</impl:input>
</impl:add>
</soapenv:Body>
</soapenv:Envelope>
Example WS-security soap envelope Password Digest
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header> <wsse:Security soap:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> <wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="UsernameToken-B7CAA505B63AE99FD314316382790611"> <wsse:Username>wsuser</wsse:Username> <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">doAUEzig+QRcXp+04R/7zZx96oQ=</wsse:Password> <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">YNLZe1hq693qGsboH5dCag==</wsse:Nonce> <wsu:Created>2015-05-14T21:17:59.059Z</wsu:Created> </wsse:UsernameToken> </wsse:Security> </soap:Header> <soap:Body> <ns2:add xmlns="http://domain.calc.elan.com" xmlns:ns2="http://impl.service.calc.elan.com"> <ns2:input> <inputA>20</inputA> <inputB>14</inputB> </ns2:input> </ns2:add> </soap:Body> </soap:Envelope>
your ws security plain text/Digest Password policy Client implementation is completed successfully!!! You can also download complete working code from the following location. Thanks for visiting my learning blog.
Download Source Code
No comments:
Post a Comment