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); // } }