问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

API如何设置跨域

创作时间:
2025-03-24 19:01:39
作者:
@小白创作中心

API如何设置跨域

引用
1
来源
1.
https://docs.pingcode.com/baike/3276400

API设置跨域的关键在于:使用CORS头部、配置服务器、使用代理。其中,使用CORS头部是最常见和基本的方式,具体是在服务器端的响应头中添加Access-Control-Allow-Origin头部。

使用CORS头部

跨域资源共享(CORS)是一种浏览器机制,它允许来自一个源的网页向另一个源发出请求,从而实现跨域访问。通过设置适当的CORS头部,服务器可以允许特定的域访问其资源。具体实现步骤如下:

添加 Access-Control-Allow-Origin 头部

在服务器端响应中添加 Access-Control-Allow-Origin 头部,可以指定允许访问该资源的域。例如,允许所有域访问:

Access-Control-Allow-Origin: *

或者,仅允许特定域访问:

Access-Control-Allow-Origin: https://example.com

其他 CORS 头部

除了 Access-Control-Allow-Origin 外,还可能需要设置其他 CORS 头部,例如:

  • Access-Control-Allow-Methods:指定允许的 HTTP 方法,如 GET, POST, PUT, DELETE。
  • Access-Control-Allow-Headers:指定允许的 HTTP 头部,如 Content-Type, Authorization。
  • Access-Control-Allow-Credentials:如果需要允许发送凭据(如 Cookie),将其设置为 true。
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: Content-Type, Authorization
Access-Control-Allow-Credentials: true

CORS(跨域资源共享)

跨域资源共享(CORS)是实现跨域请求的最常见方法。CORS 允许服务器明确指定哪些域可以访问其资源,并设置必要的 HTTP 头部来管理这些请求。

CORS 基本概念

CORS 由一组 HTTP 头部组成,它允许服务器控制跨域请求。浏览器会根据这些头部决定是否允许客户端访问资源。主要的 CORS 头部包括:

  • Access-Control-Allow-Origin:指定允许的域。
  • Access-Control-Allow-Methods:指定允许的 HTTP 方法。
  • Access-Control-Allow-Headers:指定允许的请求头。
  • Access-Control-Allow-Credentials:是否允许发送凭据。

服务器端配置 CORS

不同的服务器框架和语言有不同的配置方式。下面是一些常见的示例:

Node.js(Express)

在 Express 应用中,可以使用 cors 中间件来配置 CORS:

const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors({
  origin: 'https://example.com',
  methods: ['GET', 'POST', 'PUT', 'DELETE'],
  allowedHeaders: ['Content-Type', 'Authorization'],
  credentials: true
}));
app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Java(Spring Boot)

在 Spring Boot 应用中,可以使用 @CrossOrigin 注解或全局配置来设置 CORS:

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
  @CrossOrigin(origins = "https://example.com")
  @GetMapping("/api/data")
  public String getData() {
    return "Hello World";
  }
}

全局配置:

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 WebConfig {
  @Bean
  public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurer() {
      @Override
      public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/")
                .allowedOrigins("https://example.com")
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .allowedHeaders("Content-Type", "Authorization")
                .allowCredentials(true);
      }
    };
  }
}

使用代理

有时候,直接在服务器上配置 CORS 可能不太方便,尤其是在开发环境中。此时,可以通过代理服务器来解决跨域问题。

什么是代理

代理服务器是客户端和目标服务器之间的中介,它可以代表客户端发出请求并返回响应。通过在代理服务器上配置跨域请求,可以绕过浏览器的同源策略。

使用代理解决跨域问题

Node.js(http-proxy-middleware)

在使用 React 或 Vue 等前端框架时,可以使用 http-proxy-middleware 库来配置代理。

React(create-react-app)

在 src/setupProxy.js 中配置代理:

const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function(app) {
  app.use(
    '/api',
    createProxyMiddleware({
      target: 'http://localhost:5000',
      changeOrigin: true,
    })
  );
};

Vue(vue.config.js)

在 vue.config.js 中配置代理:

module.exports = {
  devServer: {
    proxy: {
      '/api': {
        target: 'http://localhost:5000',
        changeOrigin: true,
      }
    }
  }
};

通过这种方式,前端应用中的所有 /api 请求都会被代理到 http://localhost:5000,从而避免跨域问题。

