zoukankan      html  css  js  c++  java
  • springboot跨域请求配置

    springboot跨域请求配置

    方式一:

    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;
    
    @Configuration
    public class CorsConfig {
        private CorsConfiguration buildConfig() {
            CorsConfiguration corsConfiguration = new CorsConfiguration();
            corsConfiguration.addAllowedOrigin("*"); // 允许任何域名使用
            corsConfiguration.addAllowedHeader("*"); // 允许任何头
            corsConfiguration.addAllowedMethod("*"); // 允许任何方法(post、get等)
            corsConfiguration.setAllowCredentials(true);
            return corsConfiguration;
        }
        
        @Bean
        public CorsFilter corsFilter() {
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            source.registerCorsConfiguration("/**", buildConfig()); // 对接口配置跨域设置
            return new CorsFilter(source);
        }
    }

    方式二:

    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.Ordered;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration //加配置注解可以扫描到
    public class WebConfig implements WebMvcConfigurer{
        
        //跨域请求配置
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            WebMvcConfigurer.super.addCorsMappings(registry);
            registry.addMapping("/**")// 对接口配置跨域设置
                    .allowedHeaders("*")// 允许任何头
                    .allowedMethods("POST","GET")// 允许方法(post、get等)
                    .allowedOrigins("*")// 允许任何域名使用
                    .allowCredentials(true);
        }
        
    }
  • 相关阅读:
    OpenCV教程(转自:浅墨_毛星云博客)
    基于聚类的“图像分割”(python)
    ubuntu同时装有MXNet和Caffe框架
    FCN用卷积层代替FC层原因(转)
    windows下安装TensorFlow(CPU版)
    python语法
    Deep Visualization:可视化并理解CNN(转)
    非局部均值(Nonlocal-Mean)
    转——深度学习之BN算法(Batch Normailization)
    数组和循环的应用
  • 原文地址:https://www.cnblogs.com/qiantao/p/13572043.html
Copyright © 2011-2022 走看看