| 
1 | 1 | /*  | 
2 |  | - * Copyright 2002-2022 the original author or authors.  | 
 | 2 | + * Copyright 2002-2025 the original author or authors.  | 
3 | 3 |  *  | 
4 | 4 |  * Licensed under the Apache License, Version 2.0 (the "License");  | 
5 | 5 |  * you may not use this file except in compliance with the License.  | 
@@ -50,14 +50,19 @@ public class ExceptionTranslationWebFilter implements WebFilter {  | 
50 | 50 | 	@Override  | 
51 | 51 | 	public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {  | 
52 | 52 | 		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());  | 
61 | 66 | 	}  | 
62 | 67 | 
 
  | 
63 | 68 | 	/**  | 
@@ -92,10 +97,11 @@ public void setAuthenticationTrustResolver(AuthenticationTrustResolver authentic  | 
92 | 97 | 		this.authenticationTrustResolver = authenticationTrustResolver;  | 
93 | 98 | 	}  | 
94 | 99 | 
 
  | 
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());  | 
99 | 105 | 	}  | 
100 | 106 | 
 
  | 
101 | 107 | }  | 
0 commit comments