无法在spring boot中获取cookie值

oknwwptz  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(602)

当用户登录时,我使用javascript将jwt令牌保存在cookies中

document.cookie = "token=Bearer " + userDetails.loginSuccess.token;

但是当我向spring引导控制器发出请求时,我找不到cookie

public String home(@CookieValue(value = "token") String token,
                    @PathVariable String businessId,
                    @PathVariable String cartId, ModelMap model,
                    HttpServletRequest request) {}

如何在控制器中获取它的cookie值?

fzsnzjdm

fzsnzjdm1#

您可以直接在控制器中获取httpservletrequest对象,然后像这样获取cookies。

public ResponseEntity<String> home(
  HttpServletRequest request,
  HttpServletResponse response,
  @RequestBody Map<String, String> params) {
      request.getCookies();
  }
xj3cbfub

xj3cbfub2#

应该是name=“token”而不是value

public String home(@CookieValue(name = "token") String token,
                    @PathVariable String businessId,
                    @PathVariable String cartId, ModelMap model,
                    HttpServletRequest request) {}

相关问题