简体   繁体   English

如何使用JSESSIONID(会话ID)使JAX-WS Web服务响应

[英]How to make JAX-WS webservice respond with JSESSIONID (session id)

I am a newbie in web services. 我是网络服务的新手。 I have a JAX-WS service which I need to implement session mechanism for. 我有一个JAX-WS服务,我需要为其实现会话机制。 The SOAP messages are transported over HTTP, we use WebLogic, so JAXWS application is deployed on WebLogic app server and the services are accessible from WSDL document. SOAP消息通过HTTP传输,我们使用WebLogic,因此JAXWS应用程序部署在WebLogic应用服务器上,并且可以从WSDL文档访问服务。

I have @WebServiceProvider (class that implements Provider< SOAPMessage >) 我有@WebServiceProvider(实现Provider <SOAPMessage>的类)

Now when I fire a login request I want JSESSIONID session cookie to be sent back, but we don't want to use CXF or anything else, just so called Metro, which frankly I don't yet completely understand. 现在,当我发出登录请求时,我希望将JSESSIONID会话cookie发送回去,但是我们不想使用CXF或其他任何东西,就是所谓的Metro,坦率地说我还没有完全理解。 We also do not want to make this a persistent cookie, so manually adding a cookie to the response header is also not an option. 我们也不希望将其作为持久性cookie,因此手动将cookie添加到响应头也不是一种选择。 But that works, I tried it. 但是这很有效,我试过了。 I just do not understand why session cookie is not set up automatically. 我只是不明白为什么会话cookie没有自动设置。

I've been searching the web and trying many things for 4 days now, nothing works. 我一直在网上搜索并尝试了很多东西4天,没有任何作用。 Please help. 请帮忙。

Typically, just accessing the HttpSession in your web service should be sufficient to set the session cookie in your response. 通常,只需访问Web服务中的HttpSession就足以在响应中设置会话cookie。

You can do this by injecting the WebServiceContext into your web service like so -- 您可以通过将WebServiceContext注入Web服务来实现此目的,如下所示 -

@Resource
private WebServiceContext ctx;
public void webServiceMethod() {
     MessageContext mc = ctx.getMessageContext();
     HttpSession session =    ((javax.servlet.http.HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST)).getSession();
     if (session == null)
         throw new WebServiceException("No HTTP Session found");

I found the answer to my own question. 我找到了自己问题的答案。 The issue was in the way the bindings are used in WebServiceProvider implementation. 问题在于WebServiceProvider实现中使用绑定的方式。 If HTTP binding type is used then SOAPMessage cannot be used as a type for Provider. 如果使用HTTP绑定类型,则SOAPMessage不能用作Provider的类型。 The correct solution here is to use Source (not sure if something else can be used too, didn't try), ie 这里正确的解决方案是使用Source(不确定是否可以使用其他东西,没试过),即

package com.primavera.ws.jaxws.provider;

import javax.annotation.Resource;
import javax.xml.ws.BindingType;
import javax.xml.ws.Provider;
import javax.xml.ws.Service;
import javax.xml.ws.ServiceMode;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.WebServiceProvider;

@WebServiceProvider(portName="MyPort", serviceName="MyService", targetNamespace="http://mytargetlocation", wsdlLocation="WEB-INF/wsdl/My.wsdl")
@ServiceMode(value = Service.Mode.MESSAGE)
@BindingType(HTTPBinding.HTTP_BINDING)

public class MyProvider implements Provider<Source> {

    @Resource
    private WebServiceContext context;


    public MyProvider()
    {
    }

    @Override
    public Source invoke(Source request)
    {
        MessageContext mc = context.getMessageContext();
        HttpSession session =    ((javax.servlet.http.HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST)).getSession();
        if (session == null)
            throw new WebServiceException("No HTTP Session found");

        System.out.println("SessionID: " + session.getId());

        return request;
    }
}

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

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