功能篇:JAVA后端实现跨域配置

在Java后端实现跨域配置(CORS,Cross-Origin Resource Sharing)有多种方法,具体取决于你使用的框架。如果你使用的是Spring Boot或Spring MVC,可以通过以下几种方式来配置CORS。

方法一:全局配置

对于所有请求的跨域配置,可以在Spring Boot应用中通过`WebMvcConfigurer`接口进行全局配置:

```java

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 CorsConfig {

@Bean

public WebMvcConfigurer corsConfigurer() {

return new WebMvcConfigurer() {

@Override

public void addCorsMappings(CorsRegistry registry) {

registry.addMapping("/**") // 允许所有的路径

.allowedOrigins("*") // 允许所有的来源

.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 允许的方法

.allowedHeaders("*") // 允许的头部信息

.allowCredentials(true); // 是否允许发送Cookie

}

};

}

}

```

方法二:基于注解的方式

对于特定控制器或方法级别的跨域配置,可以使用`@CrossOrigin`注解:

```java

import org.springframework.web.bind.annotation.CrossOrigin;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

@CrossOrigin(origins = "http://example.com") // 指定允许的来源

public class MyController {

@GetMapping("/api/test")

public String test() {

return "Hello, CORS!";

}

}

```

方法三:通过过滤器实现

如果需要更细粒度的控制,或者你需要对所有进入应用程序的请求都添加CORS响应头,你可以创建一个自定义过滤器:

```java

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

@Configuration

public class CorsFilterConfig {

@Bean

public Filter corsFilter() {

return new Filter() {

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)

throws IOException, ServletException {

HttpServletResponse response = (HttpServletResponse) res;

HttpServletRequest request = (HttpServletRequest) req;

response.setHeader("Access-Control-Allow-Origin", "*");

response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");

response.setHeader("Access-Control-Max-Age", "3600");

response.setHeader("Access-Control-Allow-Headers", "x-requested-with, authorization");

if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {

response.setStatus(HttpServletResponse.SC_OK);

} else {

chain.doFilter(req, res);

}

}

public void init(FilterConfig filterConfig) {}

public void destroy() {}

};

}

}

```

以上三种方法可以根据你的需求选择最适合的一种或组合使用。全局配置适用于大多数场景,而基于注解的方式则提供了更加精细的控制。过滤器提供了一种更底层的方式来处理跨域问题,并且可以在其他方面增强安全性或功能性。

相关推荐
万少4 小时前
小龙虾(openclaw),轻松玩转自动发帖
前端·人工智能·后端
Jagger_6 小时前
AI 洪水淹到脖子了:剩下的是什么?我们该往哪儿跑?
后端
Victor3567 小时前
MongoDB(28)什么是地理空间索引?
后端
Victor3567 小时前
MongoDB(29)如何创建索引?
后端
皮皮林5518 小时前
面试官:什么是 fail-fast?什么是 fail-safe?
后端
陈随易9 小时前
前端大咖mizchi不满Rust、TypeScript却爱上MoonBit
前端·后端·程序员
雨中飘荡的记忆10 小时前
Multi-Agent + Skills + Spring AI 构建自主决策智能体
后端·spring
我叫黑大帅11 小时前
Go 语言并发编程的 “工具箱”
后端·面试·go