1
0
mirror of synced 2026-08-04 01:07:02 +00:00

Implement Equals and HashCode

Internally, RequestMatcher is sometimes used as a key to a
HashMap. Accordingly, each implementation should implement
equals and hashCode.

Closes gh-17842
This commit is contained in:
Josh Cummings
2025-09-03 16:28:57 -06:00
parent 24ffda28d8
commit bd119ac411
3 changed files with 58 additions and 0 deletions
@@ -20,6 +20,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import jakarta.servlet.http.HttpServletRequest;
@@ -251,6 +252,26 @@ public final class MediaTypeRequestMatcher implements RequestMatcher {
this.ignoredMediaTypes = ignoredMediaTypes;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof MediaTypeRequestMatcher that)) {
return false;
}
return Objects.equals(this.contentNegotiationStrategy.getClass(), that.contentNegotiationStrategy.getClass())
&& Objects.equals(this.useEquals, that.useEquals)
&& Objects.equals(this.matchingMediaTypes, that.matchingMediaTypes)
&& Objects.equals(this.ignoredMediaTypes, that.ignoredMediaTypes);
}
@Override
public int hashCode() {
return Objects.hash(this.contentNegotiationStrategy.getClass(), this.useEquals, this.matchingMediaTypes,
this.ignoredMediaTypes);
}
@Override
public String toString() {
return "MediaTypeRequestMatcher [contentNegotiationStrategy=" + this.contentNegotiationStrategy
@@ -16,6 +16,8 @@
package org.springframework.security.web.util.matcher;
import java.util.Objects;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.util.Assert;
@@ -47,6 +49,22 @@ public class NegatedRequestMatcher implements RequestMatcher {
return !this.requestMatcher.matches(request);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof NegatedRequestMatcher that)) {
return false;
}
return Objects.equals(this.requestMatcher, that.requestMatcher);
}
@Override
public int hashCode() {
return Objects.hash(this.requestMatcher);
}
@Override
public String toString() {
return "Not [" + this.requestMatcher + "]";
@@ -16,6 +16,8 @@
package org.springframework.security.web.util.matcher;
import java.util.Objects;
import jakarta.servlet.http.HttpServletRequest;
import org.jspecify.annotations.Nullable;
@@ -91,6 +93,23 @@ public final class RequestHeaderRequestMatcher implements RequestMatcher {
return this.expectedHeaderValue.equals(actualHeaderValue);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof RequestHeaderRequestMatcher that)) {
return false;
}
return Objects.equals(this.expectedHeaderName, that.expectedHeaderName)
&& Objects.equals(this.expectedHeaderValue, that.expectedHeaderValue);
}
@Override
public int hashCode() {
return Objects.hash(this.expectedHeaderName, this.expectedHeaderValue);
}
@Override
public String toString() {
return "RequestHeaderRequestMatcher [expectedHeaderName=" + this.expectedHeaderName + ", expectedHeaderValue="