admin管理员组文章数量:1291004
When I run the following JavasScript, I can successfully log in but not access the modules. How can I pass the authentication to them?
Sample Code
<DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script type="module">
import * as mymodule from "./js/mymodule.js";
mymodule.runme();
</script>
</body>
</html>
Opening this with a .htaccess with basic authentication results in GET
[...]mymodule.js [HTTP/1.1 401 Authorization Required 1ms]
on Firefox 54 (dom.moduleScripts.enabled, it works without .htaccess).
.htaccess
AuthType Basic
AuthName "Internal Area"
AuthUserFile /opt/.../.htpasswd
Require valid-user
When I run the following JavasScript, I can successfully log in but not access the modules. How can I pass the authentication to them?
Sample Code
<DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script type="module">
import * as mymodule from "./js/mymodule.js";
mymodule.runme();
</script>
</body>
</html>
Opening this with a .htaccess with basic authentication results in GET
[...]mymodule.js [HTTP/1.1 401 Authorization Required 1ms]
on Firefox 54 (dom.moduleScripts.enabled, it works without .htaccess).
.htaccess
AuthType Basic
AuthName "Internal Area"
AuthUserFile /opt/.../.htpasswd
Require valid-user
Share
Improve this question
edited Jan 4, 2018 at 15:58
Konrad Höffner
asked Jun 23, 2017 at 11:10
Konrad HöffnerKonrad Höffner
12.2k19 gold badges73 silver badges133 bronze badges
2 Answers
Reset to default 7It seems that script with type="module"
does not fetch the javascript file with the required credentials data to authenticate the user by the server.
Therefore you get HTTP/1.1 401 Authorization Required
To solve this issue, You can add crossorigin
attribute to the script tag:
<script type="module" crossorigin src="./js/mymodule.js"></script>
<script type="module">
import * as mymodule from "./js/mymodule.js";
mymodule.runme();
</script>
This will inform the browser to make "credentialed" request (request which aware of HTTP cookies and HTTP Authentication information).
It seems that that the HTML is cached by Firefox and you do not authenticate to the server.
You can try the following:
Prevent html file caching. Add to .htaccess :
<filesMatch "\.(html)$">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Sat, 01 Jan 2000 00:00:00 GMT"
</ifModule>
</filesMatch>
use PHP extension for instead of HTML
本文标签: Javascript ES6 modules not passing along htaccess basic authenticationStack Overflow
版权声明:本文标题:Javascript ES6 modules not passing along .htaccess basic authentication - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741521265a2383198.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论