|
|
@@ -6,11 +6,15 @@ import com.onemap.apply.service.yzt.IZymlService;
|
|
|
import com.onemap.common.core.utils.StringUtils;
|
|
|
import com.onemap.common.core.web.domain.RequestResult;
|
|
|
import com.onemap.common.security.utils.SecurityUtils;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
import java.util.*;
|
|
|
|
|
|
@Service
|
|
|
@@ -19,6 +23,8 @@ public class ZymlServiceImpl implements IZymlService {
|
|
|
@Autowired
|
|
|
private ZymlMapper zymlMapper;
|
|
|
|
|
|
+ private static final ThreadLocal<String> IPDATA = new ThreadLocal<>();
|
|
|
+
|
|
|
/**
|
|
|
* 获取资源目录列表
|
|
|
*
|
|
|
@@ -597,4 +603,76 @@ public class ZymlServiceImpl implements IZymlService {
|
|
|
data.put("stats", stats);
|
|
|
return data;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据请求获取ip地址
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getClientIp() {
|
|
|
+ String clientIp = "";
|
|
|
+ ServletRequestAttributes attributes =
|
|
|
+ (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
+ if (Objects.nonNull(attributes)) {
|
|
|
+ HttpServletRequest request = attributes.getRequest();
|
|
|
+ clientIp = request.getRemoteAddr();
|
|
|
+ // 处理 X-Forwarded-For 头部信息(对于反向代理或负载均衡器很有用)
|
|
|
+ if (request.getHeader("X-Forwarded-For") != null) {
|
|
|
+ clientIp = request.getHeader("X-Forwarded-For").split(",")[0];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return clientIp;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public RequestResult convertGetTree(RequestResult result) {
|
|
|
+ if (Objects.isNull(result)) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ if (Objects.isNull(result.get("data"))) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ String clientIp = getClientIp();
|
|
|
+ if (StringUtils.isBlank(clientIp)) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ List<ZymlTreeDataDTO> list = (List<ZymlTreeDataDTO>) result.get("data");
|
|
|
+ IPDATA.set(clientIp);
|
|
|
+ replaceIpList(list);
|
|
|
+ IPDATA.remove();
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 这里进行ip替换
|
|
|
+ * @param list
|
|
|
+ */
|
|
|
+ public static void replaceIpList(List<ZymlTreeDataDTO> list) {
|
|
|
+ String clientIp = IPDATA.get();
|
|
|
+ if (!"0:0:0:0:0:0:0:1".equalsIgnoreCase(clientIp) && !"127.0.0.1".equalsIgnoreCase(clientIp)) {
|
|
|
+ for (ZymlTreeDataDTO obj : list) {
|
|
|
+ if (Objects.nonNull(obj)) {
|
|
|
+ List<ZymlTreeDataDTO> subList = obj.getChildren();
|
|
|
+ if (CollectionUtils.isNotEmpty(subList)) {
|
|
|
+ replaceIpList(subList);
|
|
|
+ } else {
|
|
|
+ String url = obj.getUrl();
|
|
|
+ if (url != null && url.startsWith("http://")) {
|
|
|
+ int index = url.indexOf(":", "http://".length());
|
|
|
+ if (index > 0) {
|
|
|
+ String urlNew = url.substring(0, index);
|
|
|
+ obj.setUrl(url.replace(urlNew, "http://" + clientIp));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|