zoukankan      html  css  js  c++  java
  • Vue应用请求SpringBoot API出现 CORS 跨域请求设置 Invalid CORS request错误

    1.全局配置

    在application.java文件添加CorsRegistry配置

    package com.ypnh.authority;
    
    import com.ypnh.authority.infrastructure.config.Config;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.netflix.feign.EnableFeignClients;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    @EnableConfigurationProperties({Config.class})
    @EnableEurekaClient
    @EnableFeignClients
    @SpringBootApplication
    public class AuthorityCenterApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(AuthorityCenterApplication.class, args);
        }
    
        //全局配置跨域
        @Bean
        public WebMvcConfigurer corsConfigurer() {
            return new WebMvcConfigurerAdapter() {
                @Override
                public void addCorsMappings(CorsRegistry registry) {
                    registry.addMapping("/users/*").allowedOrigins("http://localhost:9527");
                }
            };
        }
    
    
    }
    

      

    2.局部配置

    在具体方法添加@CrossOrigin(origins = “http://localhost:9527”)注解  

    @ApiOperation(value = "查询用户")
    @CrossOrigin(origins = "http://localhost:9527")
    @PostMapping(value = "/users", produces = "application/json;charset=UTF-8")
    public List<User> findAllUsers() {
    List<User> users = userService.findAll();
    return users;
    }



                                                                                                

  • 相关阅读:
    进程调度算法
    附近的人,附近的卖家(geohash+前缀树)
    海量信息库,查找是否存在(bloom filter布隆过滤器)
    继承、虚继承和虚函数表对类的大小的影响
    linux 用户空间与内核空间——高端内存详解
    0-1背包问题入门
    范式
    vue的无缝滚动插件vue-seamless-scroll的安装与使用
    在vue项目中使用swiper2.7.6
    vue项目在IE下报 [vuex] vuex requires a Promise polyfill in this browser问题
  • 原文地址:https://www.cnblogs.com/haciont/p/10784673.html
Copyright © 2011-2022 走看看