0

我正在寻找使用 CXF 构建独立的 ExactTarget SOAP 客户端。

我能够使用 Glassfish Metro 创建客户端,但出于对未来支持的考虑,我们希望使用 CXF。我找到了一个旧示例和相关项目,但它太旧了,无法使用。

目前我正在尝试了解如何在存根/端口对象上设置处理程序并将动态用户名和密码传递给它。动态我的意思是:应用程序在运行时从用户那里获取用户名和密码。这是我目前用于 Metro 实施的代码:

PartnerAPI service = new PartnerAPI();
Soap stub = service.getSoap();      
Map<String, Object> outProperties = new HashMap<String, Object>();        
Map ctx = ((BindingProvider) stub).getRequestContext();

requestContext.put(BindingProvider.USERNAME_PROPERTY, user);
requestContext.put(BindingProvider.PASSWORD_PROPERTY, password);

List<Handler> chain = new ArrayList<Handler>();
chain.add(new SecurityHandler());
((BindingProvider) stub).getBinding().setHandlerChain(chain);

我正在尝试为 CXF 实现重用前 4-6 行,但我不能使用我拥有的处理程序,因为它们依赖于com.sun.xml.wss.XWSSProcessor.

4

1 回答 1

0

这是完成所有操作的代码:

private static Soap createApiStub() {
    PartnerAPI service = new PartnerAPI();
    Soap stub = service.getSoap();          
    Client client = org.apache.cxf.frontend.ClientProxy.getClient(stub);     

    Map<String, Object> outProps = new HashMap<String, Object>();        
    outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
    outProps.put(WSHandlerConstants.USER, username);     
    outProps.put(WSHandlerConstants.PASSWORD_TYPE,WSConstants.PW_TEXT);        
    // Automatically adds a Base64 encoded message nonce and a created timestamp
    outProps.put(WSHandlerConstants.ADD_UT_ELEMENTS,WSConstants.NONCE_LN + " " + WSConstants.CREATED_LN);    
    outProps.put(WSHandlerConstants.PW_CALLBACK_REF, new ClientPasswordCallback(username, password));
    WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
    client.getOutInterceptors().add(wssOut);

    //Enable GZip compression
    Map<String, java.util.List<String>> httpHeaders = new HashMap<String, java.util.List<String>>();
    httpHeaders.put("Content-Encoding",Collections.singletonList("gzip"));
    httpHeaders.put("Accept-Encoding",Collections.singletonList("gzip"));
    Map<String, Object> reqContext = client.getRequestContext();
    reqContext.put(MessageContext.HTTP_REQUEST_HEADERS,httpHeaders);

    return stub;
}

这是处理程序的实现:

public class ClientPasswordCallback implements CallbackHandler {

    private String username;
    private String password;

    public ClientPasswordCallback(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public void handle(Callback[] callbacks) throws IOException, 
    UnsupportedCallbackException {
        for (Callback callback: callbacks){
            if (callback instanceof WSPasswordCallback){
                WSPasswordCallback pc = (WSPasswordCallback) callback;              
                if (username.equals(pc.getIdentifier())) {                  
                    pc.setPassword(password);                   
                }
            } else if (callback instanceof NameCallback){
                throw new UnsupportedCallbackException(callback);
            } else {
                throw new UnsupportedCallbackException(callback);
            }           
        }
    }
}

这个答案帮助我动态地传递密码。

于 2014-01-28T19:07:46.767 回答