jeecgboot / JeecgBoot

🔥「企业级低代码平台」前后端分离架构SpringBoot 2.x/3.x,SpringCloud,Ant Design&Vue3,Mybatis,Shiro,JWT。强大的代码生成器让前后端代码一键生成,无需写任何代码! 引领新的开发模式OnlineCoding->代码生成->手工MERGE,帮助Java项目解决70%重复工作,让开发更关注业务,既能快速提高效率,帮助公司节省成本,同时又不失灵活性。
http://www.jeecg.com
Apache License 2.0
40.41k stars 14.77k forks source link

jeecg-boot clound 3.5.0 版本 网关全局过滤器 response body 读取乱码问题 #4680

Closed fzw2408 closed 1 year ago

fzw2408 commented 1 year ago
版本号:

jeecg-boot clound 3.5.0

前端版本:vue3版?还是 vue2版?
问题描述:

jeecg-boot clound 3.5.0 版本 网关全局过滤器 response body 读取乱码问题 【list.add(new String(content, "utf-8")); 尝试过gbk Unicode 等均乱码】,尝试试过自己写的服务读取body数据正常。而且仅开了JeecgSystemCloudApplicants 。测试的接口是获取验证码的接口。 http://localhost:9999/sys/randomImage/1629428467008?_t=1678679615058http://localhost:7001/sys/randomImage/1629428467008?_t=1678679615058; 网关没有加过滤器,访问该接口正常。 加了过滤器,只要尝试修改body内容,接口还回就异常。

截图&代码:

/*

import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Joiner; import com.google.common.base.Throwables; import com.google.common.collect.Lists; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.reactivestreams.Publisher; import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.http.HttpStatus; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.http.server.reactive.ServerHttpResponseDecorator; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono;

import java.nio.charset.Charset; import java.util.List;

/**

image

友情提示(为了提高issue处理效率):

miniFrank commented 1 year ago

您好,可以参考如下写法试下,主要思路就是正确合并databuffer Flux<? extends DataBuffer> fluxBody = (Flux<? extends DataBuffer>) body; Flux dataBufferFlux = fluxBody.buffer().map(dataBuffers -> { DataBuffer join = new DefaultDataBufferFactory().join(dataBuffers); int length = join.readableByteCount(); byte[] content = new byte[length]; join.read(content); DataBufferUtils.release(join); String responseBody = new String(content, StandardCharsets.UTF_8);//MODIFY RESPONSE and Return the Modified response log.info("requestId: {}, method: {}, url: {}, \nresponse body :{}", request.getId(), request.getMethodValue(), request.getURI(), responseBody); return dataBufferFactory.wrap(responseBody.getBytes(StandardCharsets.UTF_8)); }) .switchIfEmpty(Flux.defer(() -> { System.out.println("Write to database here"); return Flux.just(); }));