I want to connect to a REST server with a jaxrs client using apache cxf. The server has an url to authenticate and some other urls to do the actual stuff. After the login the server creates a session and keeps the connection open for 30 min. My problem is that the client doesn't store the cookies and I always get a new (not authenticated) session on the server.
I configured the clients in my spring application context.
<jaxrs:client id="loginResource"
serviceClass="com.mycompany.rest.resources.LoginResource"
address="${fsi.application.url}">
</jaxrs:client>
<jaxrs:client id="actionResource"
serviceClass="com.mycompany.rest.resources.ActionResource"
address="${fsi.application.url}">
</jaxrs:client>
How can I configure both clients to use the same session or share the session-cookie between the clients?
I have been struggling with the same problem, and I just finally arrived at a solution.
1) Make the client retain cookies.
WebClient.getConfig(proxy).getRequestContext().put(
org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
Perhaps there's a way to accomplish the above via configuration vs. programmatically.
2) Copy the cookies from one client to the other.
public static void copyCookies(Object sourceProxy, Object targetProxy) {
HTTPConduit sourceConduit = WebClient.getConfig(sourceProxy).getHttpConduit();
HTTPConduit targetConduit = WebClient.getConfig(targetProxy).getHttpConduit();
targetConduit.getCookies().putAll(sourceConduit.getCookies());
}
After using proxy A to authenticate, I call the above method to share its cookies with proxy B, which does the actual work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With