Mule ESB 学习笔记(14)CXF SOAP基于UsernameToken的验证
?
?
?
?
回调类:
package com.mulesoft.mule.soap.test;import java.net.MalformedURLException;import java.net.URL;import java.util.HashMap;import java.util.Map;import javax.xml.namespace.QName;import javax.xml.ws.Service;import org.apache.cxf.endpoint.Client;import org.apache.cxf.frontend.ClientProxy;import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;import com.mulesoft.mule.soap.security.Greeter;import com.mulesoft.mule.soap.security.PasswordCallback;/** * * <p>功能描述,该部分必须以中文句号结尾。<p> * * 创建日期 2013-8-27<br> * @author $Author$<br> * @version $Revision$ $Date$ * @since 3.0.0 */public class MuleSecureClient{ public static void main(String[] args) throws Exception { Greeter service1 = createService("http://localhost:63082/services/unsecure?wsdl", null); System.out.println(service1.greet("Mule")); Greeter service2 = createService("http://localhost:63082/services/username?wsdl", getUsernameTokenProps("UsernameToken Timestamp")); System.out.println(service2.greet("Mule")); } protected static Map<String, Object> getUsernameTokenProps(String action) { Map<String, Object> wss4jProps = new HashMap<String, Object>(); //设置请求时候的参数信息 wss4jProps.put("action", action); wss4jProps.put("user", "joe"); //回调类 wss4jProps.put("passwordCallbackClass", PasswordCallback.class.getName()); return wss4jProps; } public static Greeter createService(String url, Map<String, Object> wss4jProps) { URL wsdlDocumentLocation; try { wsdlDocumentLocation = new URL(url); } catch (MalformedURLException e) { throw new RuntimeException("Invalid test definition", e); } QName serviceName = new QName("http://security.soap.mule.mulesoft.com/", "GreeterService"); Service dynService = Service.create(wsdlDocumentLocation, serviceName); Greeter service = dynService.getPort(Greeter.class); Client client = ClientProxy.getClient(service); if (wss4jProps != null) { client.getOutInterceptors().add(new WSS4JOutInterceptor(wss4jProps)); } return service; } }?