1. 一个客户端用户名密码回调类
package org.demo.ws.client;
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 ClientPasswordCallback implements CallbackHandler
{
@Override
public void handle(Callback[] callbacks) throws IOException,
UnsupportedCallbackException
{
WSPasswordCallback pwCallback = (WSPasswordCallback)callbacks[0];
pwCallback.setIdentifier("user1");
pwCallback.setPassword("password1");
}
}
2. src/ws-client.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd" >
<bean id="clientPasswordCallback" class="org.demo.ws.client.ClientPasswordCallback" />
<bean id="wSS4JOutInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor">
<constructor-arg>
<map>
<entry key="action" value="UsernameToken" />
<entry key="passwordType" value="PasswordText" />
<entry key="user" value="cxfClient" />
<entry key="passwordCallbackRef">
<ref bean="clientPasswordCallback" />
</entry>
</map>
</constructor-arg>
</bean>
<jaxws:client id="helloWorld" serviceClass="org.demo.ws.HelloWorld"
address="http://localhost:8080/spring/services/hello">
<jaxws:outInterceptors>
<ref bean="wSS4JOutInterceptor" />
</jaxws:outInterceptors>
</jaxws:client>
</beans>
3. 一个客户端测试类
package org.demo.ws.client;
import org.demo.ws.HelloWorld;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class WsClient
{
/**
* @param args
*/
public static void main(String[] args)
{
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("ws-client.xml");
HelloWorld bean = (HelloWorld)ctx.getBean("helloWorld");
String result = bean.sayHi("xiao ming");
System.out.println("result is " + result);
}
}
//