Monday, August 10, 2020

Cookies sharing in Spring boot applications

    @GetMapping("/greeting")
    public String greeting(HttpSession session, HttpServletRequest request, HttpServletResponse response, @RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);
        System.out.println(session);
        System.out.println(request);
        for (Cookie cookie : request.getCookies()) {
            System.out.println(cookie.getName() + " " + cookie.getValue());
        }
        Cookie uiColorCookie = new Cookie("color2", "red2");
        uiColorCookie.setDomain("localhost");
        uiColorCookie.setPath("/");
        response.addCookie(uiColorCookie);
        return "greeting";
    }

the code of the 2 applications is: 

https://github.com/vernetto/demosession01 

https://github.com/vernetto/demosession02 

http://localhost:8181/books01/greeting?name=pirla 

http://localhost:8282/books02/greeting?name=pirla 

In Firefox, use F12 and "storage" to examine cookies. 

Using embedded Tomcat, the session turns out to be:

org.apache.catalina.session.StandardSessionFacade https://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/session/StandardSessionFacade.html