springboot解决前端跨域请求浏览器不能接收和携带相同cookie到服务器

本文讲述了作者在学习Java全栈过程中遇到的跨域问题,涉及到前后端设置credentials和cookie,以及如何通过SpringMVC的CorsConfig和SameSiteConfig配置解决浏览器的same-site策略限制。解决方案包括在servlet容器级别和YAML配置文件中调整cookie的same-site和secure属性。
摘要由CSDN通过智能技术生成

项目场景:

记录一下今天在进行java全栈学习的过程中出现的问题

问题描述

前后端都设置了credentialstrue,但是同一个浏览器每次访问服务端都无法获取并携带到相同cookie到服务端。
后来延伸出另一个问题,通过yml配置文件可以轻松解决,但是不能通过配置类解决这个问题。后来把重心转移到如何对servlet容器进行设置才引刃而解。

前端请求代码如下:

async sendWithConfig() {
    const _axios = axios.create({ //创建请求配置
        baseURL: 'http://127.0.0.1:8080',
        withCredentials: true
    });
    const resp1 = await _axios.post('/api/post/session/set', {});
    const resp2 = await _axios.post('/api/post/session/get', {});
    console.log(resp1.data);
    console.log(resp2.data);
}

java跨域配置如下:

@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost")
                .maxAge(60000)
                .allowCredentials(true)
                .allowedHeaders("*")
                .allowedMethods("*");
    }
}

后端接口如下:

@PostMapping("/post/session/set")
public ResultVo<String> setSession(HttpSession httpSession) {
      logger.info("{}", httpSession.getId());
      httpSession.setAttribute("cookie", httpSession.getId());
      return ResultVo.respWithoutData(ResultEnums.SUCCESS);
  }

@PostMapping("/post/session/get")
public ResultVo<String> getSession(HttpSession httpSession) {
    logger.info("{}", httpSession.getId());
    logger.info("{}", httpSession.getAttribute("cookie"));
    return ResultVo.respWithoutData(ResultEnums.SUCCESS);
}

原因分析:

浏览器在进行跨域访问的时候由于默认的same-site策略,导致浏览器无法接收第三方不信任的cookie,每次发起请求的时候也就无法携带相同cookie到服务端,于是服务端每次都会生成新的cookie 返回。

解决方案:

除了在前后端中配置credentials以外,还要在后端配置类中配置same-sitesecure

方式一 ,通过配置类对servlet容器进行配置:

@Configuration
public class SameSiteConfig {

    //对servlet容器进行配置,采用这种方式才可以把值设置到servlet容器中
    @Bean
    public WebServerFactoryCustomizer<TomcatServletWebServerFactory> webServerFactoryCustomizer() {
        return factory -> {
            Session.Cookie cookie = factory.getSession().getCookie();
            cookie.setSameSite(Cookie.SameSite.NONE); //设置 SameSite 为 none,不依赖浏览器的same-site策略
            cookie.setSecure(true); //还需要配置设置 secure 浏览器才能接收并携带相同cookie
        };
    }
}

方式二,通过yml配置文件对servlet进行配置:

server:
  port: 8080
  servlet:
    context-path: /api
    session:
      cookie:
        same-site: none
        secure: true

验证:两次请求都是相同cookie

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白条君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值