|
@@ -0,0 +1,43 @@
|
|
|
|
+package com.caimei365.cloud.config;
|
|
|
|
+
|
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
|
+import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.web.reactive.config.WebFluxConfigurer;
|
|
|
|
+import org.springframework.web.server.ServerWebExchange;
|
|
|
|
+import org.springframework.web.server.WebFilter;
|
|
|
|
+import org.springframework.web.server.WebFilterChain;
|
|
|
|
+import reactor.core.publisher.Mono;
|
|
|
|
+
|
|
|
|
+import java.net.InetSocketAddress;
|
|
|
|
+import java.util.Objects;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Description
|
|
|
|
+ *
|
|
|
|
+ * @author : Charles
|
|
|
|
+ * @date : 2021/3/24
|
|
|
|
+ */
|
|
|
|
+@Configuration
|
|
|
|
+public class WebConfiguration implements WebFluxConfigurer {
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * https://stackoverflow.com/questions/51192630/how-do-you-get-clients-ip-address-spring-webflux-websocket?rq=1
|
|
|
|
+ * https://stackoverflow.com/questions/50981136/how-to-get-client-ip-in-webflux
|
|
|
|
+ * https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-filters
|
|
|
|
+ * 由于在低版本的 spring-webflux 中不支持直接获得请求 IP(https://jira.spring.io/browse/SPR-16681),
|
|
|
|
+ * 因此从org.springframework.web.server.ServerWebExchange中获得 IP 后,在放到 header 里
|
|
|
|
+ */
|
|
|
|
+ @Component
|
|
|
|
+ public static class RetrieveClientIpWebFilter implements WebFilter {
|
|
|
|
+ @Override
|
|
|
|
+ public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
|
|
|
|
+ InetSocketAddress remoteAddress = exchange.getRequest().getRemoteAddress();
|
|
|
|
+ String clientIp = Objects.requireNonNull(remoteAddress).getAddress().getHostAddress();
|
|
|
|
+ ServerHttpRequest mutatedServerHttpRequest = exchange.getRequest().mutate().header("X-CLIENT-IP", clientIp).build();
|
|
|
|
+ ServerWebExchange mutatedServerWebExchange = exchange.mutate().request(mutatedServerHttpRequest).build();
|
|
|
|
+ return chain.filter(mutatedServerWebExchange);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|