SpringBoot如何解決跨域Cores問題
更新時間:2024年09月05日 08:44:36 作者:嵐殿
這篇文章主要介紹了SpringBoot如何解決跨域Cores問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
SpringBoot解決跨域Cores
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOriginPatterns("*") .allowedHeaders("*") .allowedMethods("*") .allowCredentials(true) .maxAge(3600); } }
SpringBoot允許跨域
解決辦法
@SpringBootApplication @MapperScan("com.humorchen.pastry_examination.mapper") public class PastryExaminationApplication implements WebMvcConfigurer { public static void main(String[] args) { SpringApplication.run(PastryExaminationApplication.class, args); } @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowCredentials(true) .allowedHeaders("*") .allowedOriginPatterns("*") .allowedMethods("*"); } }
新版本springboot跨域解決辦法,把這個配置bean注入就可以了
@Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurer() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedHeaders("*") .allowedMethods("*") .allowedOrigins("*") .allowCredentials(true); } }; }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
一文講透為什么遍歷LinkedList要用增強型for循環(huán)
這篇文章主要為大家介紹了為什么遍歷LinkedList要用增強型for循環(huán)的透徹詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04java 抓取網(wǎng)頁內(nèi)容實現(xiàn)代碼
這篇文章主要介紹了java 抓取網(wǎng)頁內(nèi)容實現(xiàn)代碼,需要的朋友可以參考下2014-02-02Java Serializable和Parcelable詳解及實例代碼
這篇文章主要介紹了Java Serializable和Parcelable詳解,并附實例代碼的相關(guān)資料,需要的朋友可以參考下2016-09-09