詳解springboot解決CORS跨域的三種方式
一、實現(xiàn)WebMvcConfigurer接口
@Configuration
public class WebConfig implements WebMvcConfigurer {
/**
* 添加跨域支持
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
// 允許跨域訪問的路徑 '/**'表示應用的所有方法
registry.addMapping("/**")
// 允許跨域訪問的來源 '*'表示所有域名來源
.allowedOriginPatterns("*")
// .allowedOrigins("*") // 允許跨域訪問的來源 SpringBoot2.4.0之前的版本
// 允許跨域請求的方法 '*'表示所有
.allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
// 是否允許發(fā)送cookie true-允許 false-不允許 默認false。對服務器有特殊要求的請求,比如請求方法是PUT或DELETE,或者Content-Type字段的類型是application/json,這個值只能設為true
.allowCredentials(true)
// 預檢間隔時間1小時,單位為秒。指定本次預檢請求的有效期,在有效期間,不用發(fā)出另一條預檢請求。
// 瀏覽器發(fā)出CORS簡單請求,只需要在頭信息之中增加一個Origin字段
// 瀏覽器發(fā)出CORS非簡單請求,會在正式通信之前,增加一次OPTIONS查詢請求,稱為"預檢"請求(preflight)。瀏覽器先詢問服務器,當前網(wǎng)頁所在的域名是否在服務器的許可名單之中,以及可以使用哪些HTTP動詞和頭信息字段。只有得到肯定答復,瀏覽器才會發(fā)出正式的XMLHttpRequest請求,否則就報錯。
.maxAge(3600)
// 允許跨域請求可攜帶的header,'*'表所有header頭。CORS請求時,XMLHttpRequest對象的getResponseHeader()方法只能拿到6個基本字段:Cache-Control、Content-Language、Content-Type、Expires、Last-Modified、Pragma。如果想拿到其他字段,就必須在Access-Control-Expose-Headers里面指定
.allowedHeaders("*");
}
}二、實現(xiàn)filter過濾器方式
@WebFilter
@Configuration
public class CorsFilter implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PATCH, DELETE, PUT");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
chain.doFilter(req, res);
}
}三、注解@CrossOrigin
@CrossOrigin(originPatterns = "*", allowCredentials = "true")
@CrossOrigin可配置在方法上,也可配置在類上。
四、實戰(zhàn)
創(chuàng)建兩個普通的SpringBoot項目A、B,A配置8081端口,B配置8082端口。
在A的resources/static目錄下創(chuàng)建一個html文件index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<!-- jquery庫可百度jquery cdn -->
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<script>
function btnClick() {
$.get('http://localhost:8082/hello/hello', function (msg) {
$("#app").html(msg);
});
}
function btnClick2() {
$.post('http://localhost:8082/hello/hello', function (msg) {
$("#app").html(msg);
});
}
</script>
<body>
<div id="app"></div>
<input type="button" onclick="btnClick()" value="get_button">
<input type="button" onclick="btnClick2()" value="post_button">
</body>
</html>B提供2個web接口:
@RestController
@RequestMapping("/hello")
public class HelloController {
// @CrossOrigin(originPatterns = "*", allowCredentials = "true")
@GetMapping("/hello")
public String hello() {
System.out.println("get hello");
return "get hello";
}
// @CrossOrigin(originPatterns = "*", allowCredentials = "true")
@PostMapping("/hello")
public String hello2() {
System.out.println("post hello");
return "post hello";
}
}分別啟動A、B服務,瀏覽器訪問A的index.html,點擊按鈕,瀏覽器控制臺報錯如下:http://localhost:8081/index.html
Access to XMLHttpRequest at 'http://localhost:8082/hello/hello' from origin 'http://localhost:8081' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

為B項目使用方式一,添加跨域支持,重啟,再次點擊按鈕,可正常訪問,觀察響應頭多了支持跨域的信息:

五、cookie的跨域
從Chrome51開始,瀏覽器cookie添加了一個新屬性SameSite,以防止 CSRF 攻擊和用戶跟蹤。
SameSite可取值:Strict、Lax、None。
Strict最為嚴格,完全禁止第三方 Cookie,跨站點時,任何情況下都不會發(fā)送 Cookie。換言之,只有當前網(wǎng)頁的 URL 與請求目標一致,才會帶上 Cookie。
Lax規(guī)則稍稍放寬,大多數(shù)情況也是不發(fā)送第三方 Cookie,但是導航到目標網(wǎng)址的 Get 請求除外。導航到目標 URL 的 GET 請求僅包括三種情況:鏈接、預加載請求和 GET 表單。
網(wǎng)站可以選擇顯式關閉SameSite屬性,將其設為None。不過,前提是必須同時設置Secure屬性(Cookie 只能通過 HTTPS 協(xié)議發(fā)送),否則無效。
SpringBoot 2.6及以上版本
網(wǎng)上查到可使用配置(但親測無效!):
server.servlet.session.cookie.same-site=none server.servlet.session.cookie.secure=true
SpringBoot 2.6以下版本
如果使用 Tomcat 作為服務器,則可以通過以下配置設置會話 cookie 的 SameSite 屬性(親測無效!)。
server.servlet.session.cookie.secure=true
@Configuration
public class TomcatCookieConfig {
@Bean
public TomcatContextCustomizer sameSiteCookiesConfig() {
return context -> {
final Rfc6265CookieProcessor cookieProcessor = new Rfc6265CookieProcessor();
// SameSite
cookieProcessor.setSameSiteCookies(SameSiteCookies.NONE.getValue());
context.setCookieProcessor(cookieProcessor);
};
}
}如果您使用的是 Spring-Session,那么您可以使用以下配置來設置 cookie 的 SameSite 屬性。
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>@Configuration
public class SpringSessionConfiguration {
@Bean
public CookieSerializer cookieSerializer() {
DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
// Strict-嚴格模式 Lax-松懈模式 None-無
cookieSerializer.setSameSite("None");
cookieSerializer.setUseSecureCookie(true);
return cookieSerializer;
}
}自己的解決方式
@Configuration
public class CookieConfig {
private static String domain;
@Value("${domain}")
public void setDomain(String domain) {
CookieConfig.domain = domain;
}
public static HttpCookie generateHttpCookie(String name, String value) {
return ResponseCookie.from(name, value)
.domain(domain)
// cookie跨域設置
.sameSite("None")
// 在https下傳輸,配合sameSite=None使用
.secure(true)
.path("/")
// 有效期24小時
.maxAge(60 * 60 * 24)
.build();
}
} @GetMapping("/hello")
public String hello(HttpServletResponse response) {
HttpCookie cookie2 = CookieConfig.generateHttpCookie("age", "18");
response.addHeader(HttpHeaders.SET_COOKIE, cookie2.toString());
HttpCookie cookie3 = CookieConfig.generateHttpCookie("id", "77");
response.addHeader(HttpHeaders.SET_COOKIE, cookie3.toString());
System.out.println("get hello");
return "get hello";
}參考:https://www.cnblogs.com/javastack/p/14255114.html
到此這篇關于詳解springboot解決CORS跨域的三種方式的文章就介紹到這了,更多相關springboot CORS跨域內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
不使用他人jar包情況下優(yōu)雅的進行dubbo調用詳解
這篇文章主要為大家介紹了不使用他人jar包情況下優(yōu)雅的進行dubbo調用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
基于SpringBoot中activeMq的JmsTemplate的實例
這篇文章主要介紹了基于SpringBoot中activeMq的JmsTemplate的實例問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
Java中的PrintWriter 介紹_動力節(jié)點Java學院整理
PrintWriter 是字符類型的打印輸出流,它繼承于Writer。接下來通過本文給大家介紹java中的 PrintWriter 相關知識,感興趣的朋友一起學習吧2017-05-05
Ubuntu安裝JDK與IntelliJ?IDEA的詳細過程
APT是Linux系統(tǒng)上的包管理工具,能自動解決軟件包依賴關系并從遠程存儲庫中獲取安裝軟件包,這篇文章主要介紹了Ubuntu安裝JDK與IntelliJ?IDEA的過程,需要的朋友可以參考下2023-08-08

