前后端分类项目,关于跨域的解决

    技术2022-07-16  75

    新增一个配置类

    import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; @Configuration public class InterceptorConfig extends WebMvcConfigurationSupport { /** * 解决跨域问题 **/ @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**")// 允许跨域访问的路径 .allowedOrigins("*")// 允许跨域访问的源 .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")// 允许请求方法 .maxAge(168000)// 预检间隔时间 .allowedHeaders("*")// 允许头部设置 .allowCredentials(true);// 是否发送cookie } /** * 发现如果继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。 需要重新指定静态资源 * * @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("/webjars/**").addResourceLocations( "classpath:/META-INF/resources/webjars/"); super.addResourceHandlers(registry); } }
    Processed: 0.013, SQL: 10