【问题标题】:How to use a Service Worker With BASIC Authentication (NTLM, Negotiate)如何使用具有基本身份验证的 Service Worker(NTLM,协商)
【发布时间】:2016-07-04 23:09:29
【问题描述】:

我一直在尝试在 IIS 托管网站中使用服务工作者来缓存网站的一些静态内容。该站点是使用 Windows 身份验证的内部应用程序。我已经能够毫不费力地注册和运行服务工作者,但是一旦我打开缓存并开始将文件添加到缓存中,promise 就会因授权失败而失败。返回的 HTTP 结果是 401 Unauthorized。这是前几个请求的通常响应,直到浏览器和服务器能够协商授权。

我会尽快发布一些有助于解释的代码。

编辑

var staticCacheName = 'app-static-v1';
console.log("I AM ALIVE");
this.addEventListener('install', function (event) {
    console.log("AND I INSTALLED!!!!");
    var urlsToCache = [
        //...many js files to cache
        '/scripts/numeral.min.js?version=2.2.0',
        '/scripts/require.js',
        '/scripts/text.js?version=2.2.0',
        '/scripts/toastr.min.js?version=2.2.0',
    ];


    event.waitUntil(
        caches.open(staticCacheName).then(function (cache) {
            cache.addAll(urlsToCache);
        }).catch(function (error) {
            console.log(error);
        })
    );
});

【问题讨论】:

  • 此问题/解决方案适用于任何使用 BASIC 身份验证的网站,如果更新标题,它可能会使更多的受众受益。
  • @SGD 你对这个标题有什么建议?类似“如何使用具有基本身份验证的 Service Worker”
  • 是的,这正是我真正要寻找的。​​span>

标签: ntlm service-worker http-negotiate


【解决方案1】:

这只是一个猜测,因为缺少代码,但如果你正在做类似的事情:

caches.open('my-cache').then(cache => {
  return cache.add('page1.html'); // Or caches.addAll(['page1.html, page2.html']);
});

当您将字符串传递给cache.add()/cache.addAll() 时,您正在利用隐含的Request object creation(参见第6.4.4.4.1 节)。创建的Request 对象使用默认的credentials mode,即'omit'

您可以做的是显式构造一个 Request 对象,其中包含您喜欢的凭据模式,在您的情况下可能是 'same-origin'

caches.open('my-cache').then(cache => {
  return cache.add(new Request('page1.html', {credentials: 'same-origin'}));
});

如果您有一堆 URL,您要将数组传递给 cache.addAll(),您可以将它们 .map() 传递给对应的 Requests 数组:

var urls = ['page1.html', 'page2.html'];
caches.open('my-cache').then(cache => {
  return cache.addAll(urls.map(url => new Request(url, {credentials: 'same-origin'})));
});

【讨论】:

  • 谢谢杰夫,我会检查一下。你已经准确地推测出我在做什么。我已将代码添加到问题中。
  • 非常感谢。这正是我所需要的!
猜你喜欢
  • 2015-01-10
  • 1970-01-01
  • 1970-01-01
  • 2014-08-18
  • 2013-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多