最新springboot解決跨域的幾種方式小結(jié)
什么是跨域
跨域:指的是瀏覽器不能執(zhí)?其他?站的腳本。它是由瀏覽器的同源策略造成的,是瀏覽器對javascript施加的安全限制。
例如:a頁?想獲取b頁?資源,如果a、b頁?的協(xié)議、域名、端?、?域名不同,所進?的訪問?動都是跨域的,?瀏覽器
為了安全問題?般都限制了跨域訪問,也就是不允許跨域請求資源。注意:跨域限制訪問,其實是瀏覽器的限制。理解這?點
很重要
同源策略:是指協(xié)議,域名,端?都要相同,其中有?個不同都會產(chǎn)?跨域;
springboot解決跨域的幾種方式
方法一、SpringBoot的注解@CrossOrigin
直接在Controller方法或者類上增加@CrossOrigin注解,SpringMVC使用@CrossOrigin使用場景要求 jdk1.8+ Spring4.2+
@GetMapping("/hello") @CrossOrigin public String hello() { return "hello:" + simpleDateFormat.format(new Date()); }
方式二:使用CorsFilter
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 ConfigConfiguration { @Bean public CorsFilter CorsFilter() { CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.addAllowedOriginPattern("*"); corsConfiguration.addAllowedHeader("*"); corsConfiguration.addAllowedMethod("*"); corsConfiguration.setAllowCredentials(true); UrlBasedCorsConfigurationSource ub = new UrlBasedCorsConfigurationSource(); ub.registerCorsConfiguration("/**", corsConfiguration); return new CorsFilter(ub); } }
方式三:自定義過濾(web filter)的方式
@Component public class CustomFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletResponse res = (HttpServletResponse) servletResponse; // 設置允許Cookie res.addHeader("Access-Control-Allow-Credentials", "true"); // 允許http://www.xxx.com域(自行設置,這里只做示例)發(fā)起跨域請求 res.addHeader("Access-Control-Allow-Origin", "*"); // 設置允許跨域請求的方法 res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT"); // 允許跨域請求包含content-type res.addHeader("Access-Control-Allow-Headers", "Content-Type,X-CAF-Authorization-Token,sessionToken,X-TOKEN"); if (((HttpServletRequest) servletRequest).getMethod().equals("OPTIONS")) { servletResponse.getWriter().println("ok"); return; } filterChain.doFilter(servletRequest, servletResponse); } }
方式四:實現(xiàn)WebMvcConfigurer中addCorsMappings方法
import org.springframework.stereotype.Component; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Component public class MyWebMvcConfigurer implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") // 匹配所有的路徑 .allowCredentials(true) // 設置允許憑證 .allowedHeaders("*") // 設置請求頭 .allowedMethods("GET", "POST", "PUT", "DELETE") // 設置允許的方式 .allowedOriginPatterns("*"); } }
方法五:采用nginx做動態(tài)代理
到此這篇關(guān)于springboot解決跨域的幾種方式的文章就介紹到這了,更多相關(guān)springboot解決跨域內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringCloud注冊中心之consul詳細講解使用方法
Consul是一款由HashiCorp公司開源的,用于服務治理的軟件,Spring Cloud Consul對其進行了封裝,這篇文章主要介紹了springcloud組件consul服務治理,需要的朋友可以參考下2022-11-11Java異常處理Guava?Throwables類使用實例解析
這篇文章主要為大家介紹了Java異常處理神器Guava?Throwables類使用深入詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12詳解使用Spring Boot開發(fā)Restful程序
本篇文章主要介紹了詳解使用Spring Boot開發(fā)Restful程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05Java實現(xiàn)將PDF轉(zhuǎn)為圖片格式的方法詳解
PDF文件和圖片文件,這是兩種完全不一樣的格式,可是有的時候這兩種格式卻是有相互轉(zhuǎn)換的需要,本文將介紹如何通過Java應用程序快速高效地將PDF轉(zhuǎn)為圖片格式。一起來看看吧2023-03-03java編程實現(xiàn)簡單的網(wǎng)絡爬蟲示例過程
這篇文章主要為大家介紹了如何使用java編程實現(xiàn)一個簡單的網(wǎng)絡爬蟲示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-10-10springboot攔截器無法注入redisTemplate的解決方法
在工作中我們經(jīng)常需要做登錄攔截驗證或者其他攔截認證功能,但是在寫攔截器的時候發(fā)現(xiàn)redisTemplate一直無法注入進來,本文就詳細的介紹了解決方法,感興趣的可以了解一下2021-06-06