io.irain.reactor.security.util.ReactiveBodyParamUtil Maven / Gradle / Ivy
package io.irain.reactor.security.util;
import cn.hutool.core.text.CharPool;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.security.web.server.context.SecurityContextServerWebExchange;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
/**
* @author youta
**/
@SuppressWarnings("unused")
public class ReactiveBodyParamUtil {
private static final String EQ = "=";
/**
* 从request中获取body
*
* @param request request
* @return body
*/
public static String parseBodyFromRequest(ServerHttpRequest request) {
Flux body = request.getBody();
AtomicReference atomRef = new AtomicReference<>();
body.subscribe(buffer -> {
ByteBuffer byteBuffer = ByteBuffer.allocate(buffer.readableByteCount());
buffer.toByteBuffer(byteBuffer);
CharBuffer charBuffer = StandardCharsets.UTF_8.decode(byteBuffer);
DataBufferUtils.release(buffer);
atomRef.set(charBuffer.toString());
});
return atomRef.get();
}
/**
* 从request中获取body
*
* @param request request
* @return body
*/
public static Mono