我使用weblogic 10.3中提供的api创建了一个Web服务客户端原型。我被告知我需要使用Metro 2.0(它已经被用于其他项目)。我遇到的问题是WSDL不包含任何安全策略信息,但每个方法调用都需要UsernameToken。在weblogic中,我能够编写自己的策略xml文件并使用它实例化我的服务(见下文),但我似乎无法弄清楚如何使用Metro做同样的事情。
没有Policy.xml
<?xml version="1.0"?>
<wsp:Policy
xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200512">
<sp:SupportingTokens>
<wsp:Policy>
<sp:UsernameToken
sp:IncludeToken="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200512/IncludeToken/AlwaysToRecipient">
<wsp:Policy>
<sp:WssUsernameToken10/>
<sp:HashPassword/>
</wsp:Policy>
</sp:UsernameToken>
</wsp:Policy>
</sp:SupportingTokens>
</wsp:Policy>
Client.java(Weblogic)
ClientPolicyFeature cpf = new ClientPolicyFeature();
InputStream asStream = WebServiceSoapClient.class.getResourceAsStream("Policy.xml");
cpf.setEffectivePolicy(new InputStreamPolicySource(asStream));
try
{
webService = new WebService(new URL("http://192.168.1.10/WebService/WebService.asmx?wsdl"), new QName("http://testme.com", "WebService"));
}
catch ( MalformedURLException e )
{
e.printStackTrace();
}
WebServiceSoap client = webService.getWebServiceSoap(new WebServiceFeature[] {cpf});
List<CredentialProvider> credProviders = new ArrayList<CredentialProvider>();
String username = "user";
String password = "pass";
CredentialProvider cp = new ClientUNTCredentialProvider(username.getBytes(), password.getBytes());
credProviders.add(cp);
Map<String, Object> rc = ((BindingProvider) client).getRequestContext();
rc.put(WSSecurityContext.CREDENTIAL_PROVIDER_LIST, credProviders);
...
我能够使用Metro生成我的代理类但是我无法弄清楚如何配置它来发送UsernameToken。我尝试过网上几个不起作用的不同例子。任何帮助将不胜感激。
答案 0 :(得分:1)