Skip to content

Commit 464e506

Browse files
committed
Polish ExceptionTranslateWebFilter
- Isolated exception construction - Isolated entry point subscription Issue gh-16444
1 parent 60bed7f commit 464e506

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

web/src/main/java/org/springframework/security/web/server/authorization/ExceptionTranslationWebFilter.java

+19-13
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.
@@ -50,14 +50,19 @@ public class ExceptionTranslationWebFilter implements WebFilter {
5050
@Override
5151
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
5252
return chain.filter(exchange)
53-
.onErrorResume(AccessDeniedException.class, (denied) -> exchange.getPrincipal()
54-
.filter((principal) -> (!(principal instanceof Authentication) || (principal instanceof Authentication
55-
&& (this.authenticationTrustResolver.isAuthenticated((Authentication) principal)))))
56-
.switchIfEmpty(commenceAuthentication(exchange,
57-
new InsufficientAuthenticationException(
58-
"Full authentication is required to access this resource")))
59-
.flatMap((principal) -> this.accessDeniedHandler.handle(exchange, denied))
60-
.then());
53+
.onErrorResume(AccessDeniedException.class,
54+
(denied) -> exchange.getPrincipal()
55+
.switchIfEmpty(Mono.defer(() -> commenceAuthentication(exchange, null)))
56+
.flatMap((principal) -> {
57+
if (!(principal instanceof Authentication authentication)) {
58+
return this.accessDeniedHandler.handle(exchange, denied);
59+
}
60+
if (this.authenticationTrustResolver.isAuthenticated(authentication)) {
61+
return this.accessDeniedHandler.handle(exchange, denied);
62+
}
63+
return commenceAuthentication(exchange, authentication);
64+
})
65+
.then());
6166
}
6267

6368
/**
@@ -92,10 +97,11 @@ public void setAuthenticationTrustResolver(AuthenticationTrustResolver authentic
9297
this.authenticationTrustResolver = authenticationTrustResolver;
9398
}
9499

95-
private <T> Mono<T> commenceAuthentication(ServerWebExchange exchange, AuthenticationException denied) {
96-
return this.authenticationEntryPoint
97-
.commence(exchange, new AuthenticationCredentialsNotFoundException("Not Authenticated", denied))
98-
.then(Mono.empty());
100+
private <T> Mono<T> commenceAuthentication(ServerWebExchange exchange, Authentication authentication) {
101+
AuthenticationException cause = new InsufficientAuthenticationException(
102+
"Full authentication is required to access this resource");
103+
AuthenticationException ex = new AuthenticationCredentialsNotFoundException("Not Authenticated", cause);
104+
return this.authenticationEntryPoint.commence(exchange, ex).then(Mono.empty());
99105
}
100106

101107
}

web/src/test/java/org/springframework/security/web/server/authorization/ExceptionTranslationWebFilterTests.java

+1-2
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.
@@ -129,7 +129,6 @@ public void filterWhenDefaultsAndAccessDeniedExceptionAndNotAuthenticatedThenUna
129129
@Test
130130
public void filterWhenAccessDeniedExceptionAndAuthenticatedThenHandled() {
131131
given(this.deniedHandler.handle(any(), any())).willReturn(this.deniedPublisher.mono());
132-
given(this.entryPoint.commence(any(), any())).willReturn(this.entryPointPublisher.mono());
133132
given(this.exchange.getPrincipal()).willReturn(Mono.just(this.principal));
134133
given(this.chain.filter(this.exchange)).willReturn(Mono.error(new AccessDeniedException("Not Authorized")));
135134
StepVerifier.create(this.filter.filter(this.exchange, this.chain)).expectComplete().verify();

0 commit comments

Comments
 (0)