gpt4 book ai didi

php - 使用本地 htaccess 文件 (Apache) 实现内容安全策略

转载 作者:行者123 更新时间:2023-12-02 00:41:41 25 4
gpt4 key购买 nike

我是网络开发新手,只想在某个网页上实现内容安全策略。

这是我到目前为止所做的: 1. 以这种方式设置标题:

Header set Content-Security-Policy "
default-src 'self';
script-src 'self';
"
  1. 此设置后出现 500 内部服务器错误。在某处阅读必须启用 mod_headers 的内容。所以启用它使用:
sudo a2enmod headers
sudo service apache2 restart
  1. .htaccess 现在看起来像这样:
<IfModule mod_headers.c>Header set Content-Security-Policy "

default-src 'self';

script-src 'self';

"</IfModule>

现在,问题是我现在没有收到任何错误,但 header 仍未设置。如果我遗漏了什么,请告诉我。我已经完成了其他线程并使用了以下整个路径: How to implement content security policy? 还有其他一些问题。P.S 我在 Ubuntu 14.04 上使用 Apache 和 PHP

最佳答案

对于任何想要使用换行符的人(并且您将要为真正复杂的 CSP 使用换行符),Apache 允许他们通过简单地以反斜杠 (\) 结束一行来指示命令继续下一行。例如(摘自 Hassan 的原帖):

<IfModule mod_headers.c>Header set Content-Security-Policy "\
\
default-src 'self'; \
\
script-src 'self'; \
\
"</IfModule>

请注意,反斜杠前的空格无关紧要;您可以在非空白字符后立即使用反斜杠,或者您可以添加一个或多个空格、制表符等,然后以斜杠结尾。空行必须包含反斜杠作为该行的最后一个字符。

引用:https://httpd.apache.org/docs/current/configuring.html

httpd configuration files contain one directive per line. The backslash "\" may be used as the last character on a line to indicate that the directive continues onto the next line. There must be no other characters or white space between the backslash and the end of the line.

关于php - 使用本地 htaccess 文件 (Apache) 实现内容安全策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46273726/

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