首页 > 编程语言 > 详细

springboot 2.0前端跨域 静态资源本地映射

时间:2018-07-12 00:14:05      阅读:474      评论:0      收藏:0      [点我收藏+]

由于在springboot 2.0 WebMvcAutoConfigurationAdapter 这个类成为了自动配置类 如果你要集成这个类 自动配置的许多属性就会失效 也就是说此类已过期 你需要使用一下

接口来实现静态资源的本地映射和前端跨域 等一系列功能 

@Configuration
public class WebAppConfig implements WebMvcConfigurer {

    /*
    * 静态资源本地映射
    * img是虚拟路径
    * 映射到本地D盘javaspace下的tomcatpath下
    * 浏览器访问:localhost:8080/img/xxx文件
    * */
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/**")
                .addResourceLocations("file:D:/javaspace/tomcatPath/")
                .setCachePeriod(31556926);
    }
    //前端跨域
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")//设置允许跨域的路径
                .allowedOrigins("*")//设置允许跨域请求的域名
                .allowCredentials(true)//是否允许证书 不再默认开启
                .allowedMethods("GET", "POST", "PUT", "DELETE")//设置允许的方法
                .maxAge(3600);//跨域允许时间
    }
}

 

springboot 2.0前端跨域 静态资源本地映射

原文:https://www.cnblogs.com/woshuyuqiang/p/9297324.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!