From fd0841a0121695fe21230757d58d381e6571dbbb Mon Sep 17 00:00:00 2001 From: ChiveHao Date: Fri, 12 Jul 2024 07:32:29 +0800 Subject: [PATCH] optimize: security app token response when user not found or disable. (#620) --- .../java/run/ikaros/server/security/SecurityEndpoint.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/run/ikaros/server/security/SecurityEndpoint.java b/server/src/main/java/run/ikaros/server/security/SecurityEndpoint.java index 5e34fb745..2235cebfc 100644 --- a/server/src/main/java/run/ikaros/server/security/SecurityEndpoint.java +++ b/server/src/main/java/run/ikaros/server/security/SecurityEndpoint.java @@ -16,6 +16,8 @@ import org.springframework.web.reactive.function.server.ServerResponse; import reactor.core.publisher.Mono; import run.ikaros.api.constant.OpenApiConst; +import run.ikaros.api.infra.exception.security.UserAuthenticationException; +import run.ikaros.api.infra.exception.user.UserNotFoundException; import run.ikaros.server.endpoint.CoreEndpoint; import run.ikaros.server.security.authentication.jwt.JwtApplyParam; import run.ikaros.server.security.authentication.jwt.JwtAuthenticationProvider; @@ -71,6 +73,8 @@ private Mono applyJwtToken(ServerRequest request) { .flatMap(userDetailsService::findByUsername) .map(UserDetails::getUsername) .map(jwtAuthenticationProvider::generateToken) - .flatMap(token -> ServerResponse.ok().bodyValue(token)); + .flatMap(token -> ServerResponse.ok().bodyValue(token)) + .onErrorResume(UserNotFoundException.class, + e -> Mono.error(new UserAuthenticationException(e.getLocalizedMessage(), e))); } }