springboot 2.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);//跨域允许时间
    }
}

猜你喜欢

转载自www.cnblogs.com/woshuyuqiang/p/9297324.html