Skip to content

Instantly share code, notes, and snippets.

@vernetto
Created July 14, 2023 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vernetto/b9c55b5dc1243da7107b15b423ca21a1 to your computer and use it in GitHub Desktop.
Save vernetto/b9c55b5dc1243da7107b15b423ca21a1 to your computer and use it in GitHub Desktop.
Spring Session
in a web application that has been upgraded from Spring 4 to Spring 5, we notice that the sessionId changes from request to request, even when we load static files. This doesn't happen if the static file is in the browser cache.
ChatGPT
In a correctly functioning web application, the session ID should remain constant for a particular user session, not changing from request to request. If you observe that the session ID is changing from request to request after upgrading from Spring 4 to Spring 5, this indicates that there might be an issue with session management in your application. Here are a few things to check:
Session Creation Policy: In Spring Security, the session creation policy might be causing a new session to be created for every request. You should check this setting. By default, Spring Security will create a session when necessary (if the session does not already exist). However, it's possible that this policy has been changed.
Cookies Settings: The JSESSIONID is usually stored in a cookie. If the cookie is not being sent or saved correctly, this could result in a new session being created for every request. This might be a browser issue, or it could be related to the configuration of your application. For example, it's possible that the Secure or HttpOnly flags are being set incorrectly, or that the cookie is being set on the wrong domain or path.
Spring Security Configuration: Spring 5 introduced a new way of configuring Spring Security, which is different from Spring 4. If you migrated your security configuration to the new method, make sure you didn't introduce any errors or changes in behavior. Specifically, check if you are using the correct CSRF and session management settings.
Servlet API Compatibility: Spring 5 requires Servlet 3.1 or later. If your application server is using an older Servlet API, this might cause issues with session management.
Stateless Rest APIs: If you are using a stateless rest API, it doesn't maintain any state between the requests, so the session ID will be different for each request.
Third-party libraries: If your application is using third-party libraries that handle sessions, make sure they are compatible with Spring 5. These libraries may behave differently in different versions of Spring, which could result in changes to session management.
Debugging: You can enable debug logs for Spring Security to get more information about what's happening. This might help you track down the issue.
Remember that static files usually do not need a session. Therefore, the session should not change when loading static files, unless they are protected resources and Spring Security is involved. If the static files are in the browser cache, no request is made to the server so the server doesn't have a chance to change the session.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment