我将非常感谢你的帮助。
我正在尝试运行下面的测试(其目的是向远程服务器发送请求+安全标头)。
带有安全标头的请求应如下所示: http://s28.postimg.org/ehg8z5f5p/req_AM3.png
我正在使用CXF从WSDL生成对象,我正在尝试使用这些对象发送请求+安全标头(我的代码如下)。
不幸的是我收到了这个错误:
Feb 13, 2014 9:19:05 AM org.apache.cxf.phase.PhaseInterceptorChain doDefaultLogging
WARNING: Interceptor for {http://myserver.com url}dUser has thrown exception, unwinding now
java.lang.NullPointerException
at org.apache.cxf.binding.soap.interceptor.StartBodyInterceptor.handleMessage(StartBodyInterceptor.java:59)
at org.apache.cxf.binding.soap.interceptor.StartBodyInterceptor.handleMessage(StartBodyInterceptor.java:37)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)
at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:835)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1704)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1537)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1445)
at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:660)
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)
at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:565)
我的代码:
public class TestServiceAdmin
{
@Test
public void test1() throws CxfTest, JAXBException, IOException, XMLStreamException
{
Admin_Service service = new Admin_Service();
Admin port = service.getPort(Admin.class);
((BindingProvider)port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://x.x.x.x:8080/some_services");
Map<String,Object> inProps= new HashMap<String,Object>();
Map<String,Object> outProps = new HashMap<String,Object>();
//Security header
outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
outProps.put(WSHandlerConstants.USER, "user123");
outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
// Callback used to retrieve password for given user.
outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, ClientPasswordCallback.class.getName());
//copy from https://cxf.apache.org/docs/ws-security.html
org.apache.cxf.endpoint.Client client = ClientProxy.getClient(port);
org.apache.cxf.endpoint.Endpoint cxfEndpoint = client.getEndpoint();
//Out Intercepter
WSS4JOutInterceptor wssJOutInterceptor = new WSS4JOutInterceptor(outProps);
cxfEndpoint.getOutInterceptors().add(wssJOutInterceptor);
//in Intercepter
WSS4JInInterceptor wssJInInterceptor = new WSS4JInInterceptor(inProps);
cxfEndpoint.getInInterceptors().add(wssJInInterceptor);
//this phase is for fill the object for the method we will choose to run
RequestA aRequest = new RequestA();
JAXBElement<String> jxbUserName =new JAXBElement<String>(new QName("useName"), String.class, "username");
RequestA.setUserName(jxbUserName);
JAXBElement<String> jxbUserStatus =new JAXBElement<String>(new QName("userStatus"), String.class, "userStatus");
RequestA.setUserStatus(jxbUserStatus);
//run the method
ResponseA dUserResponse = port.dUser(aRequest);
System.out.println(dUserResponse.getUserStatus() );
}
}
public class ClientPasswordCallback implements CallbackHandler {
public void handle(Callback[] callbacks) throws IOException,
UnsupportedCallbackException {
WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
// set the password for our message.
pc.setPassword("pass2");
}
}