方式1:使用@CrossOrigin注解方式
@CrossOrigin(origins = "*") //解决跨域的关键注解,该注解可以标注在类上也可以标注在方法上
域A:http://localhost/across_test/test.html
域A下的test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="./jquery.js"></script>
</head>
<body>
<button id="btn">测试ajax跨域请求</button>
<script>
$("#btn").click(function () {
$.ajax({
type: "get",
url: "http://localhost:8080/cros_demo",
success: function (json) {
console.log(json)
},
error: function () {
alert('fail');
}
});
});
</script>
</body>
</html>
域B:http://localhost:8080/cros_demo
域B下的HelloController.java
@CrossOrigin注解标注在类上
package com.example.controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
@RestController
@CrossOrigin(origins = "*") //解决跨域的关键注解,方式一:标注在类上
public class HelloController {
@RequestMapping("/cros_demo")
public HashMap<Integer, String> crosDemo(HttpServletResponse response) {
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "Shanghai");
map.put(2, "Beijing");
return map;
}
}
@CrossOrigin注解标注在方法上
package com.example.controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
@RestController
public class HelloController {
@RequestMapping("/cros_demo")
@CrossOrigin(origins = "*") //解决跨域的关键注解,方式二:标注在方法上
public HashMap<Integer, String> crosDemo(HttpServletResponse response) {
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "Shanghai");
map.put(2, "Beijing");
return map;
}
}
效果:
方式2: 配置文件解决全局跨域问题
编写配置文件自定义CorsConfig.java
CorsConfig.java
package com.example.cros;
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("/**")
.allowCredentials(true)
.allowedOriginPatterns("*")
.allowedMethods("GET", "POST")
.allowedHeaders("*")
.exposedHeaders("*");
}
}
HelloController.java
package com.example.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
@RestController
public class HelloController {
@RequestMapping("/cros_demo")
public HashMap<Integer, String> crosDemo(HttpServletResponse response) {
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "Shanghai");
map.put(2, "Beijing");
return map;
}
}
效果如下:
方式3: 通过 CorsFilter 跨域
MyCorsFilter.java
package com.example.cros;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@Configuration
public class MyCorsFilter {
@Bean
public CorsFilter corsFilter() {
// 1.创建 CORS 配置对象
CorsConfiguration config = new CorsConfiguration();
// 支持域
config.addAllowedOriginPattern("*");
// 是否发送 Cookie
config.setAllowCredentials(true);
// 支持请求方式
config.addAllowedMethod("*");
// 允许的原始请求头部信息
config.addAllowedHeader("*");
// 暴露的头部信息
config.addExposedHeader("*");
// 2.添加地址映射
UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
corsConfigurationSource.registerCorsConfiguration("/**", config);
// 3.返回 CorsFilter 对象
return new CorsFilter(corsConfigurationSource);
}
}
HelloController.java
package com.example.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
@RestController
public class HelloController {
@RequestMapping("/cros_demo")
public HashMap<Integer, String> crosDemo(HttpServletResponse response) {
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "Shanghai");
map.put(2, "Beijing");
return map;
}
}
效果:
方式4: 添加请求头方式
添加请求头:
Access-Control-Allow-Origin
HelloController.java
package com.example.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
@RestController
public class HelloController {
@RequestMapping("/cros_demo")
public HashMap<Integer, String> crosDemo(HttpServletResponse response) {
// 设置跨域
response.setHeader("Access-Control-Allow-Origin", "*");
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "Shanghai");
map.put(2, "Beijing");
return map;
}
}
效果:
方式5: jsonp方式
省略,参考: https://blog.csdn.net/shj_php/article/details/130128484