zoukankan      html  css  js  c++  java
  • springboot跨域问题解决

    package com.qif.xdqdm.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.cors.CorsConfiguration;
    import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
    import org.springframework.web.filter.CorsFilter;
    
    
    /**
     * @author
     * @date 2019年3月19日16:40:48
     */
    
    @Configuration
    public class CorsConfigFilter {
        private CorsConfiguration buildConfig() {
            CorsConfiguration corsConfiguration = new CorsConfiguration();
            //允许任何域名
            corsConfiguration.addAllowedOrigin("*");
            //允许任何头
            corsConfiguration.addAllowedHeader("*");
            //允许任何方法
            corsConfiguration.addAllowedMethod("*");
            return corsConfiguration;
        }
    
        @Bean
        public CorsFilter corsFilter() {
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            //注册
            source.registerCorsConfiguration("/**", buildConfig());
    
            return new CorsFilter(source);
        }
    
    
    
    }
    package com.qif.xdqdm.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    
    @Configuration
    public class MyConfig extends WebMvcConfigurerAdapter {
    
        //所有的WebMvcConfigurerAdapter组件都会一起起作用
        @Bean //将组件注册在容器  设置初始页面为index
        public WebMvcConfigurerAdapter webMvcConfigurerAdapter() {
            WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
                @Override
                public void addViewControllers(ViewControllerRegistry registry) {
                    registry.addViewController("/").setViewName("index");
                }
            };
            return adapter;
        }
    
        //解决跨域
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**");
        }
    
    
    
    }
  • 相关阅读:
    通过USB转TTL串口下载stm32程序
    实验一:基于STM32F1的流水灯实验(库函数)
    opencv 常用头文件介绍
    OpenCV 1.0在VC6下安装与配置(附测试程序)
    在Angularjs使用中遇到的那些坑
    js和ts关于遍历的几个小总结
    angularjs的启动方式
    关于跨域和如何解决跨域问题的小结
    TypeScript(入门)
    截取字符串部分汇总
  • 原文地址:https://www.cnblogs.com/MagicAsa/p/11303334.html
Copyright © 2011-2022 走看看