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)
上一篇 2022年6月17日 下午1:00
下一篇 2022年6月17日 下午1:00


相关推荐

  • Cocos图片加密与解密

    Cocos图片加密与解密现在做的cocos项目没有对资源进行加密处理,发布出来的APK一旦被人解包,则所有图片资源都会暴露出来,为了避免图片资源被人恶意使用,所有我准备给自己项目中使用到的图片进行简单加密,这样可以防住一部分解包伸手党。我们这里采用最常见的**异或加密**,*异或加密性质:一个数异或同一个数两次,得到的是本身*。根据这个性质,我们可以采用把图片的字节流进行异或加密,只需要设置一个Key,在本地客户端使用…

    2022年6月21日
    33
  • 盗取QQ密码的顽固的IEXPLORE.EXE病毒

    盗取QQ密码的顽固的IEXPLORE.EXE病毒立刻加入博客人自己的广告网现象: 开机以后不久,在进程里面会出现多个IEXPLORE.EXE进程,用户名都是SYSTEM,杀掉进程之后,过一段时间就会重新启动这个进程。而且IEXPLORE.EXE进程的cpu占用率常常达到100%!计算机根本就无法使用。在进行拨号连网后,系统可能出现重起.甚是恼人!此病毒自动禁用某些杀毒软件,看来全面手工杀毒的时代即将来临!查杀方法:此病

    2022年7月20日
    29
  • 【学习笔记】CSS深入理解之absolute「建议收藏」

    【学习笔记】CSS深入理解之absolute「建议收藏」【学习笔记】CSS深入理解之absolute

    2022年4月21日
    43
  • 教你用笔记本破解无线路由器password

    教你用笔记本破解无线路由器password

    2021年11月29日
    42
  • 海明校验码的计算及检验

    海明校验码的计算及检验海明校验码的计算及检验目录海明校验码的计算及检验知识背景计算海明校验码步骤一 计算校验码位数内容一 线性布局内容二 约束布局内容三 表格布局检验最近和朋友探讨一个海明校验码的题目 因为学了很久所以有些记不清了 趁这这个机会 复习了一下海明校验码及校验过程 以此为记录 知识背景百度百科 由 RichardHammi 于 1950 年提出 还被广泛采用的一种很有效的校验方法 是只要增加少

    2026年3月19日
    2
  • openclaw如何重置密码 openclaw找回密码详细操作步骤【指南】

    openclaw如何重置密码 openclaw找回密码详细操作步骤【指南】

    2026年3月13日
    2

发表回复

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

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