HttpOnly Cookies是一个cookie安全行的解决方案。
在支持HttpOnly cookies的浏览器中(IE6+,FF3.0+),如果在Cookie中设置了”HttpOnly”属性,那么通过JavaScript脚本将无法读取到Cookie信息,这样能有效的防止XSS攻击,让网站应用更加安全。
但是J2EE4,J2EE5 的Cookie并没有提供设置 HttpOnly 属性的方法,所以如果需要设置HttpOnly属性需要自己来处理。
import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse; / * Cookie工具类 */ public class CookieUtil { / * 设置HttpOnly Cookie * @param response HTTP响应 * @param cookie Cookie对象 * @param isHTTPOnly 是否为HttpOnly */ public static void addCookie(HttpServletResponse response, Cookie cookie, boolean isHttpOnly) { String name = cookie.getName();//Cookie名称 String value = cookie.getValue();//Cookie值 int maxAge = cookie.getMaxAge();//最大生存时间(毫秒,0代表删除,-1代表与浏览器会话一致) String path = cookie.getPath();//路径 String domain = cookie.getDomain();//域 boolean isSecure = cookie.getSecure();//是否为安全协议信息 StringBuilder buffer = new StringBuilder(); buffer.append(name).append("=").append(value).append(";"); if (maxAge == 0) { buffer.append("Expires=Thu Jan 01 08:00:00 CST 1970;"); } else if (maxAge > 0) { buffer.append("Max-Age=").append(maxAge).append(";"); } if (domain != null) { buffer.append("domain=").append(domain).append(";"); } if (path != null) { buffer.append("path=").append(path).append(";"); } if (isSecure) { buffer.append("secure;"); } if (isHttpOnly) { buffer.append("HTTPOnly;"); } response.addHeader("Set-Cookie", buffer.toString()); } }
值得一提的是,Java EE 6.0 中 Cookie已经可以设置HttpOnly了,所以如果是兼容 Java EE 6.0 的容器(例如如 Tomcat 7),可以直接使用Cookie.setHttpOnly 的方法来设置HttpOnly:
cookie.setHttpOnly(true);
在Servlet 3.0中增加对Cookie(请注意,这里所说的Cookie,仅指和Session互动的Cookie,即人们常说的会话Cookie)较为全面的操作API。最为突出特性:支持直接修改Session ID的名称(默认为“JSESSIONID”),支持对cookie设置HttpOnly属性以增强安全,避免一定程度的跨站攻击。防止脚本攻击,禁止了通过脚本获取cookie信息,浏览器不会将其发送给任何第三方
setName(String name) 修改Session ID的名称,默认为"JSESSIONID" setDomain(String domain) 设置当前Cookie所处于的域 setPath(String path) 设置当前Cookie所处于的相对路径 setHttpOnly(boolean httpOnly) 设置是否支持HttpOnly属性 setSecure(boolean secure) 若使用HTTPS安全连接,则需要设置其属性为true setMaxAge(int maxAge) 设置存活时间,单位为秒
如何使用呢,很方便,在ServletContextListener监听器初始化方法中进行设定即可;下面实例演示如何修改”JSESSIONID”,以及添加支持HttpOnly支持:
全局设置Session-Cookie相交互部分属性 @WebListener public class SessionCookieInitialization implements ServletContextListener { private static final Log log = LogFactory .getLog(SessionCookieInitialization.class); public void contextInitialized(ServletContextEvent sce) { log.info("now init the Session Cookie"); ServletContext servletContext = sce.getServletContext(); SessionCookieConfig sessionCookie = servletContext .getSessionCookieConfig(); sessionCookie.setName("YONGBOYID"); sessionCookie.setPath(servletContext.getContextPath()); sessionCookie.setHttpOnly(true); sessionCookie.setSecure(false); log.info("name : " + sessionCookie.getName() + "\n" + "domain:" + sessionCookie.getDomain() + "\npath:" + sessionCookie.getPath() + "\nage:" + sessionCookie.getMaxAge()); log.info("isHttpOnly : " + sessionCookie.isHttpOnly()); log.info("isSecure : " + sessionCookie.isSecure()); } public void contextDestroyed(ServletContextEvent sce) { log.info("the context is destroyed !"); } }
YONGBOYID=601A6C82DB175A4FD5376EA; JSESSIONID=AA78738AB1EAD1F9C649F705EC64D92D; AJSTAT_ok_times=6; JSESSIONID=abcpxyJmIpBVz6WHVo_1s; BAYEUX_BROWSER=439-1vyje1gmqt8y8giva7pqsu1
<Context useHttpOnly="true", sessionCookieName="YONGBOYID", sessionCookieDomain="/servlet3" … > ... </Context>
<div style="margin: 40px; paddding: 10px"> <div><a href="sessionCookieTest">正常连接</a></div> <div><a href="<%=response.encodeURL("sessionCookieTest") %>">重定向连接</a></div> </div>
会被重写的URL地址类似于:
http://localhost/servlet3/sessionCookieTest;YONGBOYID=19B94935DE49C9E69F5B6
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/233381.html原文链接:https://javaforall.net