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 f489366c236a6a35be3a8941a886a8bab22cf0d0..2b5bc750386a3d710d47442921af1e83359dc877 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 0000000000000000000000000000000000000000..5e5ed0232fcdd3059a9c792505fae6331e29fddf --- /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 03665b05df3876922a000c7f3c8326c21ef19f9a..8a213b8f34268500177a471ddee8dda4c9e6a6d9 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 df24212dd46e8a6baed970e2666fa86bf3289ec3..52dddddc6dde56794ce8f74454da6ec3f5de7b4b 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()); } }