如何修复 Vue devServer 代理到具有 Windows 身份验证的 Web API,不一致地收到 401 错误?

问题描述 投票:0回答:1

我正在开发一个 vue 3 Intranet 应用程序,该应用程序与使用 Windows 身份验证的 Web api 进行通信。在生产中,vue 应用程序和 api 都托管在同一服务器上的 IIS 上,api 是 vue 应用程序下的应用程序。

问题是当我在本地托管它时,我有一个用于 api 的 iis express 实例和一个用于 vue 接口的 nodeJS 实例。为了模仿生产,我在 vue.config 中设置了一个 devServer 代理。

`

const https = require('https');
const HttpsAgent = require('agentkeepalive').HttpsAgent;

const fs = require('fs')
const path = require('path')

const baseFolder =
    process.env.APPDATA !== undefined && process.env.APPDATA !== ''
        ? `${process.env.APPDATA}/ASP.NET/https`
        : `${process.env.HOME}/.aspnet/https`;

const certificateArg = process.argv.map(arg => arg.match(/--name=(?<value>.+)/i)).filter(Boolean)[0];
const certificateName = certificateArg ? certificateArg.groups.value : "OLBMonitorWeb";

if (!certificateName) {
    console.error('Invalid certificate name. Run this script in the context of an npm/yarn script or pass --name=<<app>> explicitly.')
    process.exit(-1);
}

const certFilePath = path.join(baseFolder, `${certificateName}.pem`);
const keyFilePath = path.join(baseFolder, `${certificateName}.key`);

module.exports = {
    devServer: {
        https: {
            key: fs.readFileSync(keyFilePath),
            cert: fs.readFileSync(certFilePath),
        },
        proxy: {
            '/webapi': {
                target: 'https://localhost:44394',
                logLevel: "debug",
                changeOrigin: true,
                agent: new HttpsAgent({
                    maxSockets: 100,
                    keepAlive: true,
                    maxFreeSockets: 10,
                    keepAliveMsecs: 100000,
                    timeout: 6000000,
                    freeSocketTimeout: 90000
                }),
                onProxyRes: proxyRes => {
                    var key = 'www-authenticate';
                    proxyRes.headers[key] = proxyRes.headers[key] && proxyRes.headers[key].split(',');
                },
                pathRewrite: { '^/webapi': '' }
            }
        },
        webSocketServer: 'ws',
        port: 5002,
        host: '0.0.0.0',
        hot: true
    }
}

`

它有效,但不可靠。有时我可以正常浏览网站,有时我必须反复重新加载页面才能克服 401 错误。

如果我输入我的凭据,有时(很少)我会通过。通常我的帐户会被锁定,并且必须重置密码,所以我什至不再尝试。我尝试过调整 devserver 代理的时间,但这似乎只会在我成功通过身份验证后延迟下一个 401 错误。除此之外,我还尝试过对 devServer 设置进行各种重新安排,关闭 McAfee,但我不确定还有什么,因为这种情况已经持续了几个月。据我和我的同事所知,这应该可行。但是,显然我们错过了一些东西......

asp.net-web-api vuejs3 webpack-dev-server
1个回答
0
投票

这是一个令人不满意的答案,但解决方案是更改公司的防火墙。

我们之前使用代理来处理公司的网络流量,但现在不使用了。这在某种程度上解决了问题。我的网络部门有很多人离开时没有提出任何解决方案,甚至没有提出为什么会出现这样的情况。

© www.soinside.com 2019 - 2024. All rights reserved.