简体   繁体   English

独立的Java代码来调用wso2安全的Web服务

[英]Standalone java code to invoke wso2 secured web service

I have a secured web service on wso2esb. 我在wso2esb上有一个安全的Web服务。 It is based on Username token. 它基于用户名令牌。

Now, I want to create a standalone java program to invoke this web service. 现在,我想创建一个独立的Java程序来调用此Web服务。 I m having hard time figuring out how to do this. 我很难弄清楚该怎么做。

Can you please help me in this. 你能帮我吗

Thanks and Regards. 谢谢并恭祝安康。

Access to a secure web service in this way, I presume you use UT scenario: 以这种方式访问​​安全的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);

At this point you coul use the stub just like another web service call. 此时,您可以像其他Web服务调用一样使用存根。

the method loadPolicy: 方法loadPolicy:

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

And an example policy file: 还有一个示例策略文件:

<?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>

You could try taking a look at the Rampart samples. 您可以尝试查看Rampart示例。 Basic Sample 01 is UsernameToken Authentication. 基本样本01是UsernameToken身份验证。

The source code is here: link 源代码在这里: 链接

I found this tutorial 我找到了本教程
http://wso2.com/library/3190 http://wso2.com/library/3190
Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM