diff --git a/pom.xml b/pom.xml index be2e1f0..b9c2b23 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ </parent> <groupId>com.example</groupId> <artifactId>gateway</artifactId> - <version>0.0.1-SNAPSHOT</version> + <version>1.0-WIN</version> <name>gateway</name> <description>gateway project for Spring Boot</description> diff --git a/src/main/java/com/example/gateway/filter/WrapperResponseGlobalFilter.java b/src/main/java/com/example/gateway/filter/WrapperResponseGlobalFilter.java index 5736e77..d4ab180 100644 --- a/src/main/java/com/example/gateway/filter/WrapperResponseGlobalFilter.java +++ b/src/main/java/com/example/gateway/filter/WrapperResponseGlobalFilter.java @@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.example.gateway.model.PERMISSION; import com.example.gateway.model.ROLE; +import com.example.gateway.routerImpl.GBKServerHttpResponseDecorator; +import com.example.gateway.routerImpl.UTF8ServerHttpResponseDecorator; import com.example.gateway.util.JsonToBean; import lombok.extern.slf4j.Slf4j; import org.reactivestreams.Publisher; @@ -85,44 +87,8 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { // } // }; - - - /** - * 测试返回中文乱码 - */ - ServerHttpResponseDecorator decoratedResponseWindows = new ServerHttpResponseDecorator(response) { - @Override - public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) { - if (HttpStatus.OK.equals(getStatusCode()) && body instanceof Flux) { - Flux<? extends DataBuffer> fluxBody = Flux.from(body); - return super.writeWith(fluxBody.buffer().map(dataBuffers -> { - - StringBuffer stringBuffer = new StringBuffer(); - dataBuffers.forEach(dataBuffer -> { - byte[] content = new byte[dataBuffer.readableByteCount()]; - dataBuffer.read(content); - DataBufferUtils.release(dataBuffer); - String tempStr = new String(content, Charset.forName("ISO_8859_1")); - stringBuffer.append(tempStr); - }); - - String str = new String(stringBuffer); -// String fileKey = "\":\"" + Const.DATA_BASE_FILE_URL_HEAD + Const.M00; -// if (str.contains(fileKey)) { -// //修改 -// String value = "\":\"" + Const.SYSTEMS_FILE_URL_HEAD + Const.DATA_BASE_FILE_URL_HEAD + Const.M00; -// str = str.replaceAll(fileKey, value); -// } - - byte[] content = str.getBytes(Charset.forName("ISO_8859_1")); - return bufferFactory().wrap(content); - })); - } - // if body is not a flux. never got there. - return super.writeWith(body); - } - }; - return chain.filter(exchange.mutate().response(decoratedResponseWindows).build()); + ServerHttpResponseDecorator decoratedResponseGBK= new GBKServerHttpResponseDecorator(response); + return chain.filter(exchange.mutate().response(decoratedResponseGBK).build()); // // return chain.filter(exchange); } @@ -137,8 +103,6 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { if (anonymousUrlFilter(request)){ return true; } - - boolean flag = false; String token = ""; if (!request.getPath().toString().contains(LOGIN)) { HttpHeaders rqHeader = request.getHeaders(); @@ -157,15 +121,14 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { log.trace("访问url:[{}]<->权限[{}]",request.getPath().toString(),permission.getUrl()); if (pathMatcher.match(permission.getUrl(), request.getPath().toString())) { log.info("[FILTER]-[URL:{}]->鉴权成功",request.getPath().toString()); - flag = true; - break; + return true; } } }else { log.info("[FILTER]-没有对应token的redis缓存,鉴权失败"); } log.info("[FILTER]-[URL:{}]->鉴权失败",request.getPath().toString()); - return flag; + return false; } public boolean anonymousUrlFilter(ServerHttpRequest request){ @@ -185,6 +148,7 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { if(permissionList!=null && !permissionList.isEmpty()){ for (PERMISSION permission : permissionList) { if (pathMatcher.match(permission.getUrl(), request.getPath().toString())) { + log.info("[ANONYMOUS-FILTER-SUCCESS]-匿名者过滤器适配到规则-PATH:[{}]",request.getPath().toString()); return true; } } @@ -192,13 +156,13 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { } } }catch (Exception e){ - log.error("[ANONYMOUS-FILTER]匿名者过滤规则审核出错->{}",e.toString()); + log.error("[ANONYMOUS-FILTER-ERR]匿名者过滤规则审核出错->{}",e.toString()); return false; } } - log.info("[ANONYMOUS-FILTER]-匿名者过滤器为适配到符合条件的规则-PATH:[{}]",request.getPath().toString()); + log.info("[ANONYMOUS-FILTER-FALSE]-匿名者过滤器未适配到规则-PATH:[{}]",request.getPath().toString()); return false; } } diff --git a/src/main/java/com/example/gateway/routerImpl/GBKServerHttpResponseDecorator.java b/src/main/java/com/example/gateway/routerImpl/GBKServerHttpResponseDecorator.java new file mode 100644 index 0000000..35fa56c --- /dev/null +++ b/src/main/java/com/example/gateway/routerImpl/GBKServerHttpResponseDecorator.java @@ -0,0 +1,55 @@ +package com.example.gateway.routerImpl; + +import lombok.extern.slf4j.Slf4j; +import org.reactivestreams.Publisher; +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; +import reactor.core.publisher.Mono; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +@Slf4j +public class GBKServerHttpResponseDecorator extends ServerHttpResponseDecorator { + + public GBKServerHttpResponseDecorator(ServerHttpResponse delegate) { + super(delegate); + } + + @Override + public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) { + if (HttpStatus.OK.equals(getStatusCode()) && body instanceof Flux) { + Flux<? extends DataBuffer> fluxBody = Flux.from(body); + return super.writeWith(fluxBody.buffer().map(dataBuffers -> { + + StringBuffer stringBuffer = new StringBuffer(); + dataBuffers.forEach(dataBuffer -> { + byte[] content = new byte[dataBuffer.readableByteCount()]; + dataBuffer.read(content); + DataBufferUtils.release(dataBuffer); + String tempStr = new String(content, Charset.forName("ISO_8859_1")); + stringBuffer.append(tempStr); + }); + + String str = new String(stringBuffer); +// String fileKey = "\":\"" + Const.DATA_BASE_FILE_URL_HEAD + Const.M00; +// if (str.contains(fileKey)) { +// //修改 +// String value = "\":\"" + Const.SYSTEMS_FILE_URL_HEAD + Const.DATA_BASE_FILE_URL_HEAD + Const.M00; +// str = str.replaceAll(fileKey, value); +// } +// log.info("[RESPONSE]-{}",str); + byte[] content = str.getBytes(Charset.forName("ISO_8859_1")); + return bufferFactory().wrap(content); + })); + } + // if body is not a flux. never got there. + return super.writeWith(body); + } +} diff --git a/src/main/java/com/example/gateway/routerImpl/UTF8ServerHttpResponseDecorator.java b/src/main/java/com/example/gateway/routerImpl/UTF8ServerHttpResponseDecorator.java new file mode 100644 index 0000000..34c8acb --- /dev/null +++ b/src/main/java/com/example/gateway/routerImpl/UTF8ServerHttpResponseDecorator.java @@ -0,0 +1,50 @@ +package com.example.gateway.routerImpl; + +import lombok.extern.slf4j.Slf4j; +import org.reactivestreams.Publisher; +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.server.reactive.ServerHttpResponse; +import org.springframework.http.server.reactive.ServerHttpResponseDecorator; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +import java.nio.charset.StandardCharsets; +import java.util.Date; + +@Slf4j +public class UTF8ServerHttpResponseDecorator extends ServerHttpResponseDecorator { + + public UTF8ServerHttpResponseDecorator(ServerHttpResponse delegate) { + super(delegate); + } + + @Override + public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) { + + if (body instanceof Flux) { + Flux<? extends DataBuffer> fluxBody = (Flux<? extends DataBuffer>) body; + return super.writeWith(fluxBody.buffer().map(dataBuffer -> { + DataBufferFactory dataBufferFactory = new DefaultDataBufferFactory(); + DataBuffer join = dataBufferFactory.join(dataBuffer); + byte[] content = new byte[join.readableByteCount()]; + join.read(content); + + //释放掉内存 + DataBufferUtils.release(join); + String s = new String(content, StandardCharsets.UTF_8); + //TODO,s就是response的值, +// cacheService.saveCacheList(path,s,"10"); + log.info("[RESPONSE]-{}",s); + byte[] uppedContent = new String(s.getBytes(), StandardCharsets.UTF_8).getBytes(); + + return bufferFactory().wrap(uppedContent); + })); + } + + // if body is not a flux. never got there. + return super.writeWith(body); + } +}