You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by kalakhr <ka...@yahoo.com> on 2008/03/01 00:30:33 UTC

RE: client SSL question

Here is a complete example of a CXF client using SSL and WS-Security.  You
don't need cxf.xml configuration file to use this, all is done in Java.

TestClient.java
---------------

/**
 * Please modify this class to meet your needs
 * This class is not complete
 */
import java.util.HashMap;
import java.util.Map;

import javax.xml.ws.BindingProvider;

import org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor;
import org.apache.cxf.configuration.jsse.TLSClientParameters;
import org.apache.cxf.configuration.security.FiltersType;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;

/**
 * This class was generated by Apache CXF (incubator) 2.1-incubator-SNAPSHOT
Thu Feb 28 11:09:40 EST 2008 Generated source version:
 * 2.1-incubator-SNAPSHOT
 * 
 */
public final class TestClient {

    private TestClient() {
    }

    public static void main(String args[]) throws Exception {
        TestService_Service ss = new TestService_Service();
        TestService port = ss.getInquiryServiceSoap();
        BindingProvider bp = (BindingProvider) port;
       
bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
"https://www.test.com/test.svc");
        Client client = ClientProxy.getClient(port);
        HTTPConduit httpConduit = (HTTPConduit) client.getConduit();
        TLSClientParameters tlsParams = new TLSClientParameters();
        tlsParams.setSecureSocketProtocol("SSL");
        FiltersType filters = new FiltersType();
        filters.getInclude().add("SSL_RSA_WITH_RC4_128_MD5");
        filters.getInclude().add("SSL_RSA_WITH_RC4_128_SHA");
        tlsParams.setCipherSuitesFilter(filters);
        httpConduit.setTlsClientParameters(tlsParams);
        Map outProps = new HashMap();
        // outProps.put(WSHandlerConstants.ACTION,
WSHandlerConstants.USERNAME_TOKEN);
        outProps.put(WSHandlerConstants.ACTION,
WSHandlerConstants.USERNAME_TOKEN + " " + WSHandlerConstants.TIMESTAMP);
        // Specify our username
        outProps.put(WSHandlerConstants.USER, "username");
        // Password type : plain text
        // outProps.setProperty(WSHandlerConstants.PASSWORD_TYPE,
WSConstants.PW_TEXT);
        // for hashed password use:
        // properties.setProperty(WSHandlerConstants.PASSWORD_TYPE,
WSConstants.PW_DIGEST);
        outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
        // Callback used to retrive password for given user.
        outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS,
ClientPasswordHandler.class.getName());
        WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
        Endpoint endPoint = client.getEndpoint();
        endPoint.getOutInterceptors().add(wssOut);
        endPoint.getOutInterceptors().add(new SAAJOutInterceptor());
        System.out.println("Invoking operation1 ...");
        try {
            Operation1MType operation1MType = new Operation1MType();
            Operation1RsMType operation1RsMType =
port.acctSrch(operation1MType);
            System.out.println("operation1 result=" + operation1RsMType);
        }
        catch (Exception e) {
            System.out.println(e.toString());
        }
        System.exit(0);
    }
}


And here is the ClientPasswordHandler.java
-----------------------------------------

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 ClientPasswordHandler implements CallbackHandler {

    public void handle(Callback[] callbacks) throws IOException,
UnsupportedCallbackException {
        WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
        pc.setPassword("password");
    }
}

I have tested this client against a .NET service that requires both SSL and
WS-Security.

Good Luck,
Khaled


yulinxp wrote:
> 
> Now using NET, I can connect to it! But CXF client still doesn't work! The
> message could be sent!
>                                                                                                          
> --------typo, I mean couldn't        
> Please help!
> 
> 

-- 
View this message in context: http://www.nabble.com/client-SSL-question-tp15564062p15769013.html
Sent from the cxf-user mailing list archive at Nabble.com.