美文网首页
spring boot 项目 添加 跨域支持

spring boot 项目 添加 跨域支持

作者: wutianf | 来源:发表于2019-10-07 12:43 被阅读0次
    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.WebMvcConfigurer;
    
    /**
     * <p>Description: 自定义webmvc的配置 </p>
     *
     * @author 
     * @date 
     */
    @Configuration
    public class ResourceConfig implements WebMvcConfigurer {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("swagger-ui.html")
                    .addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");
            
        }
        
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowedOrigins("*")
                    .allowCredentials(true)
                    .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH")
                    .maxAge(3600);
        }
        
    }

    相关文章

      网友评论

          本文标题:spring boot 项目 添加 跨域支持

          本文链接:https://www.haomeiwen.com/subject/rkhmpctx.html