zoukankan      html  css  js  c++  java
  • Swagger跨域访问

    我们用springboot开发完后,需要前端vue用swagger跨域,默认是不能跨域的,所以需要我们后台设置跨域访问,将下面代码完整复制即可。   

    在springboot项目中新建class :

    CorsConfig.java

    完整跨域代码如下:

    @Configuration
    public class CorsConfig extends WebMvcConfigurerAdapter {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .maxAge(3600);
        }
        private CorsConfiguration buildConfig() {
            CorsConfiguration corsConfiguration = new CorsConfiguration();
            List<String> list = new ArrayList<>();
            list.add("*");
            corsConfiguration.setAllowedOrigins(list);
        /*
        // 请求常用的三种配置,*代表允许所有,当时你也可以自定义属性(比如header只能带什么,只能是post方式等等)
        */
            corsConfiguration.addAllowedOrigin("*");
            corsConfiguration.addAllowedHeader("*");
            corsConfiguration.addAllowedMethod("*");
            return corsConfiguration;
        }
        @Bean
        public CorsFilter corsFilter() {
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            source.registerCorsConfiguration("/**", buildConfig());
            return new CorsFilter(source);
        }
    }
    

      

     
  • 相关阅读:
    Resample the mask
    高斯消元法
    java中的大数BigInteger
    JAVA中如何使用SORT从大到小排
    Java中Array.sort()的几种用法简明教程 (需要初始化要排序的对象)
    java中两个字符串如何比较大小
    java数组初始化函数
    BestCoder Round #89 Fxx and string
    Flooded!
    基础练习 数列排序
  • 原文地址:https://www.cnblogs.com/xiaohouzai/p/8922041.html
Copyright © 2011-2022 走看看