springboot 跨域cookie

springboot 跨域cookiespringboot跨域cookie跨域请求默认不会发送cookie数据,需做在请求发送端、服务端做一些配置才能发送、读取cookie数据************************应用1****************config层WebConfig@ConfigurationpublicclassWebConfigimplementsWebMvcConfigurer{@OverridepublicvoidaddViewControllers(V.

大家好,又见面了,我是你们的朋友全栈君。


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

springboot 跨域cookie

****************

点击获取数据、获取数据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数据

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/149878.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 数据结构中 ElemType

    数据结构中 ElemType前段时间,我带了大一的上机课,有好多同学问我,数据结构的struct中,ElemType是什么意思。我当时就操了,尼玛这不就是数据类型嘛!智商啊。后来我就慢慢想了,为什么用ElemType然后#defineElemTypeint来定义奥原来是为了好扩展,如果代码出现变化,我们可以修改最少的代码部分,是他符合你的要求。

    2022年5月12日
    48
  • Request header field Content-Type is not allowed by Access-Control-Allow-Headers跨域

    Request header field Content-Type is not allowed by Access-Control-Allow-Headers跨域跨域错误提示:XMLHttpRequestcannotloadhttp://xxx.com.RequestheaderfieldContent-TypeisnotallowedbyAccess-Control-Allow-Headers.解决方案:例如php服务端程序设置头:header(‘Access-Control-Allow-Origin:*…

    2022年8月24日
    34
  • 智能优化算法:鲸鱼优化算法-附代码

    智能优化算法:鲸鱼优化算法-附代码智能算法:鲸鱼优化算法-附代码文章目录智能算法:鲸鱼优化算法-附代码1.算法原理1.1包围猎物1.2狩猎行为1.3搜索猎物1.4算法流程2.算法结果:参考文献:摘要:鲸鱼优化算法(whaleoptimizationalgorithm,WOA)是2016年由澳大利亚格里菲斯大学的Mirjalili等提出的一种新的群体智能优化算法,其优点在于操作简单,调整的参数少以及跳出局部最优的能力强。1.算法原理鲸鱼优化算法(whaleoptimizationalgorithm,WOA)是

    2022年5月24日
    80
  • python抛出异常写法_零基础学 Python(32):如何抛出和捕获异常?「建议收藏」

    python抛出异常写法_零基础学 Python(32):如何抛出和捕获异常?「建议收藏」1.如何抛出异常?异常的产生有两种来源:一种是程序自动抛出,比如1/0会自动抛出ZeroDivisionError一种是开发者主动抛出,使用raise关键字抛出。在Python中是使用raise关键字来抛出异常的,比如在下面这个函数中,如果不存在目标文件,则会抛出一个Exception通用异常。2.如何捕获异常?出现错误或者异常没有关系,关键在于你要学会预判程序可能会出现…

    2022年10月10日
    0
  • Java面试题及答案整理( 2022最新版,持续更新)[通俗易懂]

    Java面试题及答案整理( 2022最新版,持续更新)[通俗易懂]发现网上很多Java面试题都没有答案,所以花了很长时间搜集整理出来了这套Java面试题大全,希望对大家有帮助哈~Java面试永远是程序员迈向成功的第一个门槛,想要面试成功,各种面试题的洗礼是必不可少的,下面就来看看小编精心整理的一些Java工程师面试题及答案吧。博主已将以下这些面试题整理成了一个Java面试手册,是PDF版的。这套Java面试题大全,希望对大家有帮助哈~博主已将以下这些面试题整理成了一个Java面试手册,是PDF版的1、64位JVM中,int的长度是多数?Ja

    2022年7月7日
    14

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号