From d828520b1dcb3c8706c1354b81594d849a6c0b45 Mon Sep 17 00:00:00 2001 From: zlt2000 Date: Tue, 5 May 2020 18:54:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=86=E5=88=86ResponseUtil=E2=80=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../central/common/utils/ResponseUtil.java | 43 -------------- .../common/utils/WebfluxResponseUtil.java | 57 +++++++++++++++++++ .../gateway/auth/JsonAccessDeniedHandler.java | 4 +- .../auth/JsonAuthenticationEntryPoint.java | 4 +- 4 files changed, 61 insertions(+), 47 deletions(-) create mode 100644 zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/WebfluxResponseUtil.java diff --git a/zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/ResponseUtil.java b/zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/ResponseUtil.java index f489366..2b5bc75 100644 --- a/zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/ResponseUtil.java +++ b/zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/ResponseUtil.java @@ -1,21 +1,12 @@ package com.central.common.utils; -import com.alibaba.fastjson.JSONObject; import com.central.common.model.Result; import com.fasterxml.jackson.databind.ObjectMapper; -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.MediaType; -import org.springframework.http.server.reactive.ServerHttpResponse; -import org.springframework.web.server.ServerWebExchange; -import reactor.core.publisher.Mono; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.Writer; -import java.nio.charset.Charset; /** * @author zlt @@ -71,38 +62,4 @@ public class ResponseUtil { writer.flush(); } } - - /** - * webflux的response返回json对象 - */ - public static Mono responseWriter(ServerWebExchange exchange, int httpStatus, String msg) { - Result result = Result.of(null, httpStatus, msg); - return responseWrite(exchange, httpStatus, result); - } - - public static Mono responseFailed(ServerWebExchange exchange, String msg) { - Result result = Result.failed(msg); - return responseWrite(exchange, HttpStatus.INTERNAL_SERVER_ERROR.value(), result); - } - - public static Mono responseFailed(ServerWebExchange exchange, int httpStatus, String msg) { - Result result = Result.failed(msg); - return responseWrite(exchange, httpStatus, result); - } - - public static Mono responseWrite(ServerWebExchange exchange, int httpStatus, Result result) { - if (httpStatus == 0) { - httpStatus = HttpStatus.INTERNAL_SERVER_ERROR.value(); - } - ServerHttpResponse response = exchange.getResponse(); - response.getHeaders().setAccessControlAllowCredentials(true); - response.getHeaders().setAccessControlAllowOrigin("*"); - response.setStatusCode(HttpStatus.valueOf(httpStatus)); - response.getHeaders().setContentType(MediaType.APPLICATION_JSON_UTF8); - DataBufferFactory dataBufferFactory = response.bufferFactory(); - DataBuffer buffer = dataBufferFactory.wrap(JSONObject.toJSONString(result).getBytes(Charset.defaultCharset())); - return response.writeWith(Mono.just(buffer)).doOnError((error) -> { - DataBufferUtils.release(buffer); - }); - } } diff --git a/zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/WebfluxResponseUtil.java b/zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/WebfluxResponseUtil.java new file mode 100644 index 0000000..5e5ed02 --- /dev/null +++ b/zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/WebfluxResponseUtil.java @@ -0,0 +1,57 @@ +package com.central.common.utils; + +import com.alibaba.fastjson.JSONObject; +import com.central.common.model.Result; +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.MediaType; +import org.springframework.http.server.reactive.ServerHttpResponse; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +import java.nio.charset.Charset; + +/** + * @author zlt + * @date 2020/5/5 + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 + */ +public class WebfluxResponseUtil { + /** + * webflux的response返回json对象 + */ + public static Mono responseWriter(ServerWebExchange exchange, int httpStatus, String msg) { + Result result = Result.of(null, httpStatus, msg); + return responseWrite(exchange, httpStatus, result); + } + + public static Mono responseFailed(ServerWebExchange exchange, String msg) { + Result result = Result.failed(msg); + return responseWrite(exchange, HttpStatus.INTERNAL_SERVER_ERROR.value(), result); + } + + public static Mono responseFailed(ServerWebExchange exchange, int httpStatus, String msg) { + Result result = Result.failed(msg); + return responseWrite(exchange, httpStatus, result); + } + + public static Mono responseWrite(ServerWebExchange exchange, int httpStatus, Result result) { + if (httpStatus == 0) { + httpStatus = HttpStatus.INTERNAL_SERVER_ERROR.value(); + } + ServerHttpResponse response = exchange.getResponse(); + response.getHeaders().setAccessControlAllowCredentials(true); + response.getHeaders().setAccessControlAllowOrigin("*"); + response.setStatusCode(HttpStatus.valueOf(httpStatus)); + response.getHeaders().setContentType(MediaType.APPLICATION_JSON_UTF8); + DataBufferFactory dataBufferFactory = response.bufferFactory(); + DataBuffer buffer = dataBufferFactory.wrap(JSONObject.toJSONString(result).getBytes(Charset.defaultCharset())); + return response.writeWith(Mono.just(buffer)).doOnError((error) -> { + DataBufferUtils.release(buffer); + }); + } +} diff --git a/zlt-gateway/sc-gateway/src/main/java/com/central/gateway/auth/JsonAccessDeniedHandler.java b/zlt-gateway/sc-gateway/src/main/java/com/central/gateway/auth/JsonAccessDeniedHandler.java index 03665b0..8a213b8 100644 --- a/zlt-gateway/sc-gateway/src/main/java/com/central/gateway/auth/JsonAccessDeniedHandler.java +++ b/zlt-gateway/sc-gateway/src/main/java/com/central/gateway/auth/JsonAccessDeniedHandler.java @@ -1,6 +1,6 @@ package com.central.gateway.auth; -import com.central.common.utils.ResponseUtil; +import com.central.common.utils.WebfluxResponseUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.security.access.AccessDeniedException; @@ -21,6 +21,6 @@ import reactor.core.publisher.Mono; public class JsonAccessDeniedHandler implements ServerAccessDeniedHandler { @Override public Mono handle(ServerWebExchange exchange, AccessDeniedException e) { - return ResponseUtil.responseFailed(exchange, HttpStatus.FORBIDDEN.value(), e.getMessage()); + return WebfluxResponseUtil.responseFailed(exchange, HttpStatus.FORBIDDEN.value(), e.getMessage()); } } diff --git a/zlt-gateway/sc-gateway/src/main/java/com/central/gateway/auth/JsonAuthenticationEntryPoint.java b/zlt-gateway/sc-gateway/src/main/java/com/central/gateway/auth/JsonAuthenticationEntryPoint.java index df24212..52ddddd 100644 --- a/zlt-gateway/sc-gateway/src/main/java/com/central/gateway/auth/JsonAuthenticationEntryPoint.java +++ b/zlt-gateway/sc-gateway/src/main/java/com/central/gateway/auth/JsonAuthenticationEntryPoint.java @@ -1,6 +1,6 @@ package com.central.gateway.auth; -import com.central.common.utils.ResponseUtil; +import com.central.common.utils.WebfluxResponseUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.security.core.AuthenticationException; @@ -21,6 +21,6 @@ import reactor.core.publisher.Mono; public class JsonAuthenticationEntryPoint implements ServerAuthenticationEntryPoint { @Override public Mono commence(ServerWebExchange exchange, AuthenticationException e) { - return ResponseUtil.responseFailed(exchange, HttpStatus.UNAUTHORIZED.value(), e.getMessage()); + return WebfluxResponseUtil.responseFailed(exchange, HttpStatus.UNAUTHORIZED.value(), e.getMessage()); } } -- GitLab