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
Add a ment  | 

2 Answers 2

Reset to default 7

It 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