Skip to content

Commit 029e31e

Browse files
committed
DelegatingAuthenticationEntryPoint.Builder allows just defaultEntryPoint
Previously build threw an Exception when entryPoints was empty and defaultEntryPoint was specified. This commit changes build to return the defaultEntryPoint instead. Closes gh-17955
1 parent ad6fe4f commit 029e31e

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

web/src/main/java/org/springframework/security/web/authentication/DelegatingAuthenticationEntryPoint.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,18 @@ public Builder addEntryPointFor(AuthenticationEntryPoint entryPoint, RequestMatc
211211
* @return the {@link AuthenticationEntryPoint} to use.
212212
*/
213213
public AuthenticationEntryPoint build() {
214-
Assert.notEmpty(this.entryPoints, "entryPoints cannot be empty");
215214
AuthenticationEntryPoint defaultEntryPoint = this.defaultEntryPoint;
216215
if (defaultEntryPoint == null) {
216+
Assert.state(!this.entryPoints.isEmpty(), "entryPoints cannot be empty if defaultEntryPoint is null");
217217
AuthenticationEntryPoint firstAuthenticationEntryPoint = this.entryPoints.get(0).getEntry();
218218
if (this.entryPoints.size() == 1) {
219219
return firstAuthenticationEntryPoint;
220220
}
221221
defaultEntryPoint = firstAuthenticationEntryPoint;
222222
}
223+
else if (this.entryPoints.isEmpty()) {
224+
return defaultEntryPoint;
225+
}
223226
return new DelegatingAuthenticationEntryPoint(defaultEntryPoint, this.entryPoints);
224227
}
225228

web/src/test/java/org/springframework/security/web/authentication/DelegatingAuthenticationEntryPointTests.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
package org.springframework.security.web.authentication;
1818

19+
import java.io.IOException;
1920
import java.util.Collections;
2021
import java.util.LinkedHashMap;
2122
import java.util.List;
2223

24+
import jakarta.servlet.ServletException;
2325
import jakarta.servlet.http.HttpServletRequest;
2426
import org.junit.jupiter.api.BeforeEach;
2527
import org.junit.jupiter.api.Test;
@@ -29,11 +31,15 @@
2931
import org.springframework.security.web.util.matcher.RequestMatcher;
3032
import org.springframework.security.web.util.matcher.RequestMatcherEntry;
3133

34+
import static org.assertj.core.api.Assertions.assertThat;
3235
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
36+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
37+
import static org.mockito.ArgumentMatchers.any;
3338
import static org.mockito.BDDMockito.given;
3439
import static org.mockito.Mockito.mock;
3540
import static org.mockito.Mockito.never;
3641
import static org.mockito.Mockito.verify;
42+
import static org.mockito.Mockito.verifyNoInteractions;
3743

3844
/**
3945
* Test class for {@link DelegatingAuthenticationEntryPoint}
@@ -202,4 +208,49 @@ public void commenceWhenSecondMatchesThenDefaultNotInvoked() throws Exception {
202208
verify(this.defaultEntryPoint, never()).commence(this.request, null, null);
203209
}
204210

211+
@Test
212+
void builderWhenDefaultNullAndSingleEntryPointThenReturnsSingle() {
213+
AuthenticationEntryPoint entryPoint = mock(AuthenticationEntryPoint.class);
214+
215+
AuthenticationEntryPoint result = DelegatingAuthenticationEntryPoint.builder()
216+
.addEntryPointFor(entryPoint, mock(RequestMatcher.class))
217+
.build();
218+
219+
assertThat(result).isEqualTo(entryPoint);
220+
}
221+
222+
@Test
223+
void builderWhenDefaultNullThenFirstIsDefault() throws ServletException, IOException {
224+
AuthenticationEntryPoint firstEntryPoint = mock(AuthenticationEntryPoint.class);
225+
AuthenticationEntryPoint secondEntryPoint = mock(AuthenticationEntryPoint.class);
226+
RequestMatcher neverMatch = mock(RequestMatcher.class);
227+
given(neverMatch.matches(this.request)).willReturn(false);
228+
AuthenticationEntryPoint result = DelegatingAuthenticationEntryPoint.builder()
229+
.addEntryPointFor(firstEntryPoint, neverMatch)
230+
.addEntryPointFor(secondEntryPoint, neverMatch)
231+
.build();
232+
233+
result.commence(this.request, null, null);
234+
235+
verify(firstEntryPoint).commence(any(), any(), any());
236+
verifyNoInteractions(secondEntryPoint);
237+
}
238+
239+
@Test
240+
void builderWhenDefaultAndEmptyEntryPointsThenReturnsDefault() {
241+
AuthenticationEntryPoint defaultEntryPoint = mock(AuthenticationEntryPoint.class);
242+
243+
AuthenticationEntryPoint result = DelegatingAuthenticationEntryPoint.builder()
244+
.defaultEntryPoint(defaultEntryPoint)
245+
.build();
246+
247+
assertThat(result).isEqualTo(defaultEntryPoint);
248+
}
249+
250+
@Test
251+
void builderWhenNoEntryPointsThenIllegalStateException() {
252+
DelegatingAuthenticationEntryPoint.Builder builder = DelegatingAuthenticationEntryPoint.builder();
253+
assertThatIllegalStateException().isThrownBy(builder::build);
254+
}
255+
205256
}

0 commit comments

Comments
 (0)