一、什么是跨域請求?
跨域請求,就是說瀏覽器在執行腳本文件的ajax請求時,腳本文件所在的服務地址和請求的服務地址不一樣。說白了就是ip、網絡協議、端口都一樣的時候,就是同一個域,否則就是跨域。這是由於Netscape提出一個著名的安全策略——同源策略造成的,這是瀏覽器對JavaScript施加的安全限制。是防止外網的腳本惡意攻擊服務器的一種措施。
二、SpringBoot工程如何解決跨域問題?
那么如何在SpringBoot中處理跨域問題呢?方法有很多,這里着重講一種——利用@Configuration配置跨域。
代碼實現如下:
/** * Created by myz on 2017/7/10. * * 設置跨域請求 */ @Configuration public class CorsConfig { private CorsConfiguration buildConfig() { CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.addAllowedOrigin("*"); // 1 設置訪問源地址 corsConfiguration.addAllowedHeader("*"); // 2 設置訪問源請求頭 corsConfiguration.addAllowedMethod("*"); // 3 設置訪問源請求方法 return corsConfiguration; } @Bean public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", buildConfig()); // 4 對接口配置跨域設置 return new CorsFilter(source); } }
“*”代表全部。”**”代表適配所有接口。
其中addAllowedOrigin(String origin)方法是追加訪問源地址。如果不使用”*”(即允許全部訪問源),則可以配置多條訪問源來做控制。
例如:
corsConfiguration.addAllowedOrigin("http://www.aimaonline.cn/");
corsConfiguration.addAllowedOrigin("http://test.aimaonline.cn/");
查看CorsConfiguration類的官方文檔(http://docs.spring.io/spring/docs/4.2.x/javadoc-api/org/springframework/web/cors/CorsConfiguration.html#addAllowedOrigin-java.lang.String-)
我們可以找到官方對setAllowedOrigins(List allowedOrigins)和addAllowedOrigin(String origin)方法的介紹。
addAllowedOrigin是追加訪問源地址,而setAllowedOrigins是可以直接設置多條訪問源。
但是有一點請注意,我查看setAllowedOrigins方法源碼時發現,源碼如下
public void setAllowedOrigins(List<String> allowedOrigins) { this.allowedOrigins = allowedOrigins != null?new ArrayList(allowedOrigins):null; }
根據源碼可以得知,setAllowedOrigins會覆蓋this.allowedOrigins。所以在配置訪問源地址時,
addAllowedOrigin方法要寫在setAllowedOrigins后面,當然了,一般情況下這兩個方法也不會混着用。
addAllowedHeader、addAllowedMethod、registerCorsConfiguration方法和addAllowedOrigin的源碼差不太多,這里就不一一介紹了。感興趣的朋友可以自行查閱。
三、其他實現跨域請求方法
當然。除了用這種初始化配置的方法設置跨域問題,在官方的文檔中也介紹了其他實現跨域請求的方法(http://spring.io/blog/2015/06/08/cors-support-in-spring-framework)。
例如在接口上使用@CrossOrgin注解:
1 @RestController 2 @RequestMapping("/account") 3 public class AccountController { 4 5 @CrossOrigin 6 @GetMapping("/{id}") 7 public Account retrieve(@PathVariable Long id) { 8 // ... 9 } 10 11 @DeleteMapping("/{id}") 12 public void remove(@PathVariable Long id) { 13 // ... 14 } 15 }
對於上述代碼,官方給出如下一段說明:
You can add to your @RequestMapping annotated handler method a @CrossOrigin annotation in order to enable CORS on it (by default @CrossOrigin allows all origins and the HTTP methods specified in the @RequestMapping annotation).
意思就是可以直接在@RequestMapping接口上使用@CrossOrigin實現跨域。@CrossOrigin默認允許所有訪問源和訪問方法。
還有一種方法是直接對整個Controller進行跨域設置:
1 @CrossOrigin(origins = "http://domain2.com", maxAge = 3600) 2 @RestController 3 @RequestMapping("/account") 4 public class AccountController { 5 6 @GetMapping("/{id}") 7 public Account retrieve(@PathVariable Long id) { 8 // ... 9 } 10 11 @DeleteMapping("/{id}") 12 public void remove(@PathVariable Long id) { 13 // ... 14 } 15 }
這里,可以對@CrossOrigin設置特定的訪問源,而不是使用默認配置。
以上就是對SpringBoot下配實現跨域請求的介紹。
參考文檔:
http://spring.io/blog/2015/06/08/cors-support-in-spring-framework
http://blog.csdn.net/lovesummerforever/article/details/38052213
http://blog.csdn.net/lambert310/article/details/51683775