我读到了有关配置/实现 Content-Security-Policy 标头的信息,并且遇到了两种执行此操作的方法:
- 使用实现 Filter 的自定义过滤器,如下所示this https://stackoverflow.com/questions/37306300/can-tomcat-7-be-configured-to-insert-content-security-policy-http-header link
- 使用元标记
请注意,这个问题不重复this https://stackoverflow.com/questions/37306300/can-tomcat-7-be-configured-to-insert-content-security-policy-http-header,我正在寻找比给出的更好的解决方案this https://stackoverflow.com/questions/37306300/can-tomcat-7-be-configured-to-insert-content-security-policy-http-header link
我看到(1)中的缺点是它通过代码驱动,而不是通过配置文件,选项(2)中的缺点是如果我有100个html文件,我需要在每个HTML中放置这个标签? (如我错了请纠正我)
我正在寻找的解决方案是我可以在 web.xml 中配置并适用于所有 html 文件的解决方案。在像给定的 web.xml 中配置 X-Frame-Options 时我们所做的事情here https://stackoverflow.com/questions/34991226/xframe-option-in-tomcat-7,我们在 web.xml 中没有类似的配置 Content-Security-Policy 的方法吗?
在 web.xml 中配置内容安全策略
您可以使用以下提供的建议OWASP
here https://owasp.org/www-community/controls/Content_Security_Policy。它是一个可以在后端实现的网络过滤器。
然后必须在您的中定义以下过滤器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 标头中实现以下内容安全策略值
默认 src '无'; style-src 'self' '不安全内联'; script-src 'self' '不安全内联' '不安全评估'; img-src '自我';框架-src'自我';连接-src'自我';形式-动作“自我”;反射 xss 块
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)