可以在前端使用cookie,但在邮递员中效果很好

发布于 2025-02-01 08:14:23 字数 764 浏览 6 评论 0原文

我已经在Springboot中创建了REST API,该API在发送有效的凭据返回后,JWT访问令牌并保存在Httponly Cookie中的刷新令牌,当我将cookie在Postman中使用cookie的API发送时,它可以正常工作,但是当我尝试在React中获取React时, API返回错误,表明没有cookie

error_message:“无法读取数组长度,因为“数组”为null”

因此我必须指定域名,如果是,我在Localhost上工作时该如何执行(Springboot正在使用localhost:8080 and Code> and code>和React Localhost:3000)。

我目前如何创建cookie:

public static Cookie createRefreshCookie(String refresh_token) {
        Cookie jwtRefreshCookie = new Cookie("refresh_token", refresh_token);
        jwtRefreshCookie.setMaxAge(2678400);
        jwtRefreshCookie.setHttpOnly(true);
        jwtRefreshCookie.setPath("/");
        return jwtRefreshCookie;
    }

I've created rest API in springboot that upon sending valid credentials returns jwt access token and saves refresh token in httpOnly cookie, when I send GET request to the API that uses cookie in postman it works just fine but when I try to fetch in react API returns error that indicates that there is no cookies

error_message: "Cannot read the array length because "array" is null"

So do I have to specify domain name If yes how do I do it while working on localhost (springboot is working on localhost:8080 and react localhost:3000).

How I currently create cookie:

public static Cookie createRefreshCookie(String refresh_token) {
        Cookie jwtRefreshCookie = new Cookie("refresh_token", refresh_token);
        jwtRefreshCookie.setMaxAge(2678400);
        jwtRefreshCookie.setHttpOnly(true);
        jwtRefreshCookie.setPath("/");
        return jwtRefreshCookie;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看轻我的陪伴 2025-02-08 08:14:23

问题是由我在react中的提取请求造成的,该请求包含标头withCredentials:true来自库Axios的true axios insead forth fetter实现标题凭据:“ inclage”

Problem was caused by my fetch request in react that contained header withCredentials: true from library axios insead of fetch implementation of header credentials:"include".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文