-1

Spring boot doesn't save sessions between requests. I have to methods: /login and another one is /getInfo. They're placed in different controllers. DB for sessions is MongoDB and I can see through Mongo Compass that there are adding new sessions documents. So, I'm making post request to /login, where session are creating and after that I'm making request to /getInfo where I want to take data from session and I get This is an screen of session and the data in each sessions(including sessions id). As result I have two different sessions

This is how the session sets:

session.setAttribute("username", resp.get("username"));

This is how data are getting from session:

session.getAttribute("username")

Image of request data #1

Image of request data #2

user303680
  • 15
  • 1
  • 4
  • 1
    From the screenshot, I don't see a cookie with a JSESSIONID being sent in the request headers. Because there isn't a session ID being passed in, a new session is created each time. The response headers would then contain a Set-Cookie header with the created session - JSESSIONID. It is expected for this to be passed back for the server to lookup the corresponding session in subsequent requests. – Tim Tong Jan 31 '22 at 01:33
  • Can you add more details like how the session object is being injected in your code, which could help to identify the issue? – Anil Kumar Athuluri Jan 31 '22 at 01:46
  • [This is GitHub repository with part of code](https://github.com/Abuzik/stackIssue) – user303680 Jan 31 '22 at 07:33

1 Answers1

0

For anyone who will use @RestController with sessions add two rows into your controller:

    @Autowired
    private HttpSession httpSession;
user303680
  • 15
  • 1
  • 4