springboot跨域cookie
跨域请求默认不会发送cookie数据,需做在请求发送端、服务端做一些配置才能发送、读取cookie数据
************************
应用 1
****************
config层
WebConfig
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/index").setViewName("index");
}
}
****************
controller层
HelloController
@RestController
public class HelloController {
@RequestMapping("/hello")
public Map<String,String> hello(HttpServletRequest request){
Enumeration<String> headers=request.getHeaderNames();
while (headers.hasMoreElements()){
String header=headers.nextElement();
System.out.println(header+" ==> "+request.getHeader(header));
}
Map<String,String> map=new HashMap<>();
map.put("info","hello,瓜田李下");
return map;
}
}
****************
前端页面
index.html
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="/js/jquery-3.5.1.min.js"></script>
<script type="text/javascript">
$(function () {
$("#btn").click(function () {
$.get({
url: "/hello",
success: function (result) {
$("#data").html(result.info)
}
})
});
$("#btn2").click(function () {
$.get({
url: "http://localhost:8081/hello",
crossDomain: true,
xhrFields: {
withCredentials: true
},
success: function (result) {
$("#data2").html(result.info)
}
})
});
})
</script>
</head>
<body>
<div th:align="center">
<button id="btn">获取数据</button>
<button id="btn2">获取数据2</button><br>
<span id="data"></span><br>
<span id="data2"></span>
</div>
</body>
</html>
************************
应用 2
****************
config层
WebConfig
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:8080")
.allowCredentials(true);
}
}
****************
controller层
HelloController
@RestController
public class HelloController {
@RequestMapping("/hello")
public Map<String,String> hello(HttpServletRequest request){
Enumeration<String> headers=request.getHeaderNames();
while (headers.hasMoreElements()){
String header=headers.nextElement();
System.out.println(header+" ==> "+request.getHeader(header));
}
Map<String,String> map=new HashMap<>();
map.put("info","欢迎你");
return map;
}
}
************************
使用测试
localhost:8080/index
****************
点击获取数据、获取数据2,控制台输出
应用 1
2020-07-08 10:56:09.407 INFO 1488 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-07-08 10:56:09.414 INFO 1488 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 7 ms
host ==> localhost:8080
connection ==> keep-alive
accept ==> */*
x-requested-with ==> XMLHttpRequest
user-agent ==> Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
referer ==> http://localhost:8080/index
accept-encoding ==> gzip, deflate, br
accept-language ==> zh-CN,zh;q=0.9
cookie ==> isg=BJiYP7OIcordkV4OUKLufIy5acYqgfwLBTEbVdKJqlOGbThXepNTm7QMoaXdorTj
应用 2
2020-07-08 10:56:22.400 INFO 16104 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-07-08 10:56:22.410 INFO 16104 --- [nio-8081-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 10 ms
host ==> localhost:8081
connection ==> keep-alive
accept ==> */*
origin ==> http://localhost:8080
user-agent ==> Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
referer ==> http://localhost:8080/index
accept-encoding ==> gzip, deflate, br
accept-language ==> zh-CN,zh;q=0.9
cookie ==> isg=BJiYP7OIcordkV4OUKLufIy5acYqgfwLBTEbVdKJqlOGbThXepNTm7QMoaXdorTj
应用 2可读取属于应用 1的cookie数据