Skip to content

Commit a4cd6f4

Browse files
Gurunathan16jzheaux
authored andcommitted
Advise Overriding equals() and hashCode() in UserDetails Implementations
This commit adds a documentation note explaining the importance of overriding equals() and hashCode() in custom UserDetails implementations. The default SessionRegistryImpl in Spring Security uses an in-memory ConcurrentMap<Object, Set<String>>, Map<String,SessionInformation> to associate principals with sessions. If a custom UserDetails class does not properly override equals() and hashCode(), user sessions may not be tracked or matched correctly. I believe this helps developers avoid subtle session management issues when implementing custom authentication logic. Signed-off-by: Gurunathan <129361658+Gurunathan16@users.noreply.github.com>
1 parent 5da31ab commit a4cd6f4

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

docs/modules/ROOT/pages/servlet/authentication/session-management.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,13 @@ public class MaximumSessionsPreventLoginTests {
534534
If you are using a customized authentication filter for form-based login, then you have to configure concurrent session control support explicitly.
535535
You can try it using the {gh-samples-url}/servlet/spring-boot/java/session-management/maximum-sessions-prevent-login[Maximum Sessions Prevent Login sample].
536536

537+
[NOTE]
538+
=====
539+
If you are using a custom implementation of `UserDetails`, ensure you override the **equals()** and **hashCode()** methods.
540+
The default `SessionRegistry` implementation in Spring Security relies on an in-memory Map that uses these methods to correctly identify and manage user sessions.
541+
Failing to override them may lead to issues where session tracking and user comparison behave unexpectedly.
542+
=====
543+
537544
== Detecting Timeouts
538545

539546
Sessions expire on their own, and there is nothing that needs to be done to ensure that a security context gets removed.

0 commit comments

Comments
 (0)