无法在一个端点上访问 cookie,但可以在另一端点上访问 cookie

发布于 2025-01-19 10:08:05 字数 2245 浏览 0 评论 0原文

在这件事上我需要你的帮助。看来我有薛定谔的饼干!

我正在使用 Spring Boot 和 Java。我可以像这样成功创建 cookie:

public String createNewCookie(HttpServletResponse response) {
        // create a cookie
        String newToken = AccountsService.generateRandomCode(6);
        Cookie cookie = new Cookie("clrToken", newToken);
        cookie.setMaxAge(10 * 365 * 24 * 60 * 60); // expires in 10 years
        cookie.setSecure(true);
        cookie.setHttpOnly(true);

        //add cookie to response
        response.addCookie(cookie);

        return newToken;
    }

我可以轻松地获取创建的 cookie 并在我的一个控制器中读取其值(令牌),如下所示:

@PostMapping(value = "/public/save",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Operation(summary = "Save new affiliate click into database")
    public ResponseEntity<AffiliateClickDto> saveAffiliateClick
            (@RequestParam Long serviceId,
             @CookieValue(value = "clrToken", defaultValue = "") final String token) {
        return new ResponseEntity<>(affiliateClickService.saveAffiliateClick(serviceId, token), HttpStatus.OK);
    }

但我无法从另一个控制器中的另一个端点获取相同的 cookie。

@GetMapping(value = "/public/servicesByFilters",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Operation(summary = "Get all Providers Services for filters")
    public ResponseEntity<List<ServiceResultPageDTO>> getAllProvidersServicesForFilters
            (@RequestParam final Map<String, String> params,
            @CookieValue(value = "clrToken", defaultValue = "") final String token) {
        return new ResponseEntity<>(services.getAllProvidersServiceForFilters(params, token), HttpStatus.OK);
    }

我得到一个空字符串作为字符串标记参数值。

我也尝试使用循环来迭代 cookie,但我没有在第二个端点获得“clrToken”。我可以访问其他一些 cookie。

public String readAllCookies(HttpServletRequest request) {

        String token="";

        Cookie[] cookies = request.getCookies();
        for (Cookie c : cookies) {
            if (Objects.equals(c.getName(), "clrToken")) {
                token = c.getValue();
                break;
            }
        }

        return token;
    }

谁吃我的饼干??? :D 有谁知道这里发生了什么吗?如果您需要其他信息,请询问。

I need your help on this matter. It looks like I have the Schrodinger's cookie!

I am using Spring Boot with Java. I can successfully create the cookie like this:

public String createNewCookie(HttpServletResponse response) {
        // create a cookie
        String newToken = AccountsService.generateRandomCode(6);
        Cookie cookie = new Cookie("clrToken", newToken);
        cookie.setMaxAge(10 * 365 * 24 * 60 * 60); // expires in 10 years
        cookie.setSecure(true);
        cookie.setHttpOnly(true);

        //add cookie to response
        response.addCookie(cookie);

        return newToken;
    }

I can easily fetch created cookie and read its value (token) in one of my controllers like this:

@PostMapping(value = "/public/save",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Operation(summary = "Save new affiliate click into database")
    public ResponseEntity<AffiliateClickDto> saveAffiliateClick
            (@RequestParam Long serviceId,
             @CookieValue(value = "clrToken", defaultValue = "") final String token) {
        return new ResponseEntity<>(affiliateClickService.saveAffiliateClick(serviceId, token), HttpStatus.OK);
    }

But I can not fetch that same cookie from another endpoint in my other controller.

@GetMapping(value = "/public/servicesByFilters",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Operation(summary = "Get all Providers Services for filters")
    public ResponseEntity<List<ServiceResultPageDTO>> getAllProvidersServicesForFilters
            (@RequestParam final Map<String, String> params,
            @CookieValue(value = "clrToken", defaultValue = "") final String token) {
        return new ResponseEntity<>(services.getAllProvidersServiceForFilters(params, token), HttpStatus.OK);
    }

I get an empty String for The String token parameter value.

I allso tried to use the loop to iterate through cookies, but I do not get my "clrToken" at this second endpoint. I can access some other cookies.

public String readAllCookies(HttpServletRequest request) {

        String token="";

        Cookie[] cookies = request.getCookies();
        for (Cookie c : cookies) {
            if (Objects.equals(c.getName(), "clrToken")) {
                token = c.getValue();
                break;
            }
        }

        return token;
    }

Who eat my cookie??? :D Does anyone have idea what is happening here? If you need some other info, please ask.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文