简体   繁体   English

JAX-WS客户端:维护跨多个服务的会话/ cookie

[英]JAX-WS client: maintain session/cookies across multiple services

I'm using Netbeans to automatically create webservice clients based off WSDL files. 我正在使用Netbeans根据WSDL文件自动创建Web服务客户端。 This works well, except the webservices I'm using require that I pass in a custom cookie in the HTTP header to every webservice in order to prove my identity. 这很好用,除了我正在使用的Web服务要求我将HTTP标头中的自定义cookie传递给每个Web服务以证明我的身份。

I use one webservice named Utility to get my authorization. 我使用一个名为Utility Web服务来获得我的授权。 This sets a cookie which needs to be provided in all subsequent calls to any webservice. 这将设置一个cookie,在随后对任何Web服务的所有调用中都需要提供该cookie。

This can be accomplished by setting javax.xml.ws.session.maintain to true on the BindingProvider of the port for the webservice. 这可以通过在Web服务端口的BindingProvider上将javax.xml.ws.session.maintain设置为true来实现。 This works great for subsequent calls to the methods in the Utility webservice. 这对于后续调用Utility Webservice中的方法非常有用。 The problem is that this only maintains the session/cookie for that single webservice. 问题在于,这仅维护单个Web服务的会话/ cookie。 I need it across others as well. 我也需要其他人。

I need the cookie passed in to a separate webservice named History How can I accomplish this? 我需要将Cookie传递到名为History单独的 Web服务中,该如何实现? Is it feasible to have a super Service class which both Utility and History could extend and share the same session state with? 具有实用程序和历史记录都可以扩展并共享相同会话状态的超级服务类是否可行?

I've found a solution. 我找到了解决方案。

You can get response headers using this after you've made the call: 拨打电话后,可以使用以下方法获取响应头:

((BindingProvider)port).getResponseContext().get(MessageContext.HTTP_RESPONSE_HEADERS);

Find the Set-Cookie header and store its value. 找到Set-Cookie标头并存储其值。

Then before your next request (in any webservice) you can set the Cookie header: 然后,在下一个请求(在任何Web服务中)之前,您可以设置Cookie标头:

((BindingProvider)port).getRequestContext().put(
            MessageContext.HTTP_REQUEST_HEADERS,
                Collections.singletonMap("Cookie", Collections.singletonList(cookieValue)
            )
        );

Just commenting because solution above didn't work for me. 只是发表评论是因为上面的解决方案对我不起作用。 I got UnsupportedOperationException. 我收到了UnsupportedOperationException。 I believe issue was caused because singletonMap doesn't allow changes. 我相信是由于singletonMap不允许更改引起的。 xml headers were also needed, so I set those first. 还需要xml标头,因此我首先设置了这些标头。

Map<String, List<String>> headers= CastUtils.cast((Map)port.getRequestContext().get("javax.xml.ws.http.request.headers"));
if (headers == null) {
    headers = new HashMap<String, List<String>>();
    port.getRequestContext().put("javax.xml.ws.http.request.headers", headers);
}

headers.put("Cookie", Collections.singletonList(cookieValue));
((BindingProvider)port).getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, headers); 

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

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