在JSF应用程序中使所有cookie安全

发布于 2025-02-13 15:51:16 字数 467 浏览 0 评论 0原文

如何在Java Web / JSF应用程序中使所有Cookies安全?

我在Web.xml中添加了以下行,以使JSessionId安全和HTTPonly。但是,其他饼干并不安全。

<session-config>
    <session-timeout>
        15
    </session-timeout>
    <cookie-config>
        <http-only>true</http-only>
        <secure>true</secure>
        <max-age>6000</max-age>
    </cookie-config>
</session-config>

如果可以通过更改web.xml文件来实现这一点,那将很棒。

How can I make all cookies secure in a Java Web / JSF Application?

I added the following lines to web.xml to make JSESSIONID secure and httpOnly. Yet, the other cookies are not secure.

<session-config>
    <session-timeout>
        15
    </session-timeout>
    <cookie-config>
        <http-only>true</http-only>
        <secure>true</secure>
        <max-age>6000</max-age>
    </cookie-config>
</session-config>

If that can be achieved by changing the web.xml file, that will be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぃ弥猫深巷。 2025-02-20 15:51:16

cookie可以由您的代码或您使用的框架代码任意创建并附加到响应上。根据 servlet api 。您可以使用cookie.setsecure()方法将此标志设置为true对于任何特定的cookie,但是没有办法强制执行此标志以始终为true。您必须在每个特定情况下解决此问题。

A cookie can be arbitrarily created and attached to the response by your code or by the code of the frameworks that you use. The default value of the secure flag is false, according to the servlet API. You can use Cookie.setSecure() method to set this flag to true for any particular cookie, but there's no way to enforce this flag to always be true. You'd have to solve this issue for every particular case.

别忘他 2025-02-20 15:51:16

我认为上面选择的答案对您的要求是正确的。的确,您可以使用该方法创建一个安全的cookie,但是您询问了会话cookie,这不是使用他描述的方法创建的。

不过,您拥有的配置是正确的,但仅当您的容器支持它时才有效。

  • 您必须处于最近的tomcat(7.x,8.x,9.x等)的谱系中,
  • 您必须在谱系中使用最新版本(在撰写本文时9.0.64)
  • 您的web.xml必须声明/使用支持的模式。您只发布了Web.xml的片段。我检查了Web-common_3_0.xsd,它在其中包含仅HTTP和安全的元素。因此,您必须使用Web.xml模式为3.0或更高:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  version="4.0">

我在最新版本的Tomcat 9上对此进行了测试,并且cookie的生产正确。

I don't believe this selected answer above is correct for what you're asking. It's true that you can create a secure cookie using that method, but you're asking about the session cookie, which is not created using the method he describes.

The configuration you have is correct though, but only works if your container supports it.

  • You must be on a recent lineage of tomcat (7.x, 8.x, 9.x, etc)
  • You must use the latest version in the lineage (9.0.64 at the time of this writing)
  • Your web.xml must declare/use a supported schema. You only posted a snippet of your web.xml. I checked the web-common_3_0.xsd and it has the http-only and secure elements in it. As such, you must use web.xml schema of 3.0 or greater:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  version="4.0">

I tested this locally on the most recent version of Tomcat 9 and the cookie was produced correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文