• SpringBoot+SpringSecurity 跨域


    package org.sang;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    
    @Configuration
    public class MyWebMvcConfig implements WebMvcConfigurer {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/book/**")
                    .allowedHeaders("*")
                    .allowedMethods("*")
                    .maxAge(1800)
                    .allowedOrigins("http://localhost:8081");
        }
    }
    @Configuration
    public class WebMvcConfig implements WebMvcConfigurer {
    
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowCredentials(true)
                    .allowedOrigins("*")
                    .allowedMethods("GET", "POST", "OPTIONS")
                    .maxAge(3600);
        }
    }
    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 {
      @Bean
      public CorsFilter corsFilter() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.setAllowCredentials(Boolean.TRUE);
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(urlBasedCorsConfigurationSource);
      }
    }

    No mapping for GET /swagger-ui.html报错

    配置类继承了WebMvcConfigurationSupport,则在配置文件在中配置的相关内容会失效,需要重新指定静态资源
    需要重新指定swagger静态资源或改成实现WebMvcConfigurer就可以了

     /**
         * 发现如果继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。 需要重新指定静态资源
         *  跨域配置后swagger2可能不能访问,需要增加如下配置
         * @param registry
         */
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/**")
                    .addResourceLocations( "classpath:/static/");
            registry.addResourceHandler("swagger-ui.html")
                    .addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("doc.html")
                    .addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");
            super.addResourceHandlers(registry);
        }

    @Configuration
    @EnableWebMvc
    public class CorsConfig implements WebMvcConfigurer{
        /*
         * 这里主要为了解决跨域问题,所以重写addCorsMappings方法
         */
    //    @Override
    //    public void addCorsMappings(CorsRegistry registry) {
    //        registry.addMapping("/**")
    //                .allowedOrigins("*")
    //                .allowedMethods("*")
    //                .allowedHeaders("*")
    //                .exposedHeaders("access-control-allow-headers",
    //                        "access-control-allow-methods",
    //                        "access-control-allow-origin",
    //                        "access-control-max-age",
    //                        "X-Frame-Options")
    //                .allowCredentials(true).maxAge(3600);
    //        super.addCorsMappings(registry);
    //    }
    
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowCredentials(true)
    //                .allowedOrigins("*")
                    .allowedOriginPatterns("*")
                    .allowedMethods("GET", "POST", "OPTIONS")
                    .maxAge(3600);
        }
    
    //    @Bean
    //    public CorsFilter corsFilter() {
    //        CorsConfiguration corsConfiguration = new CorsConfiguration();
    //        corsConfiguration.setAllowCredentials(Boolean.TRUE);
    //        corsConfiguration.addAllowedOrigin("*");
    //        corsConfiguration.addAllowedHeader("*");
    //        corsConfiguration.addAllowedMethod("*");
    //        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
    //        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
    //        return new CorsFilter(urlBasedCorsConfigurationSource);
    //    }
    
    //    @Override
    //    public void addCorsMappings(CorsRegistry registry) {
    //        // 设置允许跨域的路径
    //        registry.addMapping("/**")
    //                // 设置允许跨域请求的域名
    //                .allowedOrigins("*")
    //                // 是否允许证书 不再默认开启
    //                .allowCredentials(true)
    //                // 设置允许的方法
    //                .allowedMethods("*")
    //                // 跨域允许时间
    //                .maxAge(3600);
    //    }
    
    
    
    //    /**
    //     * 发现如果继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。 需要重新指定静态资源
    //     *  跨域配置后swagger2可能不能访问,需要增加如下配置
    //     * @param registry
    //     */
    //    @Override
    //    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    //        registry.addResourceHandler("/**")
    //                .addResourceLocations( "classpath:/static/");
    //        registry.addResourceHandler("swagger-ui.html")
    //                .addResourceLocations("classpath:/META-INF/resources/");
    //        registry.addResourceHandler("doc.html")
    //                .addResourceLocations("classpath:/META-INF/resources/");
    //        registry.addResourceHandler("/webjars/**")
    //                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    //        super.addResourceHandlers(registry);
    //    }
    
    }
  • 相关阅读:
    C++开发系列-友元函数 友元类
    C++开发系列-C语言的malloc与C++的new分配空间
    C++开发系列-内联函数
    iOS开发系列-Foundation与CoreFoundation内存管理
    C开发系列-字符串
    C开发系列-数组
    列表 元组 字典
    神奇的print
    while 语句的逻辑
    <Web Crawler><Java><thread-safe queue>
  • 原文地址:https://www.cnblogs.com/mingforyou/p/14633615.html
Copyright © 2020-2023  润新知