diff --git a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/filter/ValidateCodeGatewayFilter.java b/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/filter/ValidateCodeGatewayFilter.java index b34e106c..a24e1781 100644 --- a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/filter/ValidateCodeGatewayFilter.java +++ b/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/filter/ValidateCodeGatewayFilter.java @@ -32,6 +32,7 @@ import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; +import org.springframework.core.io.buffer.DataBuffer; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -82,14 +83,19 @@ public class ValidateCodeGatewayFilter extends AbstractGatewayFilterFactory { } catch (Exception e) { ServerHttpResponse response = exchange.getResponse(); response.setStatusCode(HttpStatus.PRECONDITION_REQUIRED); - response.getHeaders().setContentType(MediaType.APPLICATION_JSON); - try { - return response.writeWith(Mono.just(response.bufferFactory() - .wrap(objectMapper.writeValueAsBytes( - R.failed(e.getMessage()))))); - } catch (JsonProcessingException e1) { - log.error("对象输出异常", e1); - } + response.getHeaders().setContentType(MediaType.APPLICATION_JSON); + + return response.writeWith(Mono.create(monoSink -> { + try { + byte[] bytes = objectMapper.writeValueAsBytes(R.failed(e.getMessage())); + DataBuffer dataBuffer = response.bufferFactory().wrap(bytes); + + monoSink.success(dataBuffer); + } catch (JsonProcessingException jsonProcessingException) { + log.error("对象输出异常", jsonProcessingException); + monoSink.error(jsonProcessingException); + } + })); } return chain.filter(exchange); diff --git a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/handler/ImageCodeHandler.java b/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/handler/ImageCodeHandler.java index 944e5af2..e0e048c8 100755 --- a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/handler/ImageCodeHandler.java +++ b/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/handler/ImageCodeHandler.java @@ -22,7 +22,8 @@ import com.google.code.kaptcha.Producer; import com.pig4cloud.pig.common.core.constant.CommonConstants; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.core.io.ByteArrayResource; +import org.springframework.core.io.buffer.DefaultDataBuffer; +import org.springframework.core.io.buffer.DefaultDataBufferFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -53,6 +54,23 @@ public class ImageCodeHandler implements HandlerFunction { @Override public Mono handle(ServerRequest serverRequest) { + return ServerResponse + .status(HttpStatus.OK) + .contentType(MediaType.IMAGE_JPEG) + .body(BodyInserters.fromDataBuffers(Mono.create(monoSink -> { + try { + byte[] bytes = createCodeImage(serverRequest); + DefaultDataBuffer dataBuffer = new DefaultDataBufferFactory().wrap(bytes); + + monoSink.success(dataBuffer); + } catch (IOException e) { + log.error("ImageIO write err", e); + monoSink.error(e); + } + }))); + } + + private byte[] createCodeImage(ServerRequest serverRequest) throws IOException { //生成验证码 String text = producer.createText(); BufferedImage image = producer.createImage(text); @@ -63,16 +81,7 @@ public class ImageCodeHandler implements HandlerFunction { // 转换流信息写出 FastByteArrayOutputStream os = new FastByteArrayOutputStream(); - try { - ImageIO.write(image, "jpeg", os); - } catch (IOException e) { - log.error("ImageIO write err", e); - return Mono.error(e); - } - - return ServerResponse - .status(HttpStatus.OK) - .contentType(MediaType.IMAGE_JPEG) - .body(BodyInserters.fromResource(new ByteArrayResource(os.toByteArray()))); + ImageIO.write(image, "jpeg", os); + return os.toByteArray(); } }