SpringBoot解決跨域問(wèn)題小結(jié)
SpringBoot解決跨域問(wèn)題
遇到前端跨域訪問(wèn)問(wèn)題,類似于這樣的:
在Springboot項(xiàng)目里加上這個(gè)配置文件CorsConfig.java
,重啟之后即可實(shí)現(xiàn)跨域訪問(wèn),前端無(wú)需再配置跨域。
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; @Configuration public class CorsConfig { // 當(dāng)前跨域請(qǐng)求最大有效時(shí)長(zhǎng)。這里默認(rèn)1天 private static final long MAX_AGE = 24 * 60 * 60; @Bean public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.addAllowedOrigin("*"); // 1 設(shè)置訪問(wèn)源地址 corsConfiguration.addAllowedHeader("*"); // 2 設(shè)置訪問(wèn)源請(qǐng)求頭 corsConfiguration.addAllowedMethod("*"); // 3 設(shè)置訪問(wèn)源請(qǐng)求方法 corsConfiguration.setMaxAge(MAX_AGE); source.registerCorsConfiguration("/**", corsConfiguration); // 4 對(duì)接口配置跨域設(shè)置 return new CorsFilter(source); } }
注意,加完這個(gè)配置類后一定要 重啟后臺(tái)
?。。?/strong>
Spring Boot 中實(shí)現(xiàn)跨域的方式匯總
前言
在現(xiàn)代Web應(yīng)用中,由于安全性和隱私的考慮,瀏覽器限制了從一個(gè)域向另一個(gè)域發(fā)起的跨域HTTP請(qǐng)求。解決這個(gè)問(wèn)題的一種常見方式是實(shí)現(xiàn)跨域資源共享(CORS)。Spring Boot提供了多種方式來(lái)處理跨域請(qǐng)求,本文將介紹其中的幾種方法。
1. 使用@CrossOrigin注解
Spring Boot提供了一個(gè)注解@CrossOrigin
,可以直接應(yīng)用于控制器類或方法上,以聲明允許跨域請(qǐng)求的配置。例如:
@RestController @CrossOrigin(origins = "http://localhost:3000") public class MyController { // Controller methods }
這種方法簡(jiǎn)單明了,但可能不夠靈活,特別是當(dāng)需要配置更多的跨域選項(xiàng)時(shí)。
2. 使用WebMvcConfigurer配置
通過(guò)實(shí)現(xiàn)WebMvcConfigurer
接口,可以進(jìn)行更細(xì)粒度的跨域配置。下面是一個(gè)例子:
@Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/api/**") .allowedOrigins("http://localhost:3000") .allowedMethods("GET", "POST", "PUT", "DELETE") .allowCredentials(true); } }
這種方式允許更多的自定義配置,適用于復(fù)雜的跨域場(chǎng)景。
3. 使用Filter配置
通過(guò)自定義Filter
來(lái)處理跨域請(qǐng)求也是一種有效的方式。創(chuàng)建一個(gè)CorsFilter
類,實(shí)現(xiàn)Filter
接口:
@Component public class CorsFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletResponse httpResponse = (HttpServletResponse) response; httpResponse.setHeader("Access-Control-Allow-Origin", "http://localhost:3000"); httpResponse.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); httpResponse.setHeader("Access-Control-Allow-Credentials", "true"); chain.doFilter(request, response); } }
然后,將該Filter注冊(cè)到Spring Boot應(yīng)用中。
4. 使用全局配置
在application.properties
或application.yml
中添加全局配置項(xiàng):
spring.mvc.cors.allowed-origins=http://localhost:3000 spring.mvc.cors.allowed-methods=GET,POST,PUT,DELETE spring.mvc.cors.allow-credentials=true
這種方式不需要編寫額外的Java代碼,適用于全局的跨域配置。
結(jié)束語(yǔ)
Spring Boot提供了多種方式來(lái)實(shí)現(xiàn)跨域請(qǐng)求,開發(fā)者可以根據(jù)具體需求選擇適合的方法。在配置時(shí),要確保不僅考慮安全性,還要兼顧應(yīng)用的靈活性和性能。希望本文對(duì)你理解Spring Boot中跨域配置提供了一些幫助。
到此這篇關(guān)于Spring Boot 中實(shí)現(xiàn)跨域的幾種方式的文章就介紹到這了,更多相關(guān)Spring Boot 跨域內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在idea中使用JaCoCo插件統(tǒng)計(jì)單元測(cè)試覆蓋率的實(shí)現(xiàn)
這篇文章主要介紹了在idea中使用JaCoCo插件統(tǒng)計(jì)單元測(cè)試覆蓋率的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01在SpringBoot 中從application.yml中獲取自定義常量方式
這篇文章主要介紹了在SpringBoot 中從application.yml中獲取自定義常量方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04java使用ZipInputStream實(shí)現(xiàn)讀取和寫入zip文件
zip文檔可以以壓縮格式存儲(chǔ)一個(gè)或多個(gè)文件,本文主要為大家詳細(xì)介紹了java如何使用ZipInputStream讀取Zip文檔與寫入,需要的小伙伴可以參考下2023-11-11java 中Executor, ExecutorService 和 Executors 間的不同
這篇文章主要介紹了java 中Executor, ExecutorService 和 Executors 間的不同的相關(guān)資料,需要的朋友可以參考下2017-06-06Java Web會(huì)話技術(shù)Session的簡(jiǎn)單使用
在請(qǐng)求需要傳遞的信息比較多,使用Cookie技術(shù)就會(huì)增大請(qǐng)求的難度。而Session可以存儲(chǔ)對(duì)象、數(shù)組等信息,并且Session是存儲(chǔ)到服務(wù)器端的,在客戶端請(qǐng)求時(shí)只需要將session id一并攜帶給服務(wù)器端。本文將簡(jiǎn)單的介紹如何使用Session2021-05-05