【问题标题】:Using Cordova 5.0.0 Google Maps with Content Security Policy使用带有内容安全策略的 Cordova 5.0.0 Google 地图
【发布时间】:2016-07-04 13:28:58
【问题描述】:

我正在使用 Cordova 5.0.0 构建适用于 Android 的 Cordova 应用程序。我正在使用没有插件的谷歌地图,它必须没有插件。 我使用白名单插件包含此脚本和元标记 .also Cordova。

  <!-- Meta tag -->
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
  
   <!-- Google Maps-->
 <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=true"></script>

它给了我这个错误:

 Refused to load the script 'https://maps.googleapis.com/maps/api/js?sensor=true' because it violates the following Content Security Policy directive: "script-src 'self' 'unsafe-inline' 'unsafe-eval'".

【问题讨论】:

  • @Nazir Dogan,以下任何一项对您有用吗?

标签: android google-maps cordova


【解决方案1】:

尝试使用以下内容安全策略:

<meta http-equiv="Content-Security-Policy" content="
        default-src 'self' data: gap:
            https://ssl.gstatic.com;
        script-src 'self' 'unsafe-inline' 'unsafe-eval'
            https://*.gstatic.com https://*.googleapis.com;
        style-src 'self' 'unsafe-inline';
        media-src *">

Google 地图需要为 script-src 定义的规则,如上面的 'unsafe-inline' 'unsafe-eval'。 ; )

【讨论】:

  • 这比公认的答案要好得多,因为它不会让 Content-Security-Policy 完全打开。您可能还需要将“maps.googleapis.commaps.google.com”添加到 script-src。
【解决方案2】:

试试这个块来消除这个错误

<meta http-equiv="Content-Security-Policy" content="
default-src 'self' data: gap:
    *;
script-src 'self' 'unsafe-inline' 'unsafe-eval'
    *;
style-src 'self' 'unsafe-inline' 'unsafe-eval'
    *;
media-src 'self' 'unsafe-inline' 'unsafe-eval'
    *;
img-src 'self' 'unsafe-inline' 'unsafe-eval'
    *;
connect-srv 'self' 'unsafe-inline' 'unsafe-eval'
    *
">

【讨论】:

  • 这不是一个好主意,因为它使内容安全策略完全开放,这有点像关闭防火墙而不是打开您需要的一个端口。请参阅下面 Italo Maia 的答案以获得更安全的答案。
【解决方案3】:

如果要允许导航其他 URL,则必须将 &lt;allow-navigation&gt; 标记添加到您的 config.xml 文件中。以下应该有效:

<access origin="*" />
<allow-navigation href="*" />
<allow-intent href="*" />

提醒一下,&lt;allow-intent&gt; 行将通过 HTTP 和 HTTPS 的整个网络列入白名单,这在生产环境中可能不是您想要的。您可以查看documentation 了解更多信息。否则,如果您有任何其他安全库,则可能必须删除它们,因为它们可能会阻止 Google 地图。

【讨论】:

  • 我忘了说这个,但我也加了这个。
  • 尝试删除并重新添加所有插件。您可能还需要更改脚本标记的位置。
  • 我做了你的推荐,但它给了我同样的结果。
【解决方案4】:

较早的帖子,但经过几次柚木树后找到了适合我的帖子。

<meta http-equiv="Content-Security-Policy" content="default-src 'self' data gap  https://*.google.com  https://*.googleapis.com  https://*.gstatic.com  https://*.googleusercontent.com 'unsafe-inline' 'unsafe-eval'; media-src *">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-03
    • 2018-02-14
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    • 2023-02-09
    相关资源
    最近更新 更多