Java development | setting the secure attribute of security Cookie
What is it and why do I care ?
Session} cookies (or cookies containing JSSESSIONID) refer to cookies used to manage session sessions of web applications The session ID identification of a specific user is saved in these cookies, and the same session ID and related data in the session life cycle are also saved on the server. The most commonly used session management method in web applications is to identify sessions by sending cookies to the server at each request.
You can set an additional secure ID to prompt the browser that cookies can only be transmitted through HTTPS (encrypted mode), but not HTTP (unencrypted mode). This method ensures that your session cookie is invisible to the attacker and avoids man in the middle Attack (MITM Attack for short). This is not a perfect session security management scheme, but an important step.
what should I do about it ?
The response is simple. You must add the secure flag to the session cookie (if possible, it is best to ensure that all cookies in the request are transmitted through Https)
The following is an example: a session cookie without a secure ID may be compromised
Cookie: jsessionid=AS348AF929FK219CKA9FK3B79870H;
Add secure identity:
Cookie: jsessionid=AS348AF929FK219CKA9FK3B79870H; secure;
The way is simple. You can even set this flag manually. If you are developing in Servlet3 or newer environment, you only need to set it on the web XML simple configuration. You just need to be on the web Add the following fragment to the XML:
<session-config>
<cookie-config>
<secure>true</secure>
</cookie-config>
</session-config>
___________________________________________________________________________________________________
Java development | security section sets the HttpOnly attribute of cookies
Description of HttpOnly property of Cookie
Interceptor settings add
This code is taken from CookieFilter Let's add the HttpOnly attribute to all cookie s.
- public class CookieFilter implements Filter {
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- HttpServletRequest req = (HttpServletRequest) request;
- HttpServletResponse resp = (HttpServletResponse) response;
- Cookie[] cookies = req.getCookies();
- if (cookies != null) {
- Cookie cookie = cookies[0];
- if (cookie != null) {
- /*cookie.setMaxAge(3600);
- cookie.setSecure(true);
- resp.addCookie(cookie);*/
- //Servlet 2.5 does not support setting HttpOnly property directly on cookies
- String value = cookie.getValue();
- StringBuilder builder = new StringBuilder();
- builder.append("JSESSIONID=" + value + "; ");
- builder.append("Secure; ");
- builder.append("HttpOnly; ");
- Calendar cal = Calendar.getInstance();
- cal.add(Calendar.HOUR, 1);
- Date date = cal.getTime();
- Locale locale = Locale.CHINA;
- SimpleDateFormat sdf =
- new SimpleDateFormat("dd-MM-yyyy HH:mm:ss",locale);
- builder.append("Expires=" + sdf.format(date));
- resp.setHeader("Set-Cookie", builder.toString());
- }
- }
- chain.doFilter(req, resp);
- }
- public void destroy() {
- }
- public void init(FilterConfig arg0) throws ServletException {
- }
- }
The red font is the servlet version. References: View servlet/jsp version
- Manifest-Version: 1.0
- Ant-Version: Apache Ant 1.9.3
- Created-By: 1.6.0_45-b06 (Sun Microsystems Inc.)
- X-Compile-Source-JDK: 1.6
- X-Compile-Target-JDK: 1.6
- Name: javax/servlet/
- Specification-Title: Java API for Servlets
- <span style="color:#ff0000;">Specification-Version: 3.0</span>
- Specification-Vendor: Sun Microsystems, Inc.
- Implementation-Title: javax.servlet
- Implementation-Version: 3.0.FR
- Implementation-Vendor: Apache Software Foundation
Tomcat configures the Jsessionid HttpOnly property
useHttpOnly Should the HttpOnly flag be set on session cookies to prevent client side script from accessing the session ID? Defaults to false.
useHttpOnlyShould the HttpOnly flag be set on session cookies to prevent client side script from accessing the session ID? Defaults to true.
From the documentation, tomcat6 and 5.5useHttpOnly are false by default, and 7 is true by default
Modify Tomcat / conf / Web xml
- <Context useHttpOnly="true"></context>
Most of the information on the Internet is only configured above, but it is not found in the actual measurement
- <session-config>
- <session-timeout>30</session-timeout>
- <cookie-config>
- <http-only>true</http-only>
- </cookie-config>
- </session-config>
Enable security for port 8080 and start the Tomcat access project. It is found that HttpOnly and secure properties have been started
- <Connector port="8080" protocol="HTTP/1.1"
- connectionTimeout="20000"
- redirectPort="8443" secure="true" />