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

发布于 2024-12-26 18:44:00 字数 425 浏览 2 评论 0原文

我是网络服务的新手。我有一个 JAX-WS 服务,我需要为其实现会话机制。 SOAP 消息通过 HTTP 传输,我们使用 WebLogic,因此 JAXWS 应用程序部署在 WebLogic 应用程序服务器上,并且可以从 WSDL 文档访问服务。

我有 @WebServiceProvider (实现 Provider的类)

现在,当我触发登录请求时,我希望发回 JSESSIONID 会话 cookie,但我们不想使用 CXF 或其他任何东西,只是所谓的 Metro,它坦白说我还没有完全理解。我们也不希望将其设为持久性 cookie,因此也不能手动将 cookie 添加到响应标头。但这确实有效,我尝试过。我只是不明白为什么会话cookie 没有自动设置。

我已经在网上搜索并尝试了很多东西,已经四天了,但没有任何效果。请帮忙。

I am a newbie in web services. I have a JAX-WS service which I need to implement session mechanism for. 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.

I have @WebServiceProvider (class that implements 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. 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. But that works, I tried it. I just do not understand why session cookie is not set up automatically.

I've been searching the web and trying many things for 4 days now, nothing works. Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南冥有猫 2025-01-02 18:44:00

通常,只需访问 Web 服务中的 HttpSession 就足以在响应中设置会话 cookie。

您可以通过将 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");

Typically, just accessing the HttpSession in your web service should be sufficient to set the session cookie in your response.

You can do this by injecting the WebServiceContext into your web service like so --

@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");
对你而言 2025-01-02 18:44:00

我找到了我自己问题的答案。问题在于 WebServiceProvider 实现中使用绑定的方式。如果使用 HTTP 绑定类型,则 SOAPMessage 不能用作 Provider 的类型。这里正确的解决方案是使用 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;
    }
}

I found the answer to my own question. The issue was in the way the bindings are used in WebServiceProvider implementation. If HTTP binding type is used then SOAPMessage cannot be used as a type for Provider. The correct solution here is to use Source (not sure if something else can be used too, didn't try), i.e.

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文