【问题标题】:Basic Auth using Vite proxy使用 Vite 代理的基本身份验证
【发布时间】:2022-01-18 23:48:32
【问题描述】:

我尝试通过基本身份验证连接到使用 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 中修改请求头,直接在请求中注入凭证?

【问题讨论】:

    标签: node.js authentication basic-authentication http-proxy vite


    【解决方案1】:

    Vite 服务器代理是http-proxy (https://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}`;
            },
          }
        },
      },
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 2018-07-19
      • 2015-08-17
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多