Browse Source

河湖长域名调用

pull/44/head
chenjiandong 1 year ago
parent
commit
b3916af2a6
2 changed files with 19 additions and 2 deletions
  1. +1
    -1
      src/main/java/com/tuoheng/gateway/config/GatewayFilterConfig.java
  2. +18
    -1
      src/main/java/com/tuoheng/gateway/utils/GatewayUrlPathUtil.java

+ 1
- 1
src/main/java/com/tuoheng/gateway/config/GatewayFilterConfig.java View File

if(!StringUtils.isEmpty(clientId)){ if(!StringUtils.isEmpty(clientId)){
log.info("clientId is :{}", clientId); log.info("clientId is :{}", clientId);
String requestUrl = exchange.getRequest().getPath().value(); String requestUrl = exchange.getRequest().getPath().value();
log.info("requestUrl is :{}", clientId);
log.info("requestUrl is :{}", requestUrl);
//去除gateway path 前缀 //去除gateway path 前缀
String apiUrl = requestUrl.replace(GatewayUrlPathUtil.getPathByClientId(clientId),""); String apiUrl = requestUrl.replace(GatewayUrlPathUtil.getPathByClientId(clientId),"");
log.info("apiUrl is :{}", apiUrl); log.info("apiUrl is :{}", apiUrl);

+ 18
- 1
src/main/java/com/tuoheng/gateway/utils/GatewayUrlPathUtil.java View File



import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.tuoheng.gateway.commons.CommonsConfig; import com.tuoheng.gateway.commons.CommonsConfig;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*; import org.springframework.http.*;


private static final String HHZ_MP = "tuoheng-hhz-mp"; private static final String HHZ_MP = "tuoheng-hhz-mp";


private static final String FREEWAY_ADMIN = "tuoheng-freeway-admin";

private static final String FREEWAY_MP = "tuoheng-freeway-miniprogram";

/** /**
* 获取 gateway 路由前缀,匹配url * 获取 gateway 路由前缀,匹配url
* @param clientId * @param clientId
case HHZ_MP: case HHZ_MP:
apiPath = "/hhz/api"; apiPath = "/hhz/api";
break; break;
case FREEWAY_ADMIN:
apiPath = "/freeway/admin";
break;
case FREEWAY_MP:
apiPath = "/freeway/miniprogram";
break;
} }
return apiPath; return apiPath;
} }
* @return * @return
*/ */
public static List<Integer> getRoleIdByApiUrlPermission(String clientId, String apiUrl, String token){ public static List<Integer> getRoleIdByApiUrlPermission(String clientId, String apiUrl, String token){
log.info("getRoleIdByApiUrlPermission - start");
List<Integer> resList = new ArrayList<>(); List<Integer> resList = new ArrayList<>();
String url = "";
if(clientId.equals(HHZ_ADMIN) || clientId.equals(HHZ_MP)){ if(clientId.equals(HHZ_ADMIN) || clientId.equals(HHZ_MP)){
String url = CommonsConfig.hhzPermissionUrl;
url = CommonsConfig.hhzPermissionUrl;
}
if(StringUtils.isNotBlank(url)){
log.info("getRoleIdByApiUrlPermission - url:{}", url);
HttpHeaders resultRequestHeader = new HttpHeaders(); HttpHeaders resultRequestHeader = new HttpHeaders();
resultRequestHeader.add("Authorization", "Bearer " + token); resultRequestHeader.add("Authorization", "Bearer " + token);
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("apiUrl", apiUrl); json.put("apiUrl", apiUrl);
HttpEntity<JSONObject> entity = new HttpEntity<>(json, resultRequestHeader); HttpEntity<JSONObject> entity = new HttpEntity<>(json, resultRequestHeader);
String result = new RestTemplate().postForObject(url, entity, String.class); String result = new RestTemplate().postForObject(url, entity, String.class);
log.info("getRoleIdByApiUrlPermission - result:{}", result);
JSONObject jsonObject = JSONObject.parseObject(result); JSONObject jsonObject = JSONObject.parseObject(result);
Object obj = jsonObject.get("data"); Object obj = jsonObject.get("data");
if(!Objects.isNull(obj)){ if(!Objects.isNull(obj)){

Loading…
Cancel
Save