Spring Boot 2.X優(yōu)雅的解決跨域問題
一、什么是源和跨域
源(origin)就是協(xié)議、域名和端口號。
URL由協(xié)議、域名、端口和路徑組成,如果兩個URL的協(xié)議、域名和端口全部相同,則表示他們同源。否則,只要協(xié)議、域名、端口有任何一個不同,就是跨域。
對https://www.baidu.com/index.html進行跨域比較:
URL | 是否跨域 | 原因 |
---|---|---|
https://www.baidu.com/more/index.html | 不跨域 | 三要素相同 |
https://map.baidu.com/ | 跨域 | 域名不同 |
http://www.baidu.com/index.html | 跨域 | 協(xié)議不同 |
https://www.baidu.com:81/index.html | 跨域 | 端口號不同 |
隨著前后端分離開發(fā)的越來越普及,會經常遇到跨域的問題,當我們在瀏覽器中看到這樣的錯誤時,就需要意識到遇到了跨域:
二、什么是同源策略?
同源策略(Same origin policy)是一種約定,它是瀏覽器最核心也最基本的安全功能,如果缺少了同源策略,則瀏覽器的正常功能可能都會受到影響??梢哉fWeb是構建在同源策略基礎之上的,瀏覽器只是針對同源策略的一種實現。
同源策略又分為以下兩種:
- DOM同源策略:禁止對不同源頁面DOM 進行操作。這里主要場景是iframe跨域的情況,不同域名的iframe是限制互相訪問的。
- XMLHttpRequest同源策略:禁止使用XHR對象向不同源的服務器地址發(fā)起HTTP請求。
三、Spring Boot跨域解決方案
本例使用Spring Boot 2.1.2.RELEASE演示,分別用8080和8081端口啟動,部分代碼如下:
跨域頁面:testOtherDomain.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>不同域名-Java碎碎念</title> </head> <body> <button id="b1">點我測試</button> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script> <script> $("#b1").click(function () { $.ajax({ url: "http://localhost:8081/hello", type: "post", success:function (res) { console.log(res); } }) }); </script> </body> </html>
接口類:HelloController
package com.example.helloSpringBoot.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String HelloSpring (){ return "hello Java碎碎念!"; } }
未解決跨域前運行截圖:
在Spring Boot 2.X應用程序中可以使用注解@CrossOrigin,也可以通過使用WebMvcConfigurer對象來定義全局CORS配置。
1、@CrossOrigin注解示例代碼
package com.example.helloSpringBoot.controller; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @CrossOrigin @RequestMapping("/hello") public String HelloSpring (){ return "hello Java碎碎念!"; } }
2. WebMvcConfigurer對象示例代碼
package com.example.helloSpringBoot.config; import org.springframework.context.annotation.Bean; 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 MyConfiguration { @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurer() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/*") .allowedOrigins("*") .allowCredentials(true) .allowedMethods("GET", "POST", "DELETE", "PUT","PATCH") .maxAge(3600); } }; } }
按照上面兩種方式的一種配置完成后,即可實現對跨域的支持,運行成功截圖如下:
完整源碼地址:https://github.com/suisui2019/helloSpringBoot (本地下載)
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。
相關文章
Java線程之線程同步synchronized和volatile詳解
這篇文章主要介紹了Java線程之線程同步synchronized和volatile詳解,具有一定參考價值,需要的朋友可以了解下。2017-11-11關于idea的gitignore文件編寫及解決ignore文件不生效問題
這篇文章主要介紹了idea的gitignore文件編寫及解決ignore文件不生效問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03