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:
+21
@@ -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
|
||||
|
||||
+18
@@ -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 + "]";
|
||||
|
||||
+19
@@ -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="
|
||||
|
||||
Reference in New Issue
Block a user