Skip to content

Commit bd119ac

Browse files
committed
Implement Equals and HashCode
Internally, RequestMatcher is sometimes used as a key to a HashMap. Accordingly, each implementation should implement equals and hashCode. Closes spring-projectsgh-17842
1 parent 24ffda2 commit bd119ac

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

web/src/main/java/org/springframework/security/web/util/matcher/MediaTypeRequestMatcher.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Collection;
2121
import java.util.Collections;
2222
import java.util.List;
23+
import java.util.Objects;
2324
import java.util.Set;
2425

2526
import jakarta.servlet.http.HttpServletRequest;
@@ -251,6 +252,26 @@ public void setIgnoredMediaTypes(Set<MediaType> ignoredMediaTypes) {
251252
this.ignoredMediaTypes = ignoredMediaTypes;
252253
}
253254

255+
@Override
256+
public boolean equals(Object obj) {
257+
if (this == obj) {
258+
return true;
259+
}
260+
if (!(obj instanceof MediaTypeRequestMatcher that)) {
261+
return false;
262+
}
263+
return Objects.equals(this.contentNegotiationStrategy.getClass(), that.contentNegotiationStrategy.getClass())
264+
&& Objects.equals(this.useEquals, that.useEquals)
265+
&& Objects.equals(this.matchingMediaTypes, that.matchingMediaTypes)
266+
&& Objects.equals(this.ignoredMediaTypes, that.ignoredMediaTypes);
267+
}
268+
269+
@Override
270+
public int hashCode() {
271+
return Objects.hash(this.contentNegotiationStrategy.getClass(), this.useEquals, this.matchingMediaTypes,
272+
this.ignoredMediaTypes);
273+
}
274+
254275
@Override
255276
public String toString() {
256277
return "MediaTypeRequestMatcher [contentNegotiationStrategy=" + this.contentNegotiationStrategy

web/src/main/java/org/springframework/security/web/util/matcher/NegatedRequestMatcher.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.security.web.util.matcher;
1818

19+
import java.util.Objects;
20+
1921
import jakarta.servlet.http.HttpServletRequest;
2022

2123
import org.springframework.util.Assert;
@@ -47,6 +49,22 @@ public boolean matches(HttpServletRequest request) {
4749
return !this.requestMatcher.matches(request);
4850
}
4951

52+
@Override
53+
public boolean equals(Object obj) {
54+
if (this == obj) {
55+
return true;
56+
}
57+
if (!(obj instanceof NegatedRequestMatcher that)) {
58+
return false;
59+
}
60+
return Objects.equals(this.requestMatcher, that.requestMatcher);
61+
}
62+
63+
@Override
64+
public int hashCode() {
65+
return Objects.hash(this.requestMatcher);
66+
}
67+
5068
@Override
5169
public String toString() {
5270
return "Not [" + this.requestMatcher + "]";

web/src/main/java/org/springframework/security/web/util/matcher/RequestHeaderRequestMatcher.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.security.web.util.matcher;
1818

19+
import java.util.Objects;
20+
1921
import jakarta.servlet.http.HttpServletRequest;
2022
import org.jspecify.annotations.Nullable;
2123

@@ -91,6 +93,23 @@ public boolean matches(HttpServletRequest request) {
9193
return this.expectedHeaderValue.equals(actualHeaderValue);
9294
}
9395

96+
@Override
97+
public boolean equals(Object obj) {
98+
if (this == obj) {
99+
return true;
100+
}
101+
if (!(obj instanceof RequestHeaderRequestMatcher that)) {
102+
return false;
103+
}
104+
return Objects.equals(this.expectedHeaderName, that.expectedHeaderName)
105+
&& Objects.equals(this.expectedHeaderValue, that.expectedHeaderValue);
106+
}
107+
108+
@Override
109+
public int hashCode() {
110+
return Objects.hash(this.expectedHeaderName, this.expectedHeaderValue);
111+
}
112+
94113
@Override
95114
public String toString() {
96115
return "RequestHeaderRequestMatcher [expectedHeaderName=" + this.expectedHeaderName + ", expectedHeaderValue="

0 commit comments

Comments
 (0)