Skip to content

Commit f6d7736

Browse files
committed
override OncePerRequestFilter#getAlreadyFilteredAttributeName in AuthenticationFilter (#17173)
Signed-off-by: Andrey Litvitski <[email protected]>
1 parent 6d3b54d commit f6d7736

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -64,6 +64,7 @@
6464
* </ul>
6565
*
6666
* @author Sergey Bespalov
67+
* @author Andrey Litvitski
6768
* @since 5.2.0
6869
*/
6970
public class AuthenticationFilter extends OncePerRequestFilter {
@@ -222,4 +223,9 @@ private Authentication attemptAuthentication(HttpServletRequest request, HttpSer
222223
return authenticationResult;
223224
}
224225

226+
@Override
227+
protected String getAlreadyFilteredAttributeName() {
228+
return getClass().getName() + "-" + System.identityHashCode(this) + ".FILTERED";
229+
}
230+
225231
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.security.web.authentication;
1818

1919
import jakarta.servlet.FilterChain;
20+
import jakarta.servlet.Servlet;
2021
import jakarta.servlet.ServletException;
2122
import jakarta.servlet.ServletRequest;
2223
import jakarta.servlet.ServletResponse;
@@ -57,6 +58,7 @@
5758

5859
/**
5960
* @author Sergey Bespalov
61+
* @author Andrey Litvitski
6062
* @since 5.2.0
6163
*/
6264
@ExtendWith(MockitoExtension.class)
@@ -318,4 +320,18 @@ public void filterWhenCustomSecurityContextRepositoryAndSuccessfulAuthentication
318320
assertThat(securityContextArg.getValue().getAuthentication()).isEqualTo(authentication);
319321
}
320322

323+
@Test
324+
public void filterWhenInFilterChainThenBothAreExecuted() throws Exception {
325+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
326+
MockHttpServletResponse response = new MockHttpServletResponse();
327+
AuthenticationFilter filter1 = new AuthenticationFilter(this.authenticationManager,
328+
this.authenticationConverter);
329+
AuthenticationConverter converter2 = mock(AuthenticationConverter.class);
330+
AuthenticationFilter filter2 = new AuthenticationFilter(this.authenticationManager, converter2);
331+
FilterChain chain = new MockFilterChain(mock(Servlet.class), filter1, filter2);
332+
chain.doFilter(request, response);
333+
verify(this.authenticationConverter).convert(any());
334+
verify(converter2).convert(any());
335+
}
336+
321337
}

0 commit comments

Comments
 (0)