Web开发经常会遇到跨域问题,解决方案有:jsonp,iframe,CORS等等。 CORS 与 JSONP 相比: 1、 JSONP 只能实现 GET 请求,而 CORS 支持所有类型的 HTTP 请求。 2、 使用 CORS,开发者可以使用普通的 XMLHttpRequest 发起请求和获得数据,比起 JSONP 有更好的 错误处理。 3、 JSONP 主要被老的浏览器支持,它们往往不支持 CORS,而绝大多数现代浏览器都已经支持了 CORS。
这里我仅仅写出一个需要被跨域访问的方法,提出了三种解决方案。 需要被跨域访问的方法:
package com.lemon.springboot.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* @author lemon
*/
@RestController
@RequestMapping("/api")
public class ApiController {
private static final Logger logger = LoggerFactory.getLogger(ApiController.class);
// @CrossOrigin({"http://localhost:8081", "http://localhost:8082"})
@RequestMapping("/get")
public Map<String, Object> get(@RequestParam String name) {
Map<String, Object> map = new HashMap<>();
map.put("title", "hello world");
map.put("name", name);
return map;
}
}
写一个配置类,指定可以被跨域访问的路径以及可以跨域的主机链接。这样,8081和8082端口的应用就可以访问/api后所有的方法或者资源。
package com.lemon.springboot.configuration;
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;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* 全局配置跨域问题
* @author lemon
*/
@Configuration
public class CustomCorsConfiguration {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
// 设置了可以被跨域访问的路径和可以被哪些主机跨域访问
registry.addMapping("/api/**").allowedOrigins("http://localhost:8081", "http://localhost:8082");
}
};
}
}
package com.lemon.springboot.configuration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* @author lemon
*/
@Configuration
public class CustomCorsConfiguration2 extends WebMvcConfigurerAdapter {
@Override
public void addCorsMappings(CorsRegistry registry) {
// 设置了可以被跨域访问的路径和可以被哪些主机跨域访问
registry.addMapping("/api/**").allowedOrigins("http://localhost:8081", "http://localhost:8082");
}
}
直接在需要被跨域访问的方法上加上@CrossOrigin注解就可以实现被跨域访问。
package com.lemon.springboot.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* @author lemon
*/
@RestController
@RequestMapping("/api")
public class ApiController {
private static final Logger logger = LoggerFactory.getLogger(ApiController.class);
@CrossOrigin({"http://localhost:8081", "http://localhost:8082"})
@RequestMapping("/get")
public Map<String, Object> get(@RequestParam String name) {
Map<String, Object> map = new HashMap<>();
map.put("title", "hello world");
map.put("name", name);
return map;
}
}
这三种配置方法,当其他系统内的资源跨域访问的时候,就会起作用!