Skip to content

Commit e3d4d66

Browse files
committed
BasicAuthenticationFilter case insenstive
Fixes: gh-5586
1 parent 2cd2bab commit e3d4d66

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ protected void doFilterInternal(HttpServletRequest request,
154154

155155
String header = request.getHeader("Authorization");
156156

157-
if (header == null || !header.startsWith("Basic ")) {
157+
if (header == null || !header.toLowerCase().startsWith("basic ")) {
158158
chain.doFilter(request, response);
159159
return;
160160
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,26 @@ public void testNormalOperation() throws Exception {
156156
.isEqualTo("rod");
157157
}
158158

159+
// gh-5586
160+
@Test
161+
public void doFilterWhenSchemeLowercaseThenCaseInsensitveMatchWorks() throws Exception {
162+
String token = "rod:koala";
163+
MockHttpServletRequest request = new MockHttpServletRequest();
164+
request.addHeader("Authorization",
165+
"basic " + new String(Base64.encodeBase64(token.getBytes())));
166+
request.setServletPath("/some_file.html");
167+
168+
// Test
169+
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
170+
FilterChain chain = mock(FilterChain.class);
171+
filter.doFilter(request, new MockHttpServletResponse(), chain);
172+
173+
verify(chain).doFilter(any(ServletRequest.class), any(ServletResponse.class));
174+
assertThat(SecurityContextHolder.getContext().getAuthentication()).isNotNull();
175+
assertThat(SecurityContextHolder.getContext().getAuthentication().getName())
176+
.isEqualTo("rod");
177+
}
178+
159179
@Test
160180
public void testOtherAuthorizationSchemeIsIgnored() throws Exception {
161181

0 commit comments

Comments
 (0)