/** * 全局设置 * */ @Configuration public class CustomCorsConfiguration2 extends WebMvcConfigurerAdapter { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/api/**").allowedOrigins("http://localhost:8080"); } } /** *细粒度配置 * */ @RestController @RequestMapping(value = "/api", method = RequestMethod.POST) public class ApiController { @CrossOrigin(origins = "http://localhost:8080") @RequestMapping(value = "/get") public HashMap<String, Object> get(@RequestParam String name) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("msg", "跨域测试"); map.put("name", name); return map; } }