diff --git a/flyfish-web/src/main/java/com/flyfish/framework/handler/JsonAuthenticationFailureHandler.java b/flyfish-web/src/main/java/com/flyfish/framework/handler/JsonAuthenticationFailureHandler.java index e4f716c..9538181 100644 --- a/flyfish-web/src/main/java/com/flyfish/framework/handler/JsonAuthenticationFailureHandler.java +++ b/flyfish-web/src/main/java/com/flyfish/framework/handler/JsonAuthenticationFailureHandler.java @@ -1,7 +1,6 @@ package com.flyfish.framework.handler; import com.flyfish.framework.bean.Result; -import com.flyfish.framework.domain.po.User; import com.flyfish.framework.service.MongoUserDetailsService; import com.flyfish.framework.transform.DataBufferTransformer; import org.springframework.http.MediaType; @@ -44,19 +43,30 @@ public class JsonAuthenticationFailureHandler implements ServerAuthenticationFai */ @Override public Mono onAuthenticationFailure(WebFilterExchange webFilterExchange, AuthenticationException exception) { + ServerHttpResponse response = webFilterExchange.getExchange().getResponse(); + response.getHeaders().setContentType(MediaType.APPLICATION_JSON); return Mono.justOrEmpty(Optional.ofNullable(webFilterExchange.getExchange().getApplicationContext())) .flatMap(applicationContext -> { MongoUserDetailsService userDetailsService = applicationContext.getBean(MongoUserDetailsService.class); - Mono mono = exception instanceof BadCredentialsException ? userDetailsService.error(webFilterExchange.getExchange()) - : Mono.justOrEmpty(Optional.empty()); - return mono.flatMap(user -> { - ServerHttpResponse response = webFilterExchange.getExchange().getResponse(); - response.getHeaders().setContentType(MediaType.APPLICATION_JSON); - return response.writeWith(Mono.fromCallable(() -> - dataBufferTransformer.transform( - Result.error(descriptionMap.getOrDefault(exception.getClass(), exception.getMessage())) - ))); - }); + if (exception instanceof BadCredentialsException) { + return userDetailsService.error(webFilterExchange.getExchange()) + .flatMap(user -> write(response, exception)); + } + return write(response, exception); }); } + + /** + * 写入 + * + * @param response 响应 + * @param exception 异常 + * @return 结果 + */ + private Mono write(ServerHttpResponse response, AuthenticationException exception) { + return response.writeWith(Mono.fromCallable(() -> + dataBufferTransformer.transform( + Result.error(descriptionMap.getOrDefault(exception.getClass(), exception.getMessage())) + ))); + } }