【发布时间】:2019-10-08 22:01:38
【问题描述】:
我们有一个在 Apache 和 nginx 上运行的 Angular 8 应用程序。我们使用带有.htaccess 和.htpasswd 的基本身份验证来保护此应用程序。
很遗憾,由于对服务器的请求被 Basic Auth 阻止,因此应用无法加载,并且 Angular 无法加载这些文件:
阻止 http://root/polyfills-es2015.js 请求凭据,因为它是跨域请求。
阻止 http://root/runtime-es2015.js 请求凭据,因为它是跨域请求。
加载资源失败:服务器响应状态为 401(需要授权)(runtime-es2015.js,第 0 行)
阻止 http://root/styles-es2015.js 请求凭据,因为它是跨域请求。
加载资源失败:服务器响应状态为 401(需要授权)(styles-es2015.js,第 0 行)
阻止 http://root/vendor-es2015.js 请求凭据,因为它是跨域请求。 阻止 http://root/main-es2015.js 请求凭据,因为它是跨域请求。
加载资源失败:服务器响应状态为 401(需要授权)(vendor-es2015.js,第 0 行)
加载资源失败:服务器响应状态为 401(需要授权)(main-es2015.js,第 0 行)
加载资源失败:服务器响应状态为 401(需要授权)(polyfills-es2015.js,第 0 行)
如果我们删除对.htpasswd 的引用,应用程序将按预期运行。
我尝试使用自定义 HTTP 拦截器:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const cloned = req.clone({
headers: req.headers.set('Authorization', 'Basic ' + btoa('user:password'))
});
return next.handle(cloned);
}
但看起来,这并没有用于 Angulars 的内部加载机制。
我们怎样才能让它发挥作用?
【问题讨论】:
标签: angular basic-authentication angular8 http-status-code-401