跨域请求解决方法----不允许有多个 ‘Access-Control-Allow-Origin‘ CORS 头

后端配置了代码:

spring:
  application:
    name: spzx-server-gateway
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      discovery:
        locator:
          enabled: true
      globalcors:
        cors-configurations:
          '[/**]':
            allowedOriginPatterns: "*"
            # 允许请求中携带的头信息
            allowedHeaders: "*"
            # 运行跨域的请求方式
            allowedMethods: "*"
            # 跨域检测的有效期,单位s
            maxAge: 36000

但是后端启动后,前端报错'Access-Control-Allow-Origin' CORS 多个头

是因为多重配置导致:

比如说:

java 复制代码
package com.atguigu.spzx.product.controller;

import com.atguigu.spzx.model.entity.product.Category;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.h5.IndexVo;
import com.atguigu.spzx.product.service.CategoryService;
import com.atguigu.spzx.product.service.ProductSkuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "首页接口管理")
@RestController
@RequestMapping(value="/api/product/index")
@CrossOrigin   重复配置跨域
public class IndexController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ProductSkuService productSkuService;

    //查询所有一级分类
    //查询畅销商品
    @Operation(summary = "获取首页数据")
    @GetMapping
    public Result<IndexVo> findData(){
        //查询所有一级分类
        List<Category> categoryList=categoryService.findOneCategory();
        查询畅销商品
        List<ProductSku> productSkuList=productSkuService.findProductSkuBySale();
        IndexVo indexVo=new IndexVo();
        indexVo.setCategoryList(categoryList);
        indexVo.setProductSkuList(productSkuList);
        return Result.build(indexVo, ResultCodeEnum.SUCCESS);

    }
}

如上代码:注解重复配置跨域

再比如利用java代码配置导致前端报错'Access-Control-Allow-Origin' CORS 多个头:

java 复制代码
package com.atguigu.spzx.manager.config;



import com.atguigu.spzx.manager.properties.UserAuthProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Component
@SuppressWarnings({"all","silent"})
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Autowired
    private UserAuthProperties userAuthProperties;
    @Autowired

    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")       //添加路径规则
                .allowCredentials(true)   //是否允许在跨域情况下传递cookie
                .allowedOriginPatterns("*")//允许请求来源的域规则
                .allowedMethods("*")
                .allowedHeaders("*");           //允许所有请求头
    }


}
相关推荐
正小安1 小时前
如何在微信小程序中实现分包加载和预下载
前端·微信小程序·小程序
_.Switch2 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
一路向前的月光3 小时前
Vue2中的监听和计算属性的区别
前端·javascript·vue.js
长路 ㅤ   3 小时前
vite学习教程06、vite.config.js配置
前端·vite配置·端口设置·本地开发
长路 ㅤ   3 小时前
vue-live2d看板娘集成方案设计使用教程
前端·javascript·vue.js·live2d
编程、小哥哥3 小时前
netty之Netty与SpringBoot整合
java·spring boot·spring
Fan_web3 小时前
jQuery——事件委托
开发语言·前端·javascript·css·jquery
安冬的码畜日常3 小时前
【CSS in Depth 2 精译_044】第七章 响应式设计概述
前端·css·css3·html5·响应式设计·响应式
莹雨潇潇4 小时前
Docker 快速入门(Ubuntu版)
java·前端·docker·容器
Jiaberrr4 小时前
Element UI教程:如何将Radio单选框的圆框改为方框
前端·javascript·vue.js·ui·elementui