未通过身份验证发送脚本请求 header

Script request not being sent with Authentication header

我有一个包含基本身份验证的页面。一旦我验证了我对页面的请求,它的资源(CSS 等)就有了身份验证 header

Authorization   Basic NOTREALNOTREALNOTREALNOTREALNOTREAL=

在 body 结束时,我正在加载外部脚本。

<script type="module" src="/main.js"></script>

此请求未通过身份验证发送 header。这是页面上唯一没有的东西。这会导致 401 响应,其中包含以下 header

WWW-Authenticate    Basic realm="mydomain.com"

这在 chrome 和 firefox 中都会发生。

有谁知道为什么会这样或如何发送?

Looks like this is a quirk of <script type="module">,这导致它们都被视为 CORS 请求,因此默认情况下它们不发送身份验证 headers。

这可以通过将 crossorigin="use-credentials" 属性添加到您的脚本标签来解决:

<script type="module" src="/main.js" crossorigin="use-credentials"></script>

应该可以。