跨域问题- Access-Control-Allow-Origin报错

Blade 未结 1 230
zhoushr
zhoushr 2024-08-26 10:41

一、该问题的重现步骤是什么?

1. 我下载SpringBoot 3.2.7免费版,前端SABER3-X

2. 运行后,老是报错

报错内容:

Access to XMLHttpRequest at 'http://192.168.0.43:8683/blade-auth/captcha' from origin 'http://192.168.0.43:2888' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.



SpringBoot 服务端地址 :

http://192.168.0.43:8683


saber3-x配置如下:


VITE_APP_API= 'http://192.168.0.43:8683'


 return defineConfig({

    base: VITE_APP_BASE,

    server: {

      host: "192.168.0.43",

      port: 2888,

      proxy: {

        '/api': {

          target: 'http://192.168.0.43',

          changeOrigin: true,

          rewrite: (path) => path.replace(/^\/api/, '')

        }

      }

    },

    resolve: {

      alias: {

        '~': resolve(__dirname, './'),

        "@": resolve(__dirname, "./src"),

        "components": resolve(__dirname, "./src/components"),

        "styles": resolve(__dirname, "./src/styles"),

        "utils": resolve(__dirname, "./src/utils"),

      }

    },

    plugins: createVitePlugins(env, command === 'build'),



不知道为什么?

1条回答
  •  admin
    admin (楼主)
    2024-08-26 10:42

    server: {
     port: 2888,
     proxy: {
       '/api': {
         target: 'http://192.168.0.43:8683',
         changeOrigin: true,
         rewrite: (path) => path.replace(/^\/api/, '')
       }
     }
    },

    反向代理的target要配置好后端接口的ip地址+端口,端口是不能忽略的

    还有VITE_APP_API是作为反向代理前缀使用的,不要改掉配置,保留原样

    VITE_APP_API=/api


    跟着公开课走一遍,里面有完整的运行流程:https://space.bilibili.com/525525/channel/seriesdetail?sid=2740449

    先按照公开课同样的配置跑,跑成功了再改成自定义的其他配置

    0 讨论(0)
提交回复