mirror of
https://gitee.com/log4j/pig.git
synced 2024-12-31 08:14:18 +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 lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
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.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
@ -82,14 +83,19 @@ public class ValidateCodeGatewayFilter extends AbstractGatewayFilterFactory {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
ServerHttpResponse response = exchange.getResponse();
|
ServerHttpResponse response = exchange.getResponse();
|
||||||
response.setStatusCode(HttpStatus.PRECONDITION_REQUIRED);
|
response.setStatusCode(HttpStatus.PRECONDITION_REQUIRED);
|
||||||
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
||||||
try {
|
|
||||||
return response.writeWith(Mono.just(response.bufferFactory()
|
return response.writeWith(Mono.create(monoSink -> {
|
||||||
.wrap(objectMapper.writeValueAsBytes(
|
try {
|
||||||
R.failed(e.getMessage())))));
|
byte[] bytes = objectMapper.writeValueAsBytes(R.failed(e.getMessage()));
|
||||||
} catch (JsonProcessingException e1) {
|
DataBuffer dataBuffer = response.bufferFactory().wrap(bytes);
|
||||||
log.error("对象输出异常", e1);
|
|
||||||
}
|
monoSink.success(dataBuffer);
|
||||||
|
} catch (JsonProcessingException jsonProcessingException) {
|
||||||
|
log.error("对象输出异常", jsonProcessingException);
|
||||||
|
monoSink.error(jsonProcessingException);
|
||||||
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
return chain.filter(exchange);
|
return chain.filter(exchange);
|
||||||
|
@ -22,7 +22,8 @@ import com.google.code.kaptcha.Producer;
|
|||||||
import com.pig4cloud.pig.common.core.constant.CommonConstants;
|
import com.pig4cloud.pig.common.core.constant.CommonConstants;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
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.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
@ -53,6 +54,23 @@ public class ImageCodeHandler implements HandlerFunction<ServerResponse> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mono<ServerResponse> handle(ServerRequest serverRequest) {
|
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();
|
String text = producer.createText();
|
||||||
BufferedImage image = producer.createImage(text);
|
BufferedImage image = producer.createImage(text);
|
||||||
@ -63,16 +81,7 @@ public class ImageCodeHandler implements HandlerFunction<ServerResponse> {
|
|||||||
|
|
||||||
// 转换流信息写出
|
// 转换流信息写出
|
||||||
FastByteArrayOutputStream os = new FastByteArrayOutputStream();
|
FastByteArrayOutputStream os = new FastByteArrayOutputStream();
|
||||||
try {
|
ImageIO.write(image, "jpeg", os);
|
||||||
ImageIO.write(image, "jpeg", os);
|
return os.toByteArray();
|
||||||
} 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())));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user