Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing Tomcat to use secure JSESSIONID cookie over http

Is there a way to configure Tomcat 7 to create JSESSIONID cookie with a secure flag in all occasions?

Usual configuration results in Tomcat flagging session cookie with secure flag only if connection is made through https. However in my production scenario, Tomcat is behind a reverse proxy/load balancer which handles (and terminates) the https connection and contacts tomcat over http.

Can I somehow force secure flag on session cookie with Tomcat, even though connection is made through plain http?

like image 288
Krešimir Nesek Avatar asked Feb 20 '13 20:02

Krešimir Nesek


People also ask

How do I set HTTP cookies only?

Set HttpOnly cookie in PHPini_set("session. cookie_httponly", True); This is the most common way to set cookies in PHP, empty variables will hold their default value.

How do I apply the secure attribute to session cookies?

Overview. The secure attribute is an option that can be set by the application server when sending a new cookie to the user within an HTTP Response. The purpose of the secure attribute is to prevent cookies from being observed by unauthorized parties due to the transmission of the cookie in clear text.


2 Answers

In the end, contrary to my initial tests, web.xml solution worked for me on Tomcat 7.

E.g. I added this snippet to web.xml and it marks session cookie as secure even when reverse proxy contacts tomcat over plain HTTP.

<session-config>     <cookie-config>         <http-only>true</http-only>         <secure>true</secure>     </cookie-config> </session-config> 
like image 80
Krešimir Nesek Avatar answered Sep 19 '22 04:09

Krešimir Nesek


ServletContext.getSessionCookieConfig().setSecure(true)

like image 44
Mark Thomas Avatar answered Sep 21 '22 04:09

Mark Thomas