Struts2: Session Problem (after reverse proxy)

2020-04-12 12:58发布

问题:

I store session parameters in a Struts2 session map that I get in my actions using the SessionAware interface. My Application is in the /MyApp path.

After setting up the struts2 application on an Apache server with an inverse proxy redirect that makes the URL http://www.appdomain.com/ point to my local tomcat on localhost:8080/MyApp, Struts2 session handling doesn't work anymore. I expect that the session cookies are stored for the Struts2 context of http://localhost:8080/MyApp instead of http://www.appdomain.com/ ...

Is there a solution in Struts2 configuration? Or in programmatically changing the session cookie somehow? Couldn't find any info about this on the interwebs or in the official documentation. Please help, I'm already in production and my logins don't work ;-)

回答1:

This is old but I found it and would like to drop my 5 cents.

One fix that you can use is to edit the web.xml and in the session-config set something like:

<session-config>
    <session-timeout>10</session-timeout>
    <cookie-config>
        <name>MYAPPSESSIONID</name>
        <path>/</path>
    </cookie-config>
</session-config>

This changes

  • The sessionid cookie from JSESSIONID to MYAPPSESSIONID so it will not collide with other apps that may be exposed on the same proxy
  • The path that the cookie applies. So it will always be sent to the server

Hope this may help others.



回答2:

I just solved the problem with a dirty hack: I passed the Session Id to the JSP and use a javascript to set the needed JSESSIONID cookie clientside.

function createCookie(name,value,days) { if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); } else var expires = ""; document.cookie = name+"="+value+expires+"; path=/"; }

$(document).ready(function() { createCookie("JSESSIONID","",3); });

Got the JS code from this page: http://www.quirksmode.org/js/cookies.html

Thank you, problem solved!

Best Regards, Tim



回答3:

Put this in your httpd.conf

#all cookies from /MyApp are proxied to "/"
ProxyPassReverseCookiePath /MyApp /

http://httpd.apache.org/docs/current/mod/mod_proxy.html#proxypassreversecookiepath