skip to Main Content
@RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(HttpServletResponse response, Model modelAndView, @RequestParam String email, @RequestParam String password, @RequestParam(required = false) String redirect) {
        LoginDTO loginDTO = account.doLogin(email, password);
        if (loginDTO.getLoginMGS() == LoginStatus.LOGGED_IN) {
            Cookie cookie = new Cookie("token", loginDTO.getToken());
            cookie.setMaxAge(24 * 60 * 60);
            response.addCookie(cookie);
            modelAndView.addAttribute("result", "Login successful");
            if (redirect != null && !redirect.equals("")) {
                return "redirect:" + redirect;
            }
        } else {
            modelAndView.addAttribute("result", "Password not same");
        }
        return "login";
    }

when i try to get the cookie like this

private String getToken(Cookie[] cookies) {
        for (Cookie cookie : cookies) {
            System.out.println("cookie name " + cookie.getName() + " " + cookie.getValue());
            if (cookie.getName().equals("token")) {
                return cookie.getValue();
            }
        }
        return null;
    }

it prints: cookie name fm-shopify-session
s%3AsYalW2cHkgglgGNQDfQ3o2hhHAK_7Lrv.pulogz2rfpYWGtZL26K13%2FU6sW7wqUwdKNnvpLgplq8

it not containing token

2

Answers


  1. If you want to access a cookie between requests you’d have to specify path details

    Cookie cookie = new Cookie("token", loginDTO.getToken());
    cookie.setMaxAge(24 * 60 * 60);
    cookie.setPath(path);
    response.addCookie(cookie); 
    
    Login or Signup to reply.
  2. In my case cookie was not created because cookie was “secure” while I was on localhost without https

    cookie.setSecure(true)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search