功能篇: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() {}

};

}

}

```

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

相关推荐
zs宝来了21 分钟前
Playwright 自动发布 CSDN 的完整实践
java
彭于晏Yan1 小时前
Redisson分布式锁
spring boot·redis·分布式
吴声子夜歌1 小时前
TypeScript——基础类型(三)
java·linux·typescript
GetcharZp2 小时前
Git 命令行太痛苦?这款 75k Star 的神级工具,让你告别“合并冲突”恐惧症!
后端
Victor3562 小时前
MongoDB(69)如何进行增量备份?
后端
Victor3563 小时前
MongoDB(70)如何使用副本集进行备份?
后端
DynamicsAgg3 小时前
企业数字化底座-k8s企业实践系列第二篇pod创建调度
java·容器·kubernetes
千寻girling3 小时前
面试官 : “ 说一下 Python 中的常用的 字符串和数组 的 方法有哪些 ? ”
人工智能·后端·python
森林里的程序猿猿3 小时前
并发设计模式
java·开发语言·jvm