拆分ResponseUtil‘
This commit is contained in:
@@ -1,21 +1,12 @@
|
|||||||
package com.central.common.utils;
|
package com.central.common.utils;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
|
||||||
import com.central.common.model.Result;
|
import com.central.common.model.Result;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
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.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 javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.nio.charset.Charset;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author zlt
|
* @author zlt
|
||||||
@@ -71,38 +62,4 @@ public class ResponseUtil {
|
|||||||
writer.flush();
|
writer.flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* webflux的response返回json对象
|
|
||||||
*/
|
|
||||||
public static Mono<Void> responseWriter(ServerWebExchange exchange, int httpStatus, String msg) {
|
|
||||||
Result result = Result.of(null, httpStatus, msg);
|
|
||||||
return responseWrite(exchange, httpStatus, result);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Mono<Void> responseFailed(ServerWebExchange exchange, String msg) {
|
|
||||||
Result result = Result.failed(msg);
|
|
||||||
return responseWrite(exchange, HttpStatus.INTERNAL_SERVER_ERROR.value(), result);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Mono<Void> responseFailed(ServerWebExchange exchange, int httpStatus, String msg) {
|
|
||||||
Result result = Result.failed(msg);
|
|
||||||
return responseWrite(exchange, httpStatus, result);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Mono<Void> 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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+57
@@ -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
|
||||||
|
* <p>
|
||||||
|
* Blog: https://zlt2000.gitee.io
|
||||||
|
* Github: https://github.com/zlt2000
|
||||||
|
*/
|
||||||
|
public class WebfluxResponseUtil {
|
||||||
|
/**
|
||||||
|
* webflux的response返回json对象
|
||||||
|
*/
|
||||||
|
public static Mono<Void> responseWriter(ServerWebExchange exchange, int httpStatus, String msg) {
|
||||||
|
Result result = Result.of(null, httpStatus, msg);
|
||||||
|
return responseWrite(exchange, httpStatus, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Mono<Void> responseFailed(ServerWebExchange exchange, String msg) {
|
||||||
|
Result result = Result.failed(msg);
|
||||||
|
return responseWrite(exchange, HttpStatus.INTERNAL_SERVER_ERROR.value(), result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Mono<Void> responseFailed(ServerWebExchange exchange, int httpStatus, String msg) {
|
||||||
|
Result result = Result.failed(msg);
|
||||||
|
return responseWrite(exchange, httpStatus, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Mono<Void> 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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
package com.central.gateway.auth;
|
package com.central.gateway.auth;
|
||||||
|
|
||||||
import com.central.common.utils.ResponseUtil;
|
import com.central.common.utils.WebfluxResponseUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.security.access.AccessDeniedException;
|
import org.springframework.security.access.AccessDeniedException;
|
||||||
@@ -21,6 +21,6 @@ import reactor.core.publisher.Mono;
|
|||||||
public class JsonAccessDeniedHandler implements ServerAccessDeniedHandler {
|
public class JsonAccessDeniedHandler implements ServerAccessDeniedHandler {
|
||||||
@Override
|
@Override
|
||||||
public Mono<Void> handle(ServerWebExchange exchange, AccessDeniedException e) {
|
public Mono<Void> handle(ServerWebExchange exchange, AccessDeniedException e) {
|
||||||
return ResponseUtil.responseFailed(exchange, HttpStatus.FORBIDDEN.value(), e.getMessage());
|
return WebfluxResponseUtil.responseFailed(exchange, HttpStatus.FORBIDDEN.value(), e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
package com.central.gateway.auth;
|
package com.central.gateway.auth;
|
||||||
|
|
||||||
import com.central.common.utils.ResponseUtil;
|
import com.central.common.utils.WebfluxResponseUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.security.core.AuthenticationException;
|
import org.springframework.security.core.AuthenticationException;
|
||||||
@@ -21,6 +21,6 @@ import reactor.core.publisher.Mono;
|
|||||||
public class JsonAuthenticationEntryPoint implements ServerAuthenticationEntryPoint {
|
public class JsonAuthenticationEntryPoint implements ServerAuthenticationEntryPoint {
|
||||||
@Override
|
@Override
|
||||||
public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException e) {
|
public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException e) {
|
||||||
return ResponseUtil.responseFailed(exchange, HttpStatus.UNAUTHORIZED.value(), e.getMessage());
|
return WebfluxResponseUtil.responseFailed(exchange, HttpStatus.UNAUTHORIZED.value(), e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user