0

我想为wso2 esb 中存在的版本代理服务创建一个 java 客户端。我已经使用用户名令牌身份验证方案保护了版本代理。现在我已经开始创建 java 客户端来调用这个安全代理服务,我的客户端代码是:

package org.wso2.carbon.security.ws;

import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.axis2.description.AxisBinding;
import org.apache.axis2.description.AxisEndpoint;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.neethi.Policy;

import javax.xml.namespace.QName;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Map;

public class HelloServiceClient {

    static {
        System.setProperty("javax.net.ssl.trustStore", "/path/to/keystore" + File.separator+ "wso2carbon.jks");
        System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
    }

    public static void main(String[] args) {
        try {

            int securityScenario = getSecurityScenario();

            String repository = "/path/to/repo" + File.separator + "repository";

            ConfigurationContext confContext =
                    ConfigurationContextFactory.
                            createConfigurationContextFromFileSystem(repository, null);

            String endPoint = "HelloServiceHttpSoap12Endpoint";
            if(securityScenario == 1){
                endPoint = "HelloServiceHttpsSoap12Endpoint";   // scenario 1 uses HelloServiceHttpsSoap12Endpoint
            }

            RPCServiceClient dynamicClient =
                    new RPCServiceClient(confContext,
                                         new URL("http://pc213712:8281/services/Version?wsdl"),
                                         new QName("http://version.services.core.carbon.wso2.org", "Version"),
                                         endPoint);

            //Engage Modules
            dynamicClient.engageModule("rampart");
            dynamicClient.engageModule("addressing");

            //TODO : Change the port to monitor the messages through TCPMon
            if(securityScenario != 1){
                dynamicClient.getOptions().setTo(new EndpointReference("http://pc213712:8281/services/Version/"));
            }

            //Get the policy from the binding and append the rampartconfig assertion
            Map endPoints = dynamicClient.getAxisService().getEndpoints();
            AxisBinding axisBinding = ((AxisEndpoint) endPoints.values().iterator().next()).getBinding();
            Policy policy = axisBinding.getEffectivePolicy();
            **policy.addAssertion(RampartConfigBuilder.createRampartConfig(securityScenario));**
            axisBinding.applyPolicy(policy);

            //Invoke the service
            Object[] returnArray = dynamicClient.invokeBlocking(new QName("http://www.wso2.org/types","greet"),
                                                                new Object[]{"Alice"},
                                                                new Class[]{String.class});

            System.out.println((String) returnArray[0]);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static int getSecurityScenario() {
        int scenarioNumber = 0;
        while (scenarioNumber < 1 || scenarioNumber > 15) {
            System.out.print("Insert the security scenario no : ");
            String inputString = readOption();
            try {
                scenarioNumber = new Integer(inputString);
            } catch (Exception e) {
                System.out.println("invalid input, insert a integer between 1 and 15");
            }
            if(scenarioNumber < 1 || scenarioNumber > 15){
                System.out.println("Scenario number should be between 1 and 15");
            }
        }
        return scenarioNumber;
    }
    private static String readOption() {
        try {
            BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
            String str;
            while ((str = console.readLine()).equals("")) {
            }
            return str;
        } catch (Exception e) {
            return null;
        }
    }
}

但是在上面的代码中,我被以下一行所震撼:

policy.addAssertion(RampartConfigBuilder.createRampartConfig(securityScenario));

在我的rampart_core jar 中,我得到了RampartConfigBuilder类,但在这个类中没有这样的方法,称为createRampartConfig。所以我无法创建 Rampart 配置。我能做些什么来解决这个问题?期待您的解决方案。提前致谢

4

0 回答 0