2013-07-01 71 views
2

我在wso2esb上有一个安全的web服务。它基于用户名令牌。独立的java代码来调用wso2安全的web服务

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

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

感谢和问候。

+0

你可以检查此链接与我回应:http://stackoverflow.com/questions/17207648/invoke-secured-web-service -in-wso2esb –

回答

0

你可以试试看Rampart样本。基本示例01是UsernameToken身份验证。

的源代码是在这里:link

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>