Skip to content

Commit

Permalink
Fix the problem of logging in successfully even if rate limiting
Browse files Browse the repository at this point in the history
Signed-off-by: John Niang <johnniang@fastmail.com>
  • Loading branch information
JohnNiang committed Jun 20, 2023
1 parent 2fd9cbd commit c5ef019
Showing 1 changed file with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ public UsernamePasswordAuthenticator(ServerResponse.Context context,
this.rateLimiterRegistry = rateLimiterRegistry;
this.messageSource = messageSource;

this.authenticationWebFilter = new AuthenticationWebFilter(authenticationManager());
this.authenticationWebFilter =
new UsernamePasswordAuthenticationWebFilter(authenticationManager());
configureAuthenticationWebFilter(this.authenticationWebFilter);
}

Expand Down Expand Up @@ -179,6 +180,23 @@ private Locale getLocale(ServerWebExchange exchange) {
return locale == null ? Locale.getDefault() : locale;
}

private class UsernamePasswordAuthenticationWebFilter extends AuthenticationWebFilter {

public UsernamePasswordAuthenticationWebFilter(
ReactiveAuthenticationManager authenticationManager) {
super(authenticationManager);
}

@Override
protected Mono<Void> onAuthenticationSuccess(Authentication authentication,
WebFilterExchange webFilterExchange) {
return super.onAuthenticationSuccess(authentication, webFilterExchange)
.transformDeferred(createIPBasedRateLimiter(webFilterExchange.getExchange()))
.onErrorResume(RequestNotPermitted.class,
e -> handleRequestNotPermitted(e, webFilterExchange.getExchange()));
}
}

public class LoginSuccessHandler implements ServerAuthenticationSuccessHandler {

private final ServerAuthenticationSuccessHandler defaultHandler =
Expand Down Expand Up @@ -206,10 +224,7 @@ public Mono<Void> onAuthenticationSuccess(WebFilterExchange webFilterExchange,
.bodyValue(principal)
.flatMap(serverResponse ->
serverResponse.writeTo(exchange, context));
})
.transformDeferred(createIPBasedRateLimiter(exchange))
.onErrorResume(RequestNotPermitted.class,
e -> handleRequestNotPermitted(e, exchange));
});
}
}

Expand Down

0 comments on commit c5ef019

Please sign in to comment.