这个插件可以让我们实现对应用程序白名单导览策略。当我们创建新的 Cordova 项目,白名单插件默认安装并执行。您可以打开config.xml文件看看 Cordova提供 allow-intent 默认设置。
导航白名单
在简单的例子,下面我们允许链接到一些外部URL。此代码放置在 config.xml 中。导航到 file:// URL 默认是允许的。
<allow-navigation href = "http://example.com/*" />
星号符号,*,用于允许导览到多个值。 在上面的例子中,我们允许导览到 example.com 所有子域。同样可以应用到协议或前缀到主机。
<allow-navigation href = "*://*.example.com/*" />
意图白名单
还有它用allow-intent 元素来指定哪些网址系统可以打开 。可以在config.xml中看到 Cordova 已经允许对我们来说需要的一些链接。
网络请求白名单
当看到config.xml文件里面,有 <access origin="*" />元素。这个元素允许应用程序通过 Cordova 钩子所有的网络请求。 如果你想只允许特定的请求,可以从 config.xml 中删除,并自行设置。
如前面实例相同的原理。
<access origin = "http://example.com" />
这将允许来自所有网络请求 http://example.com.
内容安全策略
可以看到当前的安全策略在应用程序的 index.htmll 中头元素中。
<meta http-equiv = "Content-Security-Policy" content = "default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
这是默认配置。如果你想允许一切从同一来源和 example.com 可以使用 −
<meta http-equiv = "Content-Security-Policy" content = "default-src 'self' foo.com">
还可以允许的一切,但限制CSS和JavaScript相同的源。
<meta http-equiv = "Content-Security-Policy" content = "default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
由于这是初学者教程中,我们推荐的默认Cordova 选项。当熟悉了Cordova ,你可以尝试设置一些不同的值。
上一篇:
Cordova振动
下一篇:无