无法在一个端点上访问 cookie,但可以在另一端点上访问 cookie
在这件事上我需要你的帮助。看来我有薛定谔的饼干!
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论