JSONP(JSON with Padding)

JSONP 是一种早期的跨域解决方案,它通过动态插入 <script> 标签来实现跨域请求。虽然 JSONP 在现代应用中不太常用,但了解它的原理仍然有助于理解跨域问题。

JSONP 的基本原理

JSONP 利用了 <script> 标签不受同源策略限制的特点,通过动态创建 <script> 标签并将数据作为 JavaScript 代码执行。服务器返回的数据通常是一个调用指定回调函数的 JavaScript 代码。

JSONP 的实现

前端代码

<script>
  function handleResponse(data) {
    console.log(data);
  }
  const script = document.createElement('script');
  script.src = 'https://example.com/api/data?callback=handleResponse';
  document.body.appendChild(script);
</script>

服务器端代码(Node.js)

const express = require('express');
const app = express();
app.get('/api/data', (req, res) => {
  const callback = req.query.callback;
  const data = { message: 'Hello World' };
  res.send(`${callback}(${JSON.stringify(data)})`);
});
app.listen(3000, () => {
  console.log('Server running on port 3000');
});

通过这种方式,服务器返回的数据被包装成一个 JavaScript 函数调用,从而实现跨域请求。

服务器端配置与安全性

在配置跨域请求时,安全性是一个重要的考虑因素。错误的配置可能会导致跨站请求伪造(CSRF)等安全问题。

限制允许的源

尽量避免使用通配符 *,而是明确指定允许访问的域。这样可以防止恶意网站访问您的资源。

Access-Control-Allow-Origin: https://trusted-domain.com

验证请求头

根据需要,限制允许的请求头,以防止恶意请求。例如:

Access-Control-Allow-Headers: Content-Type, Authorization

使用凭据

如果需要允许跨域请求发送凭据(如 Cookie),确保配置正确的 CORS 头部,并在客户端设置 withCredentials 为 true:

Access-Control-Allow-Credentials: true

客户端代码(AJAX 请求):

const xhr = new XMLHttpRequest();
xhr.open('GET', 'https://example.com/api/data', true);
xhr.withCredentials = true;
xhr.send();

浏览器端的跨域限制与解决方案

浏览器端的跨域限制是由同源策略引起的。同源策略是一种安全机制,防止不同源的网页之间相互访问。了解浏览器端的跨域限制有助于更好地配置服务器端的 CORS。

同源策略

同源策略要求,只有协议、域名和端口号都相同的请求才能被允许。这意味着以下情况都被视为跨域请求:

  • 不同的协议(http vs https)
  • 不同的域名(example.com vs api.example.com)
  • 不同的端口号(80 vs 8080)

预检请求

对于一些复杂请求(如使用 PUT、DELETE 方法或自定义头部),浏览器会先发起一个 OPTIONS 请求(预检请求),以确认服务器是否允许该请求。服务器需要正确处理预检请求,并返回相应的 CORS 头部。

OPTIONS /api/data HTTP/1.1
Host: example.com
Origin: https://client.com
Access-Control-Request-Method: PUT
Access-Control-Request-Headers: Content-Type

服务器响应:

HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://client.com
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Credentials: true

通过正确处理预检请求,可以确保复杂请求也能顺利进行。

常见问题与解决方案

在实际项目中,设置跨域时可能会遇到各种问题。以下是一些常见问题及其解决方案:

Access-Control-Allow-Origin 头部缺失

症状:浏览器报错:“No 'Access-Control-Allow-Origin' header is present on the requested resource。”

解决方案:确保服务器端响应中包含 Access-Control-Allow-Origin 头部,并且值设置为允许的域名或通配符 *。

预检请求失败

症状:浏览器报错:“Response for preflight has invalid HTTP status code 404。”

解决方案:确保服务器正确处理 OPTIONS 预检请求,并返回相应的 CORS 头部。

凭据发送失败

症状:浏览器报错:“The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'。”

解决方案:在服务器端设置 Access-Control-Allow-Credentials 为 true,并确保 Access-Control-Allow-Origin 头部的值不是通配符 *,而是指定的域名。

总结来说,API 设置跨域是一个涉及多方面配置和考虑的过程。通过理解 CORS 原理、配置服务器、使用代理以及注意安全性,可以有效地解决跨域问题,并确保应用的正常运行。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号