Spring Boot 2.X優(yōu)雅的解決跨域問題
一、什么是源和跨域
源(origin)就是協(xié)議、域名和端口號(hào)。
URL由協(xié)議、域名、端口和路徑組成,如果兩個(gè)URL的協(xié)議、域名和端口全部相同,則表示他們同源。否則,只要協(xié)議、域名、端口有任何一個(gè)不同,就是跨域。
對(duì)https://www.baidu.com/index.html進(jìn)行跨域比較:
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 | 跨域 | 端口號(hào)不同 |
隨著前后端分離開發(fā)的越來越普及,會(huì)經(jīng)常遇到跨域的問題,當(dāng)我們?cè)跒g覽器中看到這樣的錯(cuò)誤時(shí),就需要意識(shí)到遇到了跨域:
二、什么是同源策略?
同源策略(Same origin policy)是一種約定,它是瀏覽器最核心也最基本的安全功能,如果缺少了同源策略,則瀏覽器的正常功能可能都會(huì)受到影響??梢哉fWeb是構(gòu)建在同源策略基礎(chǔ)之上的,瀏覽器只是針對(duì)同源策略的一種實(shí)現(xiàn)。
同源策略又分為以下兩種:
- DOM同源策略:禁止對(duì)不同源頁(yè)面DOM 進(jìn)行操作。這里主要場(chǎng)景是iframe跨域的情況,不同域名的iframe是限制互相訪問的。
- XMLHttpRequest同源策略:禁止使用XHR對(duì)象向不同源的服務(wù)器地址發(fā)起HTTP請(qǐng)求。
三、Spring Boot跨域解決方案
本例使用Spring Boot 2.1.2.RELEASE演示,分別用8080和8081端口啟動(dòng),部分代碼如下:
跨域頁(yè)面:testOtherDomain.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>不同域名-Java碎碎念</title> </head> <body> <button id="b1">點(diǎn)我測(cè)試</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碎碎念!"; } }
未解決跨域前運(yùn)行截圖:
在Spring Boot 2.X應(yīng)用程序中可以使用注解@CrossOrigin,也可以通過使用WebMvcConfigurer對(duì)象來定義全局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對(duì)象示例代碼
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); } }; } }
按照上面兩種方式的一種配置完成后,即可實(shí)現(xiàn)對(duì)跨域的支持,運(yùn)行成功截圖如下:
完整源碼地址:https://github.com/suisui2019/helloSpringBoot (本地下載)
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
用JAVA 設(shè)計(jì)生成二維碼詳細(xì)教程
本文主要介紹用JAVA 設(shè)計(jì)生成二維碼,這里一步一步詳細(xì)介紹用 java 如何設(shè)計(jì)二維碼,并附有代碼示例以便參考,有需要的小伙伴可以參考下2016-08-08關(guān)于java中可變長(zhǎng)參數(shù)的定義及使用方法詳解
下面小編就為大家?guī)硪黄P(guān)于java中可變長(zhǎng)參數(shù)的定義及使用方法詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12spring aop之鏈?zhǔn)秸{(diào)用的實(shí)現(xiàn)
這篇文章主要介紹了spring aop之鏈?zhǔn)秸{(diào)用的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02Java線程之線程同步synchronized和volatile詳解
這篇文章主要介紹了Java線程之線程同步synchronized和volatile詳解,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11關(guān)于idea的gitignore文件編寫及解決ignore文件不生效問題
這篇文章主要介紹了idea的gitignore文件編寫及解決ignore文件不生效問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03java實(shí)現(xiàn)簡(jiǎn)單猜數(shù)字
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單猜數(shù)字,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05