Skip to content

Commit f4cc27c

Browse files
committed
Change Default for (Server)AuthenticationEntryPointFailureHandler
Closes gh-9429
1 parent 5afc7cb commit f4cc27c

File tree

6 files changed

+14
-17
lines changed

6 files changed

+14
-17
lines changed

docs/modules/ROOT/pages/whats-new.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Instead, use `requestMatchers` or `HttpSecurity#securityMatchers`.
3232
* https://github.com/spring-projects/spring-security/issues/11960[gh-11960] - Default to Xor CSRF protection for xref:servlet/exploits/csrf.adoc#servlet-csrf-configure-request-handler[servlet] and xref:reactive/exploits/csrf.adoc#webflux-csrf-configure-request-handler[reactive]
3333
* https://github.com/spring-projects/spring-security/issues/12019[gh-12019] - Remove deprecated method `setTokenFromMultipartDataEnabled` from `CsrfWebFilter`
3434
* https://github.com/spring-projects/spring-security/issues/12020[gh-12020] - Remove deprecated method `tokenFromMultipartDataEnabled` from Java Configuration
35+
* https://github.com/spring-projects/spring-security/issues/9429[gh-9429] - `Authentication(Web)Filter` rethrows `AuthenticationServiceException`s
3536

3637
== Observability
3738

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/web/authentication/BearerTokenAuthenticationFilter.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.security.authentication.AuthenticationDetailsSource;
2828
import org.springframework.security.authentication.AuthenticationManager;
2929
import org.springframework.security.authentication.AuthenticationManagerResolver;
30-
import org.springframework.security.authentication.AuthenticationServiceException;
3130
import org.springframework.security.core.Authentication;
3231
import org.springframework.security.core.AuthenticationException;
3332
import org.springframework.security.core.context.SecurityContext;
@@ -40,6 +39,7 @@
4039
import org.springframework.security.oauth2.server.resource.web.BearerTokenResolver;
4140
import org.springframework.security.oauth2.server.resource.web.DefaultBearerTokenResolver;
4241
import org.springframework.security.web.AuthenticationEntryPoint;
42+
import org.springframework.security.web.authentication.AuthenticationEntryPointFailureHandler;
4343
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
4444
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
4545
import org.springframework.security.web.context.RequestAttributeSecurityContextRepository;
@@ -73,12 +73,8 @@ public class BearerTokenAuthenticationFilter extends OncePerRequestFilter {
7373

7474
private AuthenticationEntryPoint authenticationEntryPoint = new BearerTokenAuthenticationEntryPoint();
7575

76-
private AuthenticationFailureHandler authenticationFailureHandler = (request, response, exception) -> {
77-
if (exception instanceof AuthenticationServiceException) {
78-
throw exception;
79-
}
80-
this.authenticationEntryPoint.commence(request, response, exception);
81-
};
76+
private AuthenticationFailureHandler authenticationFailureHandler = new AuthenticationEntryPointFailureHandler(
77+
(request, response, exception) -> this.authenticationEntryPoint.commence(request, response, exception));
8278

8379
private BearerTokenResolver bearerTokenResolver = new DefaultBearerTokenResolver();
8480

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
*/
3636
public class AuthenticationEntryPointFailureHandler implements AuthenticationFailureHandler {
3737

38-
private boolean rethrowAuthenticationServiceException = false;
38+
private boolean rethrowAuthenticationServiceException = true;
3939

4040
private final AuthenticationEntryPoint authenticationEntryPoint;
4141

@@ -59,7 +59,7 @@ public void onAuthenticationFailure(HttpServletRequest request, HttpServletRespo
5959
}
6060

6161
/**
62-
* Set whether to rethrow {@link AuthenticationServiceException}s (defaults to false)
62+
* Set whether to rethrow {@link AuthenticationServiceException}s (defaults to true)
6363
* @param rethrowAuthenticationServiceException whether to rethrow
6464
* {@link AuthenticationServiceException}s
6565
* @since 5.8

web/src/main/java/org/springframework/security/web/server/authentication/ServerAuthenticationEntryPointFailureHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class ServerAuthenticationEntryPointFailureHandler implements ServerAuthe
3535

3636
private final ServerAuthenticationEntryPoint authenticationEntryPoint;
3737

38-
private boolean rethrowAuthenticationServiceException = false;
38+
private boolean rethrowAuthenticationServiceException = true;
3939

4040
public ServerAuthenticationEntryPointFailureHandler(ServerAuthenticationEntryPoint authenticationEntryPoint) {
4141
Assert.notNull(authenticationEntryPoint, "authenticationEntryPoint cannot be null");
@@ -54,7 +54,7 @@ public Mono<Void> onAuthenticationFailure(WebFilterExchange webFilterExchange, A
5454
}
5555

5656
/**
57-
* Set whether to rethrow {@link AuthenticationServiceException}s (defaults to false)
57+
* Set whether to rethrow {@link AuthenticationServiceException}s (defaults to true)
5858
* @param rethrowAuthenticationServiceException whether to rethrow
5959
* {@link AuthenticationServiceException}s
6060
* @since 5.8

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@
3030
public class AuthenticationEntryPointFailureHandlerTests {
3131

3232
@Test
33-
void onAuthenticationFailureWhenDefaultsThenAuthenticationServiceExceptionSwallowed() throws Exception {
33+
void onAuthenticationFailureWhenRethrowingThenAuthenticationServiceExceptionSwallowed() throws Exception {
3434
AuthenticationEntryPoint entryPoint = mock(AuthenticationEntryPoint.class);
3535
AuthenticationEntryPointFailureHandler handler = new AuthenticationEntryPointFailureHandler(entryPoint);
36+
handler.setRethrowAuthenticationServiceException(false);
3637
handler.onAuthenticationFailure(null, null, new AuthenticationServiceException("fail"));
3738
}
3839

3940
@Test
40-
void handleWhenRethrowingThenAuthenticationServiceExceptionRethrown() {
41+
void handleWhenDefaultsThenAuthenticationServiceExceptionRethrown() {
4142
AuthenticationEntryPoint entryPoint = mock(AuthenticationEntryPoint.class);
4243
AuthenticationEntryPointFailureHandler handler = new AuthenticationEntryPointFailureHandler(entryPoint);
43-
handler.setRethrowAuthenticationServiceException(true);
4444
assertThatExceptionOfType(AuthenticationServiceException.class).isThrownBy(
4545
() -> handler.onAuthenticationFailure(null, null, new AuthenticationServiceException("fail")));
4646
}

web/src/test/java/org/springframework/security/web/server/authentication/ServerAuthenticationEntryPointFailureHandlerTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,16 @@ public void onAuthenticationFailureWhenInvokedThenDelegatesToEntryPoint() {
7171
}
7272

7373
@Test
74-
void onAuthenticationFailureWhenDefaultsThenAuthenticationServiceExceptionSwallowed() {
74+
void onAuthenticationFailureWhenRethrownFalseThenAuthenticationServiceExceptionSwallowed() {
7575
AuthenticationServiceException e = new AuthenticationServiceException("fail");
76+
this.handler.setRethrowAuthenticationServiceException(false);
7677
given(this.authenticationEntryPoint.commence(this.exchange, e)).willReturn(Mono.empty());
7778
this.handler.onAuthenticationFailure(this.filterExchange, e).block();
7879
}
7980

8081
@Test
81-
void handleWhenRethrowingThenAuthenticationServiceExceptionRethrown() {
82+
void handleWhenDefaultsThenAuthenticationServiceExceptionRethrown() {
8283
AuthenticationServiceException e = new AuthenticationServiceException("fail");
83-
this.handler.setRethrowAuthenticationServiceException(true);
8484
assertThatExceptionOfType(AuthenticationServiceException.class)
8585
.isThrownBy(() -> this.handler.onAuthenticationFailure(this.filterExchange, e).block());
8686
}

0 commit comments

Comments
 (0)