在tomcat中配置Content-Security-Policy

vsp*_*vsp 5 security tomcat clickjacking content-security-policy

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

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

请注意,这个问题是不能重复的这个,荫寻找一个更好的解决方案比在给定的这个链接

我看到(1)中的缺点是通过代码驱动的,而不是通过配置文件驱动的,选项(2)中的缺点是,如果我说有100个html文件,则需要在每个HTML中放入此标记吗?(如果我错了,请纠正我)我正在寻找的解决方案是可以在web.xml中配置的东西,并且适用于所有html文件。这是我们在web.xml中配置X-框架,选项的情况下做这样给出的方式在这里,我们不是已经在web.xml配置内容安全,政策的类似的方式?

RIC*_*HAM 7

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

您可以使用OWASP 此处提供的建议。它是一个可以在后端实现的网络过滤器。

然后必须在您的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>
Run Code Online (Sandbox Code Playgroud)

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

默认-src '无'; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src '自我'; frame-src '自我'; 连接-src '自我'; 形式动作“自我”;反射 xss 块


ore*_*ake 3

您是否尝试过使用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
}
Run Code Online (Sandbox Code Playgroud)