Spring MVC跨域设置

简介

出于安全方面考虑,浏览器发起请求时,会先检查同源策略(协议、主机、端口是否与当前页面相同),不匹配则认为是跨域请求。

CORS (Cross-Origin Resource Sharing)

CORS是一种机制,允许服务器声明哪些域(origin)可以访问其资源,从而绕过同源策略的限制。浏览器会发送预检请求(Preflight Request)以确定是否允许跨域访问。

Preflight请求

Preflight请求是跨域资源共享(CORS)中的一种机制,用于在实际请求之前发送一个预检请求。浏览器在发送某些类型的跨域请求(例如带有自定义标头的请求)之前,会自动发送一个OPTIONS请求,以获取目标服务器是否允许实际请求的权限。

预检请求包含一组查询信息,询问服务器是否允许实际请求。这些查询信息包括:

  • Access-Control-Request-Method: 表示实际请求中将使用的 HTTP 方法(例如 GET、POST)。
  • Access-Control-Request-Headers: 表示实际请求中将使用的自定义 HTTP 标头。

服务器收到预检请求后,会检查这些信息,然后决定是否允许实际请求。如果服务器允许,它会在响应中包含相应的 CORS 头(例如 Access-Control-Allow-Origin、Access-Control-Allow-Methods 等)。

这个预检请求机制有助于确保安全,因为它防止了潜在的恶意跨域请求。如果服务器支持并验证了预检请求,浏览器才会允许实际请求。

以下是一个预检请求的示例:

请求:

makefile 复制代码
OPTIONS /example/resource HTTP/1.1
Host: example.com
Access-Control-Request-Method: POST
Access-Control-Request-Headers: Content-Type, Authorization

响应:

makefile 复制代码
HTTP/1.1 200 OK
Access-Control-Allow-Origin: http://example.com
Access-Control-Allow-Methods: GET, POST
Access-Control-Allow-Headers: Content-Type, Authorization

Spring MVC跨域设置

浏览器发起Preflight请求,SpringMVC的处理流程为:
DispatcherServlet#doDispatch -> HttpRequestHandlerAdapter#handle -> AbstractHandlerMapping#handleRequest -> DefaultCorsProcessor#processRequest
DefaultCorsProcessor会根据当前配置的跨域规则,检查当前资源你是否允许发起的域访问,检查不通过时直接返回403 Forbidden,body为Invalid CORS request。

注解方式

可以在类,或者方法上使用@CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.OPTIONS})

Filter模式

对于使用了Spring-Web而没有使用SpringMVC的项目,可以使用Spring提供的CorsFilter,它会拦截的Servlet请求,并添加一些允许跨域的头,以下是允许所有请求跨域的示例

java 复制代码
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOrigin("*");
        config.addAllowedMethod("*");
        config.addAllowedHeader("*");
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}

测试

可以使用以下html进行跨域测试

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>POST Request Form</title>
</head>
<body>

<h1>POST Request Form</h1>

<form id="postForm">
    <label for="url">URL:</label>
    <input type="text" id="url" name="url" value="http://xxx" style="width: 100%; margin-bottom: 10px;">
    
    <label for="postData">POST Data:</label>
    <textarea id="postData" name="postData" style="width: 100%; height: 100px; margin-bottom: 10px;">
{

}
    </textarea>
    
    <button type="button" onclick="sendPostRequest()">Send POST Request</button>
</form>

<script>
function sendPostRequest() {
    var url = document.getElementById("url").value;
    var postData = document.getElementById("postData").value;

    fetch(url, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: postData,
    })
    .then(response => response.json())
    .then(data => {
        console.log('Success:', data);
        alert('POST request sent successfully!');
    })
    .catch((error) => {
        console.error('Error:', error);
        alert('Error sending POST request!');
    });
}
</script>

</body>
</html>
相关推荐
非优秀程序员1 小时前
第04章 邮箱大战,163 与 QQ 的暗战|《互联网产品恩仇录》
java·vue.js·架构
知已难寻2 小时前
Docker 入门:容器技术与 Docker 基础
java·运维·服务器·docker·云原生·容器
文丑颜不良啊4 小时前
LeetCode2481~2500题目解析
java·leetcode
Flynn Liang5 小时前
同一需求三套写法:用 Spring AI、LangChain4j、AgentScope Java 各实现一个带工具调用的 RAG 助手(实战对比)
java·人工智能·大模型·架构设计·spring ai·langchain4j·agentscope
聪明的笨猪猪5 小时前
实际工程问题:AI 生成 Spring Cache(@Cacheable / @CachePut / @CacheEvict)缓存注解踩坑
java·经验分享·面试·ai编程
愿有所息6 小时前
记一次 ARM64 + JDK8 上 fastjson2 的诡异数据错乱
java
秋饼6 小时前
MCP 2026-07-28 无状态协议大改版:Spring AI 2.1 多轮往返请求与网关路由落地
java·ai·技术分享·后端开发
SL_staff7 小时前
JVS-BI 实战:业务人员如何用零代码 ELT + 可视化配置构建实时经营分析闭环
java·spring·全栈
SL_staff8 小时前
企业AI落地难?JVS-AI渐进式实践:安全可控、小切口验证、标准化沉淀与规模化复制
java·开源·aigc
SL_staff9 小时前
JVS数字底座实践:构建企业级统一内容管理平台的技术路径
java·产品