From 2a487ae7f881e3e2cb16896b2d2f6f5549601454 Mon Sep 17 00:00:00 2001 From: Josh Cummings Date: Tue, 20 Sep 2022 15:27:31 -0600 Subject: [PATCH] Updated hashcode and equals Closes gh-4133 --- .../WebAuthenticationDetails.java | 53 +++++-------------- 1 file changed, 14 insertions(+), 39 deletions(-) diff --git a/web/src/main/java/org/springframework/security/web/authentication/WebAuthenticationDetails.java b/web/src/main/java/org/springframework/security/web/authentication/WebAuthenticationDetails.java index b4247d08d4..9d038cc9e0 100644 --- a/web/src/main/java/org/springframework/security/web/authentication/WebAuthenticationDetails.java +++ b/web/src/main/java/org/springframework/security/web/authentication/WebAuthenticationDetails.java @@ -17,6 +17,7 @@ package org.springframework.security.web.authentication; import java.io.Serializable; +import java.util.Objects; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpSession; @@ -62,37 +63,6 @@ public class WebAuthenticationDetails implements Serializable { return (session != null) ? session.getId() : null; } - @Override - public boolean equals(Object obj) { - if (obj instanceof WebAuthenticationDetails) { - WebAuthenticationDetails other = (WebAuthenticationDetails) obj; - if ((this.remoteAddress == null) && (other.getRemoteAddress() != null)) { - return false; - } - if ((this.remoteAddress != null) && (other.getRemoteAddress() == null)) { - return false; - } - if (this.remoteAddress != null) { - if (!this.remoteAddress.equals(other.getRemoteAddress())) { - return false; - } - } - if ((this.sessionId == null) && (other.getSessionId() != null)) { - return false; - } - if ((this.sessionId != null) && (other.getSessionId() == null)) { - return false; - } - if (this.sessionId != null) { - if (!this.sessionId.equals(other.getSessionId())) { - return false; - } - } - return true; - } - return false; - } - /** * Indicates the TCP/IP address the authentication request was received from. * @return the address @@ -110,16 +80,21 @@ public class WebAuthenticationDetails implements Serializable { return this.sessionId; } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + WebAuthenticationDetails that = (WebAuthenticationDetails) o; + return Objects.equals(this.remoteAddress, that.remoteAddress) && Objects.equals(this.sessionId, that.sessionId); + } + @Override public int hashCode() { - int code = 7654; - if (this.remoteAddress != null) { - code = code * (this.remoteAddress.hashCode() % 7); - } - if (this.sessionId != null) { - code = code * (this.sessionId.hashCode() % 7); - } - return code; + return Objects.hash(this.remoteAddress, this.sessionId); } @Override