作者 朱兆平

优化基于win或者linux的乱码返回方法

... ... @@ -73,4 +73,7 @@ management:
endpoint:
health:
show-details: always
debug: false
\ No newline at end of file
debug: false
host:
# 部署所在服务器操作系统名称,小写[windows/linux]
system-info: linux
... ...
... ... @@ -10,6 +10,7 @@ import com.example.gateway.util.JsonToBean;
import lombok.extern.slf4j.Slf4j;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
... ... @@ -43,6 +44,12 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Value("${host.system-info}")
private String systemInfo;
//默认部署操作系统名称
private static final String DEFAULT_SYSTEM="linux";
private static AntPathMatcher pathMatcher = new AntPathMatcher();
@Override
... ... @@ -87,8 +94,15 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered {
// }
// };
ServerHttpResponseDecorator decoratedResponseGBK= new GBKServerHttpResponseDecorator(response);
return chain.filter(exchange.mutate().response(decoratedResponseGBK).build());
ServerHttpResponseDecorator decoratedResponse= new UTF8ServerHttpResponseDecorator(response);
if (DEFAULT_SYSTEM.equals(systemInfo)){
decoratedResponse= new UTF8ServerHttpResponseDecorator(response);
}else {
decoratedResponse= new GBKServerHttpResponseDecorator(response);
}
//
return chain.filter(exchange.mutate().response(decoratedResponse).build());
//
// return chain.filter(exchange);
}
... ... @@ -131,6 +145,11 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered {
return false;
}
/**
* 白名单接口访问判定
* @param request
* @return
*/
public boolean anonymousUrlFilter(ServerHttpRequest request){
//内部服务接口,不允许外部访问
if(pathMatcher.match("/**/anonymous/**", request.getPath().toString())) {
... ...
... ... @@ -6,6 +6,7 @@ import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import reactor.core.publisher.Flux;
... ... @@ -24,7 +25,7 @@ public class UTF8ServerHttpResponseDecorator extends ServerHttpResponseDecorator
@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
if (body instanceof Flux) {
if (HttpStatus.OK.equals(getStatusCode()) && body instanceof Flux) {
Flux<? extends DataBuffer> fluxBody = (Flux<? extends DataBuffer>) body;
return super.writeWith(fluxBody.buffer().map(dataBuffer -> {
DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory();
... ...
... ... @@ -20,8 +20,10 @@ public class JsonToBean {
USERS users = JSONObject.toJavaObject(jsonObject,USERS.class);
List<PERMISSION> list = users.getPermissions();
List<PERMISSION> permissionList = new ArrayList<>();
for (PERMISSION permission: list){
permissionList.add(permission);
if (list!=null && !list.isEmpty()){
for (PERMISSION permission: list){
permissionList.add(permission);
}
}
return permissionList;
}
... ...