【问题标题】:Java HttpOnly FlagJava HttpOnly 标志
【发布时间】:2014-07-21 13:17:38
【问题描述】:

我使用了 Servlet 3.0,我希望使用 HttpOnly 标志来保护我的 cookie。我的 web.xml 是

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">


    <session-config>
        <cookie-config>
            <http-only>true</http-only>
            <secure>true</secure>
        </cookie-config>
    </session-config>

</web-app>

我的 Servlet 是

response.setContentType("application/json");
PrintWriter pw = response.getWriter();

Cookie cookie = new Cookie("url", "google.com");
cookie.setMaxAge(60 * 60); //1 hour
response.addCookie(cookie);

pw.println("Cookies created");

我的 context.xml 是

<Context cookies="true" crossContext="true" useHttpOnly="true">
    <SessionCookie httpOnly="true"/>    
</Context>

但我可以从 Javascript 读取 cookie。有人可以帮帮我吗?

【问题讨论】:

  • 你在使用Tomcat吗?哪个版本?
  • 你的 context.xml 是什么?
  • 我使用的是JBOSS 7,但它相当大吗?
  • 是的,并非所有 Web 服务器的所有版本都实现 httponly
  • 据我所知 Jboss7 支持它。我已经更新了我的问题

标签: java servlets cookies httponly


【解决方案1】:

web.xml 只配置 session-cookie。

你应该添加

cookie.setHttpOnly(true);

到您的 Servlet。

【讨论】:

    猜你喜欢
    • 2011-05-18
    • 2012-10-13
    • 2011-12-29
    • 1970-01-01
    • 2018-02-19
    • 2015-07-21
    • 2013-04-02
    • 2015-06-14
    • 2013-04-20
    相关资源
    最近更新 更多