gpt4 book ai didi

Java HttpOnly 标志

转载 作者:行者123 更新时间:2023-11-30 08:23:00 30 4
gpt4 key购买 nike

我使用的是 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。谁能帮帮我?

最佳答案

web.xml 只配置session-cookie。

你应该添加

cookie.setHttpOnly(true);

到您的 Servlet。

关于Java HttpOnly 标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23977304/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com