mirror of
https://gitee.com/log4j/pig.git
synced 2024-12-22 20:54:25 +08:00
替换"inappropriate blocking method call"代码
This commit is contained in:
parent
9d62eafb2f
commit
1e6f879523
@ -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);
|
||||
|
@ -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<ServerResponse> {
|
||||
|
||||
@Override
|
||||
public Mono<ServerResponse> 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<ServerResponse> {
|
||||
|
||||
// 转换流信息写出
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user