Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

servlet set cookie secure?

javax.servlet.http.Cookie implements java.lang.Cloneable

In Cookie method, there is a method call "setSecure" , what does it use for? if i setSecure(true), is there anything i need to do on my client(javascript) side to read the cookie? what is different set/without setSecure?

like image 905
cometta Avatar asked Jan 02 '11 13:01

cometta


People also ask

How do I set a secure attribute for cookies?

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

All that setSecure(true) does is tell the browser that the cookie should only be sent back to the server if using a "secure" protocol, like https. Your JavaScript code doesn't have to do anything different.

like image 113
T.J. Crowder Avatar answered Oct 21 '22 11:10

T.J. Crowder


Yup this ensures that your session cookie is not visible to an attacker like man-in-the-middle attack. Instead of setting it manually You could alternatively configure your web.xml to handle it for you automatically.

<session-config>
   <cookie-config>
      <secure>true</secure>
   </cookie-config>
</session-config>
like image 40
Al-Kathiri Khalid Avatar answered Oct 21 '22 12:10

Al-Kathiri Khalid