Forráskód Böngészése

gateway网关调整

ZhangWenQiang 5 éve
szülő
commit
655f4cc8af

+ 9 - 2
happy-cloud-gateway/src/main/java/org/jeecg/filter/GlobalAccessTokenFilter.java

@@ -20,12 +20,19 @@ import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.a
 @Component
 public class GlobalAccessTokenFilter implements GlobalFilter, Ordered {
     public final static String X_ACCESS_TOKEN = "X-Access-Token";
+    public final static String X_GATEWAY_BASE_PATH = "X_GATEWAY_BASE_PATH";
 
     @Override
     public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
         String url = exchange.getRequest().getURI().getPath();
 
-        log.info("  access url :  "+ url);
+        log.info("  access url :  " + url);
+
+        String scheme = exchange.getRequest().getURI().getScheme();
+        String host = exchange.getRequest().getURI().getHost();
+        int port = exchange.getRequest().getURI().getPort();
+        String basePath = scheme + "://" + host + ":" + port;
+//        log.info(" base path :  "+ basePath);
 
         // 1. 重写StripPrefix(获取真实的URL)
         addOriginalRequestUrl(exchange, exchange.getRequest().getURI());
@@ -35,7 +42,7 @@ public class GlobalAccessTokenFilter implements GlobalFilter, Ordered {
         exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, newRequest.getURI());
 
         //将现在的request,添加当前身份
-        ServerHttpRequest mutableReq = exchange.getRequest().mutate().header("Authorization-UserName", "").build();
+        ServerHttpRequest mutableReq = exchange.getRequest().mutate().header("Authorization-UserName", "").header(X_GATEWAY_BASE_PATH, basePath).build();
         ServerWebExchange mutableExchange = exchange.mutate().request(mutableReq).build();
         return chain.filter(mutableExchange);
     }

+ 12 - 8
happy-common/happy-common-core/src/main/java/org/jeecg/common/constant/ServiceNameConstants.java

@@ -24,14 +24,18 @@ package org.jeecg.common.constant;
  * 服务名称
  */
 public interface ServiceNameConstants {
-	/**
-	 * 结算中心服务的SERVICEID
-	 */
-	String WISDOM_SERVICE = "happy-cloud-wisdom-biz";
+    /**
+     * 结算中心服务的SERVICEID
+     */
+    String WISDOM_SERVICE = "happy-cloud-wisdom-biz";
 
-	/**
-	 * 系统管理 admin
-	 */
-	String SYSTEM_SERVICE = "happy-cloud-system-biz";
+    /**
+     * 系统管理 admin
+     */
+    String SYSTEM_SERVICE = "happy-cloud-system-biz";
 
+    /**
+     * gateway通过header传递根路径 basePath
+     */
+    String X_GATEWAY_BASE_PATH = "X_GATEWAY_BASE_PATH";
 }

+ 69 - 62
happy-common/happy-common-core/src/main/java/org/jeecg/common/util/SpringContextUtils.java

@@ -2,6 +2,7 @@ package org.jeecg.common.util;
 
 import javax.servlet.http.HttpServletRequest;
 
+import org.jeecg.common.constant.ServiceNameConstants;
 import org.springframework.beans.BeansException;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationContextAware;
@@ -12,73 +13,79 @@ import org.springframework.web.context.request.ServletRequestAttributes;
 @Component
 public class SpringContextUtils implements ApplicationContextAware {
 
-	/**
-	 * 上下文对象实例
-	 */
-	private static ApplicationContext applicationContext;
+    /**
+     * 上下文对象实例
+     */
+    private static ApplicationContext applicationContext;
 
-	@Override
-	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
-		SpringContextUtils.applicationContext = applicationContext;
-	}
+    @Override
+    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
+        SpringContextUtils.applicationContext = applicationContext;
+    }
 
-	/**
-	 * 获取applicationContext
-	 *
-	 * @return
-	 */
-	public static ApplicationContext getApplicationContext() {
-		return applicationContext;
-	}
+    /**
+     * 获取applicationContext
+     *
+     * @return
+     */
+    public static ApplicationContext getApplicationContext() {
+        return applicationContext;
+    }
 
-	/**
-	  * 获取HttpServletRequest
-	 */
-	public static HttpServletRequest getHttpServletRequest() {
-		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
-	}
+    /**
+     * 获取HttpServletRequest
+     */
+    public static HttpServletRequest getHttpServletRequest() {
+        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
+    }
 
-	public static String getDomain(){
-		HttpServletRequest request = getHttpServletRequest();
-		StringBuffer url = request.getRequestURL();
-		return url.delete(url.length() - request.getRequestURI().length(), url.length()).toString();
-	}
+    public static String getDomain() {
+        HttpServletRequest request = getHttpServletRequest();
+        StringBuffer url = request.getRequestURL();
+        //微服务情况下,获取gateway的basePath
+        String basePath = request.getHeader(ServiceNameConstants.X_GATEWAY_BASE_PATH);
+        if (oConvertUtils.isNotEmpty(basePath)) {
+            return basePath;
+        } else {
+            return url.delete(url.length() - request.getRequestURI().length(), url.length()).toString();
+        }
+    }
 
-	public static String getOrigin(){
-		HttpServletRequest request = getHttpServletRequest();
-		return request.getHeader("Origin");
-	}
-	
-	/**
-	 * 通过name获取 Bean.
-	 *
-	 * @param name
-	 * @return
-	 */
-	public static Object getBean(String name) {
-		return getApplicationContext().getBean(name);
-	}
+    public static String getOrigin() {
+        HttpServletRequest request = getHttpServletRequest();
+        return request.getHeader("Origin");
+    }
 
-	/**
-	 * 通过class获取Bean.
-	 *
-	 * @param clazz
-	 * @param       <T>
-	 * @return
-	 */
-	public static <T> T getBean(Class<T> clazz) {
-		return getApplicationContext().getBean(clazz);
-	}
+    /**
+     * 通过name获取 Bean.
+     *
+     * @param name
+     * @return
+     */
+    public static Object getBean(String name) {
+        return getApplicationContext().getBean(name);
+    }
 
-	/**
-	 * 通过name,以及Clazz返回指定的Bean
-	 *
-	 * @param name
-	 * @param clazz
-	 * @param       <T>
-	 * @return
-	 */
-	public static <T> T getBean(String name, Class<T> clazz) {
-		return getApplicationContext().getBean(name, clazz);
-	}
+    /**
+     * 通过class获取Bean.
+     *
+     * @param clazz
+     * @param <T>
+     * @return
+     */
+    public static <T> T getBean(Class<T> clazz) {
+        return getApplicationContext().getBean(clazz);
+    }
+
+    /**
+     * 通过name,以及Clazz返回指定的Bean
+     *
+     * @param name
+     * @param clazz
+     * @param <T>
+     * @return
+     */
+    public static <T> T getBean(String name, Class<T> clazz) {
+        return getApplicationContext().getBean(name, clazz);
+    }
 }