使用 Vite 代理的基本身份验证

Basic Auth using Vite proxy

我尝试使用 vite 通过基本身份验证连接到 URL。凭据正确,但会打开一个空的表单字段。

export default defineConfig({
  base: './',
  server: {
    proxy: {
      '/data': {
        target: 'https://user:password@example.com/foo',
        changeOrigin: true,
      }
    },
  }});

遗憾的是,这不起作用 - 我在这里找到了一个相关问题 https://serverfault.com/questions/371907/can-you-pass-user-pass-for-http-basic-authentication-in-url-parameters,但我不清楚是否仍支持此功能。

是否可以修改vite config中的请求头,直接在请求中注入凭据?

Vite服务器代理是http-proxyhttps://www.npmjs.com/package/http-proxy)。因此可以应用相同的配置设置。

export default defineConfig({
  base: './',
  server: {
    proxy: {
      '/data': {
        target: 'https://example.com/foo',
        changeOrigin: true,
        configure: (proxy, options) => {
          // proxy will be an instance of 'http-proxy'
          const username = 'username';
          const password = 'password';
          options.auth = `${username}:${password}`;
        },
      }
    },
  },
})