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

};

}

}

```

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

相关推荐
卷到起飞的数分几秒前
JVM探究
java·服务器·jvm
Geek攻城猫2 分钟前
Java生产环境问题排查实战指南
java·jvm
OtIo TALL8 小时前
redis7 for windows的安装教程
java
2601_949817728 小时前
Spring Boot3.3.X整合Mybatis-Plus
spring boot·后端·mybatis
uNke DEPH8 小时前
Spring Boot的项目结构
java·spring boot·后端
xixingzhe28 小时前
idea启动vue项目
java·vue.js·intellij-idea
zhenxin01228 小时前
Spring Boot 3.x 系列【3】Spring Initializr快速创建Spring Boot项目
spring boot·后端·spring
wzl202612139 小时前
企业微信定时群发技术实现与实操指南(原生接口+工具落地)
java·运维·前端·企业微信
凌波粒9 小时前
Java 8 “新”特性详解:Lambda、函数式接口、Stream、Optional 与方法引用
java·开发语言·idea
曹牧9 小时前
Eclipse:悬停提示(Hover)
java·ide·eclipse