feat(server): persist HttpSession across server restarts#154
Open
OGoneZ wants to merge 1 commit intoSathvik-Rao:mainfrom
Open
feat(server): persist HttpSession across server restarts#154OGoneZ wants to merge 1 commit intoSathvik-Rao:mainfrom
OGoneZ wants to merge 1 commit intoSathvik-Rao:mainfrom
Conversation
Replaces the in-memory SessionRegistry with a JDBC-backed Spring Session store so that a container or process restart no longer evicts every active session. Without this fix every desktop and mobile client is forced back to the login screen on each redeploy (see Sathvik-Rao#17 — maintainer acknowledged: "A container restart will log out devices because the session keys are removed"). - pom.xml: add spring-session-jdbc (spring-session-core was already on the classpath but had no store backend wired in). - application.properties: enable the JDBC store with automatic schema init; pin server.servlet.session.cookie.name=JSESSIONID so existing clients stay compatible; add WRITE_DELAY=0 to the H2 URL so session writes are durable across an abrupt container stop. - SecurityConfiguration: swap SessionRegistryImpl for SpringSessionBackedSessionRegistry so the admin "active devices" view also reads from the persistent store. - UserPrincipal / Users: implement Serializable. BruteForceProtectionService is marked transient (it is a Spring bean, not session state); isAccountNonLocked() is null-guarded for the rehydrated path. - SessionService: SpringSessionBackedSessionRegistry.getAllPrincipals() deliberately throws UnsupportedOperationException, which would 500 every "log this user out" path (admin force-logoff, Log off from all devices, username/password change, user delete). Replaced with FindByIndexNameSessionRepository.findByPrincipalName() + deleteById() — same semantics, indexed lookup. Verified locally: login → docker restart → original cookie still authenticates and the dashboard renders without re-login.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Persist
HttpSessionacross server restarts so existing desktop and mobile clients are no longer forced back to the login screen on every redeploy.Relates to #17 — long-standing self-hoster pain point. From that thread: "A container restart will log out devices because the session keys are removed." This PR fixes that root cause.
What changes
pom.xml— addspring-session-jdbc. (spring-session-corewas already on the classpath but had no store backend wired in, so persistence was never actually active.)application.propertiesserver.servlet.session.cookie.name=JSESSIONIDso already-deployed clients keep working with no updateWRITE_DELAY=0to the default H2 URL so session writes are durable across an abruptdocker stop(without it, the most recent session updates can be lost from the file store on shutdown)SecurityConfiguration— swapSessionRegistryImplforSpringSessionBackedSessionRegistry, so the admin "active devices" view also reads from the persistent store.UserPrincipal/Users— implementSerializable.BruteForceProtectionServiceis markedtransient(it is a Spring bean, not session state);isAccountNonLocked()is null-guarded for the rehydrated path so a restored session doesn't NPE on the now-null service reference. (Brute-force gating still applies to fresh login attempts, which always go throughMyUserDetailsServiceand get a fully wired principal.)SessionService—SpringSessionBackedSessionRegistry.getAllPrincipals()deliberately throwsUnsupportedOperationException(Spring Session doesn't index across all principals). The previous implementation called it from every "log this user out" path:ClipCascadeController#486)ClipCascadeController#495)FacadeUserService#79)FacadeUserService#99)FacadeUserService#129)All five would now return HTTP 500. Replaced with
FindByIndexNameSessionRepository.findByPrincipalName()+deleteById()— same end-user semantics (cookie invalidated immediately), indexed lookup, no full scan.Compatibility
JSESSIONID); existing desktop and mobile clients continue to work without any client-side update.SPRING_SESSIONandSPRING_SESSION_ATTRIBUTES. Application tables are unaffected.How to verify
Verified locally: login →
docker restart→ request to/admin/advancewith the original cookie returns HTTP 200 and renders the dashboard.Notes
Happy to split this further if preferred (e.g.
Serializableprep first, then the registry swap, thenSessionService). Kept it as one PR because the three pieces are individually broken without each other — addingspring-session-jdbcwithoutSerializableon the principal would crash on the first request, and swapping the registry without fixingSessionServicewould 500 every user-management action.