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ā)的越來越普及,會經(jīng)常遇到跨域的問題,當(dāng)我們在瀏覽器中看到這樣的錯誤時,就需要意識到遇到了跨域:

二、什么是同源策略?
同源策略(Same origin policy)是一種約定,它是瀏覽器最核心也最基本的安全功能,如果缺少了同源策略,則瀏覽器的正常功能可能都會受到影響。可以說Web是構(gòu)建在同源策略基礎(chǔ)之上的,瀏覽器只是針對同源策略的一種實現(xiàn)。
同源策略又分為以下兩種:
- DOM同源策略:禁止對不同源頁面DOM 進行操作。這里主要場景是iframe跨域的情況,不同域名的iframe是限制互相訪問的。
- XMLHttpRequest同源策略:禁止使用XHR對象向不同源的服務(wù)器地址發(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應(yīng)用程序中可以使用注解@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);
}
};
}
}
按照上面兩種方式的一種配置完成后,即可實現(xiàn)對跨域的支持,運行成功截圖如下:
完整源碼地址:https://github.com/suisui2019/helloSpringBoot (本地下載)
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。
相關(guān)文章
Intellij?IDEA?中調(diào)試?maven?插件的步驟
這篇文章主要介紹了Intellij?IDEA?中調(diào)試?maven?插件,本文分步驟給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
淺談java中類名.class, class.forName(), getClass()的區(qū)別
下面小編就為大家?guī)硪黄獪\談java中類名.class, class.forName(), getClass()的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
java list中包含某個字符串的兩種方法實現(xiàn)
在Java開發(fā)中,經(jīng)常需要判斷一個List中是否包含特定的字符串,包括使用contains()方法和循環(huán)遍歷判斷,具有一定的參考價值,感興趣的可以了解一下2024-03-03
Windows下將JAVA?jar注冊成windows服務(wù)的方法
這篇文章主要介紹了Windows下將JAVA?jar注冊成windows服務(wù)的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07

