|
@@ -5,7 +5,10 @@ import org.apache.commons.lang.StringUtils;
|
|
|
import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.http.HttpCookie;
|
|
|
+import org.springframework.http.ResponseCookie;
|
|
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
|
+import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.reactive.config.WebFluxConfigurer;
|
|
|
import org.springframework.web.server.ServerWebExchange;
|
|
@@ -16,6 +19,7 @@ import reactor.core.publisher.Mono;
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.net.InetSocketAddress;
|
|
|
+import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
/**
|
|
@@ -27,16 +31,17 @@ import java.util.Objects;
|
|
|
@Configuration
|
|
|
public class WebConfiguration implements WebFluxConfigurer {
|
|
|
|
|
|
- @Autowired
|
|
|
- private TouristService touristService;
|
|
|
+ @Autowired
|
|
|
+ private TouristService touristService;
|
|
|
|
|
|
- private static WebConfiguration webConfiguration;
|
|
|
+ private static WebConfiguration webConfiguration;
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void init() {
|
|
|
+ webConfiguration = this;
|
|
|
+ webConfiguration.touristService = this.touristService;
|
|
|
+ }
|
|
|
|
|
|
- @PostConstruct
|
|
|
- public void init() {
|
|
|
- webConfiguration = this;
|
|
|
- webConfiguration.touristService = this.touristService;
|
|
|
- }
|
|
|
/**
|
|
|
* 获取客户端IP
|
|
|
* https://stackoverflow.com/questions/51192630/how-do-you-get-clients-ip-address-spring-webflux-websocket?rq=1
|
|
@@ -50,6 +55,7 @@ public class WebConfiguration implements WebFluxConfigurer {
|
|
|
@Override
|
|
|
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
|
|
|
ServerHttpRequest request = exchange.getRequest();
|
|
|
+ ServerHttpResponse response = exchange.getResponse();
|
|
|
// 从我们的 nginx 重写请求获取的参数,X-Real-IP是在nginx中配置的,要对应上;
|
|
|
String clientIp = request.getHeaders().getFirst("X-Real-IP");
|
|
|
if (StringUtils.isEmpty(clientIp)) {
|
|
@@ -57,15 +63,14 @@ public class WebConfiguration implements WebFluxConfigurer {
|
|
|
clientIp = Objects.requireNonNull(remoteAddress).getAddress().getHostAddress();
|
|
|
}
|
|
|
ServerHttpRequest mutatedServerHttpRequest = request.mutate().header("X-CLIENT-IP", clientIp).build();
|
|
|
+ HttpCookie tid = request.getCookies().getFirst("tid");
|
|
|
+ if (null == tid || StringUtils.isEmpty(tid.getValue())) {
|
|
|
+ String touristId = webConfiguration.touristService.touristInfo();
|
|
|
+ ResponseCookie cookie = ResponseCookie.from("tid", touristId).path("/").httpOnly(true).build();
|
|
|
+ response.addCookie(cookie);
|
|
|
+ }
|
|
|
ServerWebExchange mutatedServerWebExchange = exchange.mutate().request(mutatedServerHttpRequest).build();
|
|
|
- chain.filter(mutatedServerWebExchange);
|
|
|
- // 游客编号
|
|
|
- //TouristService touristService = new TouristService();
|
|
|
- String touristId = webConfiguration.touristService.touristInfo();
|
|
|
- return exchange.getSession().flatMap(session -> {
|
|
|
- session.getAttributes().put("touristId", touristId);
|
|
|
- return chain.filter(exchange);
|
|
|
- });
|
|
|
+ return chain.filter(mutatedServerWebExchange);
|
|
|
}
|
|
|
}
|
|
|
|