Skip to content

Commit 3bcb1d9

Browse files
ankurpathakrwinch
authored andcommitted
Allow setting authenticationEntryPoint for Http Basic
1. Added method authenticationEntryPoint in ServerHttpSecurity to allow setting authenticationEntryPoint. 2. Added test in ServerHttpSecurityTests to check if if specified realm name set by authenticationEntryPoint is returned Fixes: gh-6270
1 parent a90c217 commit 3bcb1d9

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

config/src/main/java/org/springframework/security/config/web/server/ServerHttpSecurity.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,6 +1878,19 @@ public HttpBasicSpec securityContextRepository(ServerSecurityContextRepository s
18781878
return this;
18791879
}
18801880

1881+
/**
1882+
* Allows easily setting the entry point.
1883+
* @param authenticationEntryPoint the {@link ServerAuthenticationEntryPoint} to use
1884+
* @return {@link HttpBasicSpec} for additional customization
1885+
* @since 5.2.0
1886+
* @author Ankur Pathak
1887+
*/
1888+
public HttpBasicSpec authenticationEntryPoint(ServerAuthenticationEntryPoint authenticationEntryPoint){
1889+
Assert.notNull(authenticationEntryPoint, "authenticationEntryPoint cannot be null");
1890+
this.entryPoint = authenticationEntryPoint;
1891+
return this;
1892+
}
1893+
18811894
/**
18821895
* Allows method chaining to continue configuring the {@link ServerHttpSecurity}
18831896
* @return the {@link ServerHttpSecurity} to continue configuring

config/src/test/java/org/springframework/security/config/web/server/ServerHttpSecurityTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import org.springframework.security.web.server.context.SecurityContextServerWebExchangeWebFilter;
6565
import org.springframework.web.server.WebFilterChain;
6666
import org.springframework.security.web.server.authentication.AnonymousAuthenticationWebFilterTests;
67+
import org.springframework.security.web.server.authentication.HttpBasicServerAuthenticationEntryPoint;
6768

6869
/**
6970
* @author Rob Winch
@@ -255,6 +256,29 @@ public void basicWithAnonymous() {
255256
assertThat(result.getResponseCookies().getFirst("SESSION")).isNull();
256257
}
257258

259+
@Test
260+
public void basicWithCustomRealmName() {
261+
this.http.securityContextRepository(new WebSessionServerSecurityContextRepository());
262+
HttpBasicServerAuthenticationEntryPoint authenticationEntryPoint = new HttpBasicServerAuthenticationEntryPoint();
263+
authenticationEntryPoint.setRealm("myrealm");
264+
this.http.httpBasic().authenticationEntryPoint(authenticationEntryPoint);
265+
this.http.authenticationManager(this.authenticationManager);
266+
ServerHttpSecurity.AuthorizeExchangeSpec authorize = this.http.authorizeExchange();
267+
authorize.anyExchange().authenticated();
268+
269+
WebTestClient client = buildClient();
270+
271+
EntityExchangeResult<String> result = client.get()
272+
.uri("/")
273+
.exchange()
274+
.expectStatus().isUnauthorized()
275+
.expectHeader().value(HttpHeaders.WWW_AUTHENTICATE, value -> assertThat(value).contains("myrealm"))
276+
.expectBody(String.class)
277+
.returnResult();
278+
279+
assertThat(result.getResponseCookies().getFirst("SESSION")).isNull();
280+
}
281+
258282
private <T extends WebFilter> Optional<T> getWebFilter(SecurityWebFilterChain filterChain, Class<T> filterClass) {
259283
return (Optional<T>) filterChain.getWebFilters()
260284
.filter(Objects::nonNull)

0 commit comments

Comments
 (0)