后端是在Spring引导上构建的,而前端是使用VueJS构建的。
我正在从Spring Boot中设置示例Cookie
@PostMapping(path = "/login")
@ResponseBody
public ResponseEntity<?> loginUser(HttpServletRequest httpRequest, HttpServletResponse httpResponse, @RequestBody final User user) {
try {
SessionResponse sessionResponse = loginService.authenticateUser(user);
Cookie cookie = new Cookie("token", sessionResponse.getToken());
httpResponse.addCookie(cookie);
return new ResponseEntity<SessionResponse>(sessionResponse, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>("Failed with exception " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
使用以下代码段从请求中获取Cookie
@GetMapping(path = "/dashboard")
@ResponseBody
public ResponseEntity<?> dashboardData(HttpServletRequest httpRequest) {
try {
String cookies = getCookies(httpRequest);
getLogger().info("dashboardData -> cookies :: " + cookies);
// will get dashboard data
return new ResponseEntity<>(dashboardResponse, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>("Failed with exception " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
private String getCookies(HttpServletRequest httpRequest) {
Cookie[] cookies = httpRequest.getCookies();
getLogger().info(cookies);
if (cookies != null) {
return Arrays.stream(cookies).map(c -> c.getName() + "=" + c.getValue()).collect(Collectors.joining(", "));
}
return null;
}
但是我得到null