Skip to content

Commit d23b231

Browse files
Merge branch '6.1.x'
Closes gh-13760
2 parents 8eed8cd + b64d539 commit d23b231

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

web/src/main/java/org/springframework/security/web/authentication/www/BasicAuthenticationEntryPoint.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void afterPropertiesSet() {
5252
@Override
5353
public void commence(HttpServletRequest request, HttpServletResponse response,
5454
AuthenticationException authException) throws IOException {
55-
response.addHeader("WWW-Authenticate", "Basic realm=\"" + this.realmName + "\"");
55+
response.setHeader("WWW-Authenticate", "Basic realm=\"" + this.realmName + "\"");
5656
response.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase());
5757
}
5858

web/src/test/java/org/springframework/security/web/authentication/www/BasicAuthenticationEntryPointTests.java

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

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

19+
import java.io.IOException;
20+
import java.util.List;
21+
1922
import org.junit.jupiter.api.Test;
2023

24+
import org.springframework.http.HttpHeaders;
2125
import org.springframework.http.HttpStatus;
2226
import org.springframework.mock.web.MockHttpServletRequest;
2327
import org.springframework.mock.web.MockHttpServletResponse;
@@ -61,4 +65,19 @@ public void testNormalOperation() throws Exception {
6165
assertThat(response.getHeader("WWW-Authenticate")).isEqualTo("Basic realm=\"hello\"");
6266
}
6367

68+
// gh-13737
69+
@Test
70+
void commenceWhenResponseHasHeaderThenOverride() throws IOException {
71+
BasicAuthenticationEntryPoint ep = new BasicAuthenticationEntryPoint();
72+
ep.setRealmName("hello");
73+
MockHttpServletRequest request = new MockHttpServletRequest();
74+
request.setRequestURI("/some_path");
75+
MockHttpServletResponse response = new MockHttpServletResponse();
76+
response.setHeader(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\"test\"");
77+
ep.commence(request, response, new DisabledException("Disabled"));
78+
List<String> headers = response.getHeaders("WWW-Authenticate");
79+
assertThat(headers).hasSize(1);
80+
assertThat(headers.get(0)).isEqualTo("Basic realm=\"hello\"");
81+
}
82+
6483
}

0 commit comments

Comments
 (0)