用于调用 wso2 安全 Web 服务的独立 java 代码 [英] Standalone java code to invoke wso2 secured web service

查看:15
本文介绍了用于调用 wso2 安全 Web 服务的独立 java 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 wso2esb 上有一个安全的 Web 服务.它基于用户名令牌.

I have a secured web service on wso2esb. It is based on Username token.

现在,我想创建一个独立的 Java 程序来调用这个 Web 服务.我很难弄清楚如何做到这一点.

Now, I want to create a standalone java program to invoke this web service. I m having hard time figuring out how to do this.

你能帮我解决这个问题吗?

Can you please help me in this.

感谢和问候.

推荐答案

这种方式访问​​安全的web服务,估计你用的是UT场景:

Access to a secure web service in this way, I presume you use UT scenario:

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);

此时,您可以像使用另一个 Web 服务调用一样使用存根.

At this point you coul use the stub just like another web service call.

方法loadPolicy:

the method 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>

这篇关于用于调用 wso2 安全 Web 服务的独立 java 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