我试图在开发的早期阶段从休闲窥探的眼睛保护网站.通过HTTPS的基本身份验证似乎是一个合理的解决方案,但服务工作者的存在似乎阻止它在Chrome中工作.如果已经安装了serviceworker,但浏览器没有对所需领域的主动授权,则会发生这种情况.
Chrome显示网络时间轴中的响应为401
并且还显示浏览器选项卡正在接收正确的响应标头:
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="My realm"
Date: Tue, 21 Jun 2016 03:09:35 GMT
Connection: close
Cache-Control: no-cache
Run Code Online (Sandbox Code Playgroud)
但它没有提示登录,它只显示401响应的内容正文.
这是Chrome错误,还是我的ServiceWorker可能有问题?
javascript google-chrome basic-authentication service-worker
在页面导航触发的获取处理程序中,我尝试这样做:
return event.respondWith(new Response('Hello!', {
headers: {
"Set-Cookie": "TestCookie=foo; path=/; Max-Age=60;"
"TestHeader": "foo"
}
}));
Run Code Online (Sandbox Code Playgroud)
然后我在浏览器中加载任何 URL,并得到“你好!” 身体。在 Chrome devtools 中,我TestHeader在网络面板中看到了该集合。但是 cookie 没有显示在网络面板中,也没有显示在应用程序 > Cookie 查看器中。 document.cookie也无法生产。
该请求是由页面导航发起的,因此没有机会设置credentials: "include"从浏览器选项卡的提取。
是否可以在 ServiceWorker 的响应中添加 cookie?如果没有,是否可以以任何其他方式编写 cookie?