詳解springboot解決CORS跨域的三種方式

一、實現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")
            // 是否允許發送cookie true-允許 false-不允許 默認false。對服務器有特殊要求的請求,比如請求方法是PUT或DELETE,或者Content-Type字段的類型是application/json,這個值隻能設為true
            .allowCredentials(true)
            // 預檢間隔時間1小時,單位為秒。指定本次預檢請求的有效期,在有效期間,不用發出另一條預檢請求。
            // 瀏覽器發出CORS簡單請求,隻需要在頭信息之中增加一個Origin字段
            // 瀏覽器發出CORS非簡單請求,會在正式通信之前,增加一次OPTIONS查詢請求,稱為"預檢"請求(preflight)。瀏覽器先詢問服務器,當前網頁所在的域名是否在服務器的許可名單之中,以及可以使用哪些HTTP動詞和頭信息字段。隻有得到肯定答復,瀏覽器才會發出正式的XMLHttpRequest請求,否則就報錯。
            .maxAge(3600)
            // 允許跨域請求可攜帶的header,'*'表所有header頭。CORS請求時,XMLHttpRequest對象的getResponseHeader()方法隻能拿到6個基本字段:Cache-Control、Content-Language、Content-Type、Expires、Last-Modified、Pragma。如果想拿到其他字段,就必須在Access-Control-Expose-Headers裡面指定
            .allowedHeaders("*");
    }
}

二、實現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可配置在方法上,也可配置在類上。 

四、實戰

創建兩個普通的SpringBoot項目A、B,A配置8081端口,B配置8082端口。

在A的resources/static目錄下創建一個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,跨站點時,任何情況下都不會發送 Cookie。換言之,隻有當前網頁的 URL 與請求目標一致,才會帶上 Cookie。

Lax規則稍稍放寬,大多數情況也是不發送第三方 Cookie,但是導航到目標網址的 Get 請求除外。導航到目標 URL 的 GET 請求僅包括三種情況:鏈接、預加載請求和 GET 表單。

網站可以選擇顯式關閉SameSite屬性,將其設為None。不過,前提是必須同時設置Secure屬性(Cookie 隻能通過 HTTPS 協議發送),否則無效。

SpringBoot 2.6及以上版本

網上查到可使用配置(但親測無效!):

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跨域內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: