在wso2esb中调用安全的Web服务

时间:2013-06-20 07:25:43

标签: web-services wso2 axis esb

我在WSO2 ESB中创建了一个Web服务。安全性实现只有特定的用户角色才能访问它。

现在,当我点击这个使用SOAP-UI的服务时,通过提供用户名,密码,密码类型,我可以点击该服务。

现在,我想创建一个可以访问此SECURED WEB服务的StandAlone Java项目。

我正在尝试为此目的实现apache-rampart,但我所拥有的信息是分散的。

任何人都可以帮助我了解如何访问此安全服务。

谢谢和问候。

1 个答案:

答案 0 :(得分:1)

以这种方式访问​​安全的Web服务,我假设您使用UT方案:

String trustStore = null;  
ConfigurationContext ctx = null;
String policyFilePath = "[file_system_path]/secure_sample_policy.xml";

trustStore = "[file_system_path]/wso2carbon.jks";  
System.setProperty("javax.net.ssl.trustStore",trustStore);  
System.setProperty("javax.net.ssl.trustStorePassword","pass_store");  

ctx = ConfigurationContextFactory.createConfigurationContextFromFileSystem(null,
        null);
this.stub = new ProxyStub(ctx);
stub._getServiceClient().engageModule("rampart");
stub._getServiceClient().engageModule("addressing");

Options options = this.stub._getServiceClient().getOptions();
options.setUserName("user");
        options.setPassword("pass");

options.setProperty(RampartMessageData.KEY_RAMPART_POLICY, loadPolicy(policyFilePath));
this.stub._getServiceClient().setOptions(options);

方法loadPolicy:

private static Policy loadPolicy(String xmlPath) throws Exception {
    StAXOMBuilder builder = new StAXOMBuilder(xmlPath);
    return PolicyEngine.getPolicy(builder.getDocumentElement());
}

示例政策文件:

<?xml version="1.0" encoding="UTF-8"?>

<wsp:Policy wsu:Id="UTOverTransport" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">
    <wsp:ExactlyOne>
      <wsp:All>
        <sp:TransportBinding xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
          <wsp:Policy>
            <sp:TransportToken>
              <wsp:Policy>
                <sp:HttpsToken RequireClientCertificate="false"/>
              </wsp:Policy>
            </sp:TransportToken>
            <sp:AlgorithmSuite>
              <wsp:Policy>
                <sp:Basic256/>
              </wsp:Policy>
            </sp:AlgorithmSuite>
            <sp:Layout>
              <wsp:Policy>
                <sp:Lax/>
              </wsp:Policy>
            </sp:Layout>
            <sp:IncludeTimestamp/>
          </wsp:Policy>
        </sp:TransportBinding>
        <sp:SignedSupportingTokens xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
            <wsp:Policy>
                <sp:UsernameToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient"/>
          </wsp:Policy>
        </sp:SignedSupportingTokens>

      </wsp:All>
    </wsp:ExactlyOne>
</wsp:Policy>