JAX-WS - 添加 SOAP 标头
我正在尝试创建一个独立的客户端来使用一些网络服务。我必须将我的用户名和密码添加到 SOAP 标头中。我尝试按如下方式添加凭据:
OTSWebSvcsService service = new OTSWebSvcsService();
OTSWebSvcs port = service.getOTSWebSvcs();
BindingProvider prov = (BindingProvider)port;
prov.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "myusername");
prov.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "mypassword");
...
当我调用服务上的方法时,出现以下异常:
com.ibm.wsspi.wssecurity.SoapSecurityException: WSEC5048E: One of "SOAP Header" elements required.
我做错了什么?如何将这些属性添加到 SOAP 标头?
编辑:我正在使用 JDK6 中包含的 JAX-WS 2.1。我现在使用 JAX-WS 2.2。我现在遇到以下异常:
com.ibm.wsspi.wssecurity.SoapSecurityException: WSEC5509E: A security token whose type is [http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#UsernameToken] is required.
如何创建此令牌?
I am trying to create a standalone client to consume some web services. I must add my username and password to the SOAP Header. I tried adding the credentials as follows:
OTSWebSvcsService service = new OTSWebSvcsService();
OTSWebSvcs port = service.getOTSWebSvcs();
BindingProvider prov = (BindingProvider)port;
prov.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "myusername");
prov.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "mypassword");
...
When I call a method on the service I get the following exception:
com.ibm.wsspi.wssecurity.SoapSecurityException: WSEC5048E: One of "SOAP Header" elements required.
What am I doing wrong? How would I add these properties to the SOAP Header?
Edited: I was using JAX-WS 2.1 included in JDK6. I am now using JAX-WS 2.2. I now get the following exception:
com.ibm.wsspi.wssecurity.SoapSecurityException: WSEC5509E: A security token whose type is [http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#UsernameToken] is required.
How do I go about creating this token?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
可以使用 @WebParam(header = true) 在 SOAP 标头 (JaxWS) 中传输数据:
如果要生成带有 SOAP 标头的客户端,则需要使用 -XadditionalHeaders:
如果不需要 @Oneway Web 服务,则可以使用支架:
Data can be transferred in SOAP header (JaxWS) by using @WebParam(header = true):
If you want to generate a client with SOAP Headers, you need to use -XadditionalHeaders:
If don't need @Oneway web service, you can use Holder:
不是 100% 确定,因为问题缺少一些细节,但如果您使用的是 JAX-WS RI,请查看 发送请求时添加 SOAP 标头:
相应地更新您的代码并重试。如果您没有使用 JAX-WS RI,请更新您的问题并提供更多上下文信息。
更新:您要调用的 Web 服务似乎受到 WS-Security/UsernameTokens 的保护。这与您最初的问题有点不同。无论如何,要配置您的客户端发送用户名和密码,我建议查看这篇精彩的文章 为基于 Metro 的 Web 服务实现 WS-Security UsernameToken 配置文件(跳至步骤 4)。使用 NetBeans 执行此步骤可能会使事情变得容易很多。
Not 100% sure as the question is missing some details but if you are using JAX-WS RI, then have a look at Adding SOAP headers when sending requests:
Update your code accordingly and try again. And if you're not using JAX-WS RI, please update your question and provide more context information.
Update: It appears that the web service you want to call is secured with WS-Security/UsernameTokens. This is a bit different from your initial question. Anyway, to configure your client to send usernames and passwords, I suggest to check the great post Implementing the WS-Security UsernameToken Profile for Metro-based web services (jump to step 4). Using NetBeans for this step might ease things a lot.
我添加这个答案是因为其他答案都不适合我。
我必须向代理添加一个标头处理程序:
在代理中,我只需添加处理程序:
I'm adding this answer because none of the others worked for me.
I had to add a Header Handler to the Proxy:
In the proxy, I just add the Handler:
另外,如果您使用 Maven 构建项目,则需要添加以下依赖项:
这将为您提供 com.sun.xml.ws.developer.WSBindingProvider 类。
链接: https://mvnrepository.com/artifact/com.sun.xml .ws/jaxws-rt
Also, if you're using Maven to build your project, you'll need to add the following dependency:
This provides you with the class
com.sun.xml.ws.developer.WSBindingProvider
.Link: https://mvnrepository.com/artifact/com.sun.xml.ws/jaxws-rt
您可以将用户名和密码添加到 SOAP 标头
you can add the username and password to the SOAP Header
使用 maven 和插件 jaxws-maven-plugin。这将生成一个 Web 服务客户端。确保您正在设置 xadditionalHeaders为真。这将生成带有标头输入的方法。
Use maven and the plugin jaxws-maven-plugin. this will generate a web service client. Make sure you are setting the xadditionalHeaders to true. This will generate methods with header inputs.
最好的选择(当然对我来说)是自己做。这意味着您可以通过编程方式修改 SOAP 消息的所有部分
,ModifyMessageHandler 源可以是
...
我希望这对您有帮助
The best option (for my of course) is do it yourserfl. It means you can modify programattly all parts of the SOAP message
And the ModifyMessageHandler source could be
...
I hope this helps you
在 jaxws-rt-2.2.10-ources.jar!\com\sun\xml\ws\transport\http\client\HttpTransportPipe.java 中
:来自 requestContext 的带有
MessageContext.HTTP_REQUEST_HEADERS
键的 String>> 将被复制到 SOAP 标头。通过标头使用 JAX-WS 进行应用程序身份验证<的示例/a>
BindingProvider.USERNAME_PROPERTY
和BindingProvider.PASSWORD_PROPERTY
键在HttpTransportPipe.addBasicAuth()
中以特殊方式处理,添加标准基本授权授权
标头。另请参阅 JAX-WS 中的消息上下文
In
jaxws-rt-2.2.10-ources.jar!\com\sun\xml\ws\transport\http\client\HttpTransportPipe.java
:So,
Map<String, List<String>>
from requestContext with keyMessageContext.HTTP_REQUEST_HEADERS
will be copied to SOAP headers.Sample of Application Authentication with JAX-WS via headers
BindingProvider.USERNAME_PROPERTY
andBindingProvider.PASSWORD_PROPERTY
keys are processed special way inHttpTransportPipe.addBasicAuth()
, adding standard basic authorizationAuthorization
header.See also Message Context in JAX-WS
我为这里的所有答案而苦苦挣扎,从 Pascal 的解决方案开始,由于 Java 编译器不与
rt.jar
默认情况下不再(并且使用内部类使其特定于该运行时实现)。edubriguenti 的回答让我很接近。不过,处理程序在最后一段代码中的连接方式对我来说不起作用——它从未被调用过。
我最终使用了他的处理程序类的变体,但将其连接到 javax.xml.ws.Service 实例,如下所示
:
Service service = Service.create(url, qname);
service.setHandlerResolver(
端口信息-> Collections.singletonList(new SOAPHeaderHandler(handlerArgs))
);
I struggled with all the answers here, starting with Pascal's solution, which is getting harder with the Java compiler not binding against
rt.jar
by default any more (and using internal classes makes it specific to that runtime implementation).The answer from edubriguenti brought me close. The way the handler is hooked up in the final bit of code didn't work for me, though - it was never called.
I ended up using a variation of his handler class, but wired it into the
javax.xml.ws.Service
instance like this:Service service = Service.create(url, qname);
service.setHandlerResolver(
portInfo -> Collections.singletonList(new SOAPHeaderHandler(handlerArgs))
);
将对象添加到标头我们使用此处使用的示例,但我将
通过对象工厂完成我们创建要求传递标头的对象。添加到标头
我们使用 WSBindingProvider 来添加标头。如果直接使用该对象会出现一些错误,因此我们使用该方法。
该方法将在对象 Factory 上创建一个像这样的 JAXBElement
以及我们获得的 jaxbContext,如下所示:
这会将对象添加到标头。
Adding an object to header we use the examples used here,yet i will complete
With object factory we create the object asked to pass on the header. The to add to the header
We used the WSBindingProvider to add the header. The object will have some error if used directly so we use the method
This method will create a JAXBElement like this on the object Factory
And the jaxbContext we obtained like this:
This will add the object to the header.