CXF 简单的安全认证方法

1、代理工厂模式

 long s = new Date().getTime(); // JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); ClientProxyFactoryBean factory = new ClientProxyFactoryBean(); Map<String, Object> outProps = new HashMap<String, Object>(); outProps.put(WSHandlerConstants.ACTION,WSHandlerConstants.USERNAME_TOKEN); outProps.put(WSHandlerConstants.USER, "cxfservice101"); outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT); outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS,UTPasswordCallback.class.getName()); WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps); factory.getOutInterceptors().add(wssOut); factory.getOutInterceptors().add(new LoggingOutInterceptor()); factory.setServiceClass(StandardCodeAccessService.class); factory.setAddress("http://192.168.100.56:8080/SCMS/ws/StandardCode"); StandardCodeAccessService client = StandardCodeAccessService) factory .create(); client.test();//你webservice的方法

2、QName模式 这里所有的

import java.io.File; import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.namespace.QName; import org.apache.cxf.endpoint.Client; import org.apache.cxf.endpoint.Endpoint; import org.apache.cxf.frontend.ClientProxy; import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor; import org.apache.ws.security.WSConstants; import org.apache.ws.security.handler.WSHandlerConstants; .......... DemoSrv_Service ss = new DemoSrv_Service(wsdlURL, SERVICE_NAME); DemoSrv port = ss.getDemoSrvPort(); Client client = ClientProxy.getClient(port); Endpoint cxfEndpoint = client.getEndpoint(); Map outProps = new HashMap(); outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN); outProps.put(WSHandlerConstants.USER, ClientConstants.USERNAME); outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT); outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, ClientPasswordHandler.class.getName()); WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps); cxfEndpoint.getOutInterceptors().add(wssOut);

你可能感兴趣的:(webservice,object,service,HashMap,callback,token)