|
@@ -1,10 +1,16 @@
|
|
|
package com.leanwo.gateway;
|
|
package com.leanwo.gateway;
|
|
|
|
|
|
|
|
|
|
+import java.net.URI;
|
|
|
|
|
+import java.net.URISyntaxException;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
|
|
|
|
+import org.springframework.cloud.gateway.filter.LoadBalancerClientFilter;
|
|
|
import org.springframework.core.Ordered;
|
|
import org.springframework.core.Ordered;
|
|
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
@@ -12,6 +18,7 @@ import org.springframework.web.server.ServerWebExchange;
|
|
|
|
|
|
|
|
import com.leanwo.gateway.dto.AccountManagementDto;
|
|
import com.leanwo.gateway.dto.AccountManagementDto;
|
|
|
import com.leanwo.gateway.service.AccountManagementService;
|
|
import com.leanwo.gateway.service.AccountManagementService;
|
|
|
|
|
+import com.leanwo.gateway.service.AccountManagementServiceImpl;
|
|
|
import com.leanwo.gateway.util.SpringUtil;
|
|
import com.leanwo.gateway.util.SpringUtil;
|
|
|
|
|
|
|
|
import reactor.core.publisher.Mono;
|
|
import reactor.core.publisher.Mono;
|
|
@@ -23,7 +30,8 @@ import reactor.core.publisher.Mono;
|
|
|
*/
|
|
*/
|
|
|
@Component//注入Spring容器
|
|
@Component//注入Spring容器
|
|
|
public class GlobalAccountFilter implements GlobalFilter, Ordered{
|
|
public class GlobalAccountFilter implements GlobalFilter, Ordered{
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ private static Logger logger = LoggerFactory.getLogger(GlobalAccountFilter.class);
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private AccountManagementService accountManagementService;
|
|
private AccountManagementService accountManagementService;
|
|
@@ -36,7 +44,7 @@ public class GlobalAccountFilter implements GlobalFilter, Ordered{
|
|
|
*/
|
|
*/
|
|
|
@Override
|
|
@Override
|
|
|
public int getOrder() {
|
|
public int getOrder() {
|
|
|
- return 0;
|
|
|
|
|
|
|
+ return LoadBalancerClientFilter.LOAD_BALANCER_CLIENT_FILTER_ORDER - 1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -48,19 +56,45 @@ public class GlobalAccountFilter implements GlobalFilter, Ordered{
|
|
|
*/
|
|
*/
|
|
|
//获取请求参数
|
|
//获取请求参数
|
|
|
ServerHttpRequest request = exchange.getRequest();
|
|
ServerHttpRequest request = exchange.getRequest();
|
|
|
- String url = request.getURI().toString();
|
|
|
|
|
|
|
+ //原始uri
|
|
|
|
|
+ URI originUri = request.getURI();
|
|
|
|
|
+ //构造器
|
|
|
|
|
+ ServerHttpRequest.Builder mutate = request.mutate();
|
|
|
|
|
+ //需要重定向的uri
|
|
|
|
|
+ String forwardedUri = originUri.toString();
|
|
|
|
|
+ if (StringUtils.startsWith(forwardedUri, "https")) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ //重新生成http请求方式的uri
|
|
|
|
|
+ URI uri = new URI(
|
|
|
|
|
+ "http",
|
|
|
|
|
+ originUri.getUserInfo(),
|
|
|
|
|
+ originUri.getHost(),
|
|
|
|
|
+ originUri.getPort(),
|
|
|
|
|
+ originUri.getPath(),
|
|
|
|
|
+ originUri.getQuery(),
|
|
|
|
|
+ originUri.getFragment()
|
|
|
|
|
+ );
|
|
|
|
|
+ mutate.uri(uri);
|
|
|
|
|
+ } catch (URISyntaxException e) {
|
|
|
|
|
+ throw new IllegalStateException(e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ //重新构建
|
|
|
|
|
+ ServerHttpRequest build = mutate.build();
|
|
|
|
|
+
|
|
|
|
|
+ String url = originUri.toString();
|
|
|
String path = request.getPath().toString();
|
|
String path = request.getPath().toString();
|
|
|
int endIndex = url.length() - path.length();
|
|
int endIndex = url.length() - path.length();
|
|
|
//获取域名
|
|
//获取域名
|
|
|
String domainName = url.substring(0, endIndex);
|
|
String domainName = url.substring(0, endIndex);
|
|
|
|
|
+ logger.debug("未截取前域名:" + domainName);
|
|
|
domainName = domainName.replace("http://", "").replace("https://", "");
|
|
domainName = domainName.replace("http://", "").replace("https://", "");
|
|
|
-
|
|
|
|
|
|
|
+ logger.debug("域名:" + domainName);
|
|
|
Long accountId = accountManagementService.getAccountIdByDomainName(domainName);
|
|
Long accountId = accountManagementService.getAccountIdByDomainName(domainName);
|
|
|
|
|
|
|
|
request.mutate().header("account", accountId.toString()).build();
|
|
request.mutate().header("account", accountId.toString()).build();
|
|
|
-
|
|
|
|
|
- //放行
|
|
|
|
|
- return chain.filter(exchange);//代表放行请求
|
|
|
|
|
|
|
+ //代表放行请求
|
|
|
|
|
+ return chain.filter(exchange.mutate().request(build).build());
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
}
|
|
}
|