springboot 2.0前端跨域 靜態資源本機對應

來源:互聯網
上載者:User

標籤:resource   max   str   允許   整合   registry   space   mvcc   adapter   

由於在springboot 2.0 WebMvcAutoConfigurationAdapter 這個類成為了自動設定類 如果你要整合這個類 自動設定的許多屬性就會失效 也就是說此類已到期 你需要使用一下

介面來實現靜態資源的本機對應和前端跨域 等一系列功能 

@Configurationpublic 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前端跨域 靜態資源本機對應

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.