How to get a JAX-WS web service to respond to a JSESSIONID (session id)

I am new to web services. I have a JAX-WS service for which I need to implement a session mechanism. SOAP messages are sent over HTTP, we use WebLogic, so the JAXWS application is deployed to the WebLogic application server and the services are accessible from the WSDL document.

I have @WebServiceProvider (a class that implements Provider <SOAPMessage>)

Now, when I run the login request, I want the JSESSIONID session cookie to be sent back, but we do not want to use CXF or anything else, just the so-called Metro, which, frankly, I still do not quite understand. We also do not want to make this a permanent cookie, so manually adding a cookie to the response header is also not an option. But it works, I tried. I just don't understand why the session cookie is not configured automatically.

I searched the Internet and tried many things for 4 days, nothing works. Please, help.

+4
source share
2 answers

I found the answer to my question. The problem was how the bindings are used in the WebServiceProvider implementation. If the HTTP binding type is used, SOAPMessage cannot be used as the type for the provider. The correct solution here is to use Source (not sure if something else can be used either, not tried), 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; } } 
+3
source

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

You can do this by injecting a WebServiceContext into your web service, for example:

 @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"); 
+3
source

Source: https://habr.com/ru/post/1390747/


All Articles