赞
踩
跨域请求是指浏览器发起的请求的目标资源所在的域与当前页面所在的域不一致。在Java开发中,处理跨域请求是一个常见的问题,因为前端页面与后端服务可能部署在不同的域。本文将介绍一些常见的解决方法,以确保安全而有效地处理跨域请求。
CORS是一种通过HTTP头来授权跨域请求的机制。在Java中,你可以通过在响应头中添加特定的CORS头来允许跨域请求。
在Spring Boot项目中,你可以使用@CrossOrigin
注解或配置WebMvcConfigurer
来处理CORS。
@CrossOrigin
注解:- import org.springframework.web.bind.annotation.CrossOrigin;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- @RequestMapping("/api")
- @CrossOrigin(origins = "http://localhost:8080") // 允许指定域进行跨域访问
- public class MyController {
-
- @GetMapping("/data")
- public String getData() {
- return "Hello, Cross-Origin!";
- }
- }
WebMvcConfigurer
配置:- 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 implements WebMvcConfigurer {
-
- @Override
- public void addCorsMappings(CorsRegistry registry) {
- registry.addMapping("/api/**") // 指定路径
- .allowedOrigins("http://localhost:8080") // 允许指定域进行跨域访问
- .allowedMethods("GET", "POST", "PUT", "DELETE")
- .allowCredentials(true)
- .maxAge(3600);
- }
- }

对于复杂请求,比如使用自定义头信息或发送PUT、DELETE等请求,浏览器会先发送一个预检请求(OPTIONS请求)。在处理这类请求时,确保服务器能正确响应预检请求。
- import org.springframework.web.bind.annotation.CrossOrigin;
- import org.springframework.web.bind.annotation.DeleteMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- @RequestMapping("/api")
- @CrossOrigin(origins = "http://localhost:8080", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE})
- public class MyController {
-
- @DeleteMapping("/deleteData")
- public String deleteData() {
- return "Data Deleted!";
- }
- }

JSONP是一种通过动态创建script
元素来进行跨域请求的方法。在Java中,你需要处理JSONP请求并返回相应的数据。
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- @RequestMapping("/api")
- public class MyController {
-
- @GetMapping("/jsonp")
- public String getDataJsonp(@RequestParam String callback) {
- // 处理数据逻辑
- String data = "Hello, JSONP!";
- // 构造JSONP格式的响应
- return callback + "(" + data + ");";
- }
- }

通过在服务器端进行代理请求是另一种处理跨域请求的方法。在Java中,你可以创建一个代理接口,接收前端的请求并将其代理到目标服务,然后将响应返回给前端。
- import org.springframework.http.ResponseEntity;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.client.RestTemplate;
-
- @RestController
- @RequestMapping("/api")
- public class ProxyController {
-
- private final RestTemplate restTemplate;
-
- public ProxyController(RestTemplate restTemplate) {
- this.restTemplate = restTemplate;
- }
-
- @GetMapping("/proxy")
- public ResponseEntity<String> proxyRequest(@RequestParam String url) {
- String targetUrl = "http://" + url; // 构造目标URL
- return restTemplate.getForEntity(targetUrl, String.class);
- }
- }

Access-Control-Allow-Origin
通配符。在Java中解决跨域请求问题有多种方法,每种方法都有其适用的场景。选择合适的跨域解决方法可以帮助你保障系统的安全性和稳定性。希望本文能够帮助你更好地理解和处理Java中的跨域请求。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。