在tomcat中配置Content-Security-Policy

问题描述 投票:3回答:2

我读到了有关配置/实现Content-Security-Policy标头的信息,我遇到了两种方法:

  1. 使用在this链接中给出的实现Filter的自定义过滤器
  2. 使用元标记

请注意,这个问题与this不重复,Iam寻找比this链接更好的解决方案

我看到(1)的缺点是它是通过代码驱动的,而不是通过配置文件,选项(2)的缺点是如果我说100个html文件,我需要在每个HTML中放置这个标签吗? (如果我错了,请纠正我)我正在寻找的解决方案是我可以在web.xml中配置并适用于所有html文件。如果在web.xml中配置X-Frame-Options(如给定的here),我们采取的方式是不是我们在web.xml中配置Content-Security-Policy的方式不同?

security tomcat content-security-policy clickjacking
2个回答
1
投票

您是否尝试过使用https://github.com/sourceclear/headlines(死链接,这是我能找到的全部内容:https://github.com/stevespringett/headlines)?它的目标是使与安全相关的头文件成为配置问题,而不是像你问的代码。

{
  "XContentTypeConfig": {
    "enabled": true
  },

  "XFrameOptionsConfig": {
    "enabled": true,
    "value":"DENY"
  },

  "XssProtectionConfig": {
    "enabled": true
  },

  "HstsConfig": {
    "enabled": true,
    "includeSubdomains":true,
    "maxAge":31536000
  },

  "CspConfig": {
    "csp": {
      "default-src":["'self'"]
    },
    "cspReportOnly":{}
  },

  ... snip
}

0
投票

在web.xml中配置content-security-policy

您可以使用OWASP在链接https://svn.cesecore.eu/svn/ejbca/trunk/ejbca/modules/ejbca-common-web/src/org/owasp/filters/ContentSecurityPolicyFilter.java中提供的建议。它是一个可以在后端实现的Web过滤器。

然后必须在web.xml文件中定义以下过滤器。这会在您的应用程序中的每个请求上调用。在java中,您可以通过创建适当的类来实现。

<filter>
    <filter-name>ContentSecurityPolicy</filter-name>
    <filter-class>YourPackagePath.ContentSecurityPolicyFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>ContentSecurityPolicy</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

以上内容将在HTTP标头中实现content-security-policy的以下值

default-src'none'; style-src'self''unsafe-inline'; script-src'self''unsafe-inline''unsafe-eval'; img-src'self'; frame-src'self'; connect-src'self';形式行动'自我';反射-xss块

© www.soinside.com 2019 - 2024. All rights reserved.