feat:add proxy api

This commit is contained in:
zzs01@yunemao.com 2024-09-20 19:04:23 +08:00
parent 6dae6ed4fd
commit 32bfbaf60b
8 changed files with 197 additions and 146 deletions

View File

@ -37,6 +37,8 @@ public class SecurityConfiguration {
.antMatchers(adminSeverContextPath + "/**").anonymous();
// 文件读取
registry.antMatchers(buildAdminApi("/infra/file/*/get/**")).permitAll();
registry.antMatchers("/crm/**").permitAll();
}
};

View File

@ -31,83 +31,6 @@
<artifactId>ensign-module-infra-biz</artifactId>
<version>${revision}</version>
</dependency>
<!-- 会员中心。默认注释,保证编译速度 -->
<!-- <dependency>-->
<!-- <groupId>com.ensign</groupId>-->
<!-- <artifactId>ensign-module-member-biz</artifactId>-->
<!-- <version>${revision}</version>-->
<!-- </dependency>-->
<!-- 数据报表。默认注释,保证编译速度 -->
<!-- <dependency>-->
<!-- <groupId>com.ensign</groupId>-->
<!-- <artifactId>ensign-module-report-biz</artifactId>-->
<!-- <version>${revision}</version>-->
<!-- </dependency>-->
<!-- 工作流。默认注释,保证编译速度 -->
<!-- <dependency>-->
<!-- <groupId>com.ensign</groupId>-->
<!-- <artifactId>ensign-module-bpm-biz</artifactId>-->
<!-- <version>${revision}</version>-->
<!-- </dependency>-->
<!-- 支付服务。默认注释,保证编译速度 -->
<!-- <dependency>-->
<!-- <groupId>com.ensign</groupId>-->
<!-- <artifactId>ensign-module-pay-biz</artifactId>-->
<!-- <version>${revision}</version>-->
<!-- </dependency>-->
<!-- 微信公众号模块。默认注释,保证编译速度 -->
<!-- <dependency>-->
<!-- <groupId>com.ensign</groupId>-->
<!-- <artifactId>ensign-module-mp-biz</artifactId>-->
<!-- <version>${revision}</version>-->
<!-- </dependency>-->
<!-- 商城相关模块。默认注释,保证编译速度-->
<!-- <dependency>-->
<!-- <groupId>com.ensign</groupId>-->
<!-- <artifactId>ensign-module-promotion-biz</artifactId>-->
<!-- <version>${revision}</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.ensign</groupId>-->
<!-- <artifactId>ensign-module-product-biz</artifactId>-->
<!-- <version>${revision}</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.ensign</groupId>-->
<!-- <artifactId>ensign-module-trade-biz</artifactId>-->
<!-- <version>${revision}</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.ensign</groupId>-->
<!-- <artifactId>ensign-module-statistics-biz</artifactId>-->
<!-- <version>${revision}</version>-->
<!-- </dependency>-->
<!-- CRM 相关模块。默认注释,保证编译速度 -->
<!-- <dependency>-->
<!-- <groupId>com.ensign</groupId>-->
<!-- <artifactId>ensign-module-crm-biz</artifactId>-->
<!-- <version>${revision}</version>-->
<!-- </dependency>-->
<!-- ERP 相关模块。默认注释,保证编译速度 -->
<!-- <dependency>-->
<!-- <groupId>com.ensign</groupId>-->
<!-- <artifactId>ensign-module-erp-biz</artifactId>-->
<!-- <version>${revision}</version>-->
<!-- </dependency>-->
<!-- AI 大模型相关模块。默认注释,保证编译速度 -->
<!-- <dependency>-->
<!-- <groupId>com.ensign</groupId>-->
<!-- <artifactId>ensign-module-ai-biz</artifactId>-->
<!-- <version>${revision}</version>-->
<!-- </dependency>-->
<!-- spring boot 配置所需依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -1,68 +0,0 @@
package com.ensign.crm.server.controller;
import com.ensign.crm.framework.common.pojo.CommonResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import static com.ensign.crm.framework.common.exception.enums.GlobalErrorCodeConstants.NOT_IMPLEMENTED;
/**
* 默认 Controller解决部分 module 未开启时的 404 提示
* 例如说/bpm/** 路径工作流
*
* @author 芋道源码
*/
@RestController
public class DefaultController {
@RequestMapping("/admin-api/bpm/**")
public CommonResult<Boolean> bpm404() {
return CommonResult.error(NOT_IMPLEMENTED.getCode(),
"[工作流模块 ensign-module-bpm - 已禁用][参考 https://doc.iocoder.cn/bpm/ 开启]");
}
@RequestMapping("/admin-api/mp/**")
public CommonResult<Boolean> mp404() {
return CommonResult.error(NOT_IMPLEMENTED.getCode(),
"[微信公众号 ensign-module-mp - 已禁用][参考 https://doc.iocoder.cn/mp/build/ 开启]");
}
@RequestMapping(value = {"/admin-api/product/**", // 商品中心
"/admin-api/trade/**", // 交易中心
"/admin-api/promotion/**"}) // 营销中心
public CommonResult<Boolean> mall404() {
return CommonResult.error(NOT_IMPLEMENTED.getCode(),
"[商城系统 ensign-module-mall - 已禁用][参考 https://doc.iocoder.cn/mall/build/ 开启]");
}
@RequestMapping("/admin-api/erp/**")
public CommonResult<Boolean> erp404() {
return CommonResult.error(NOT_IMPLEMENTED.getCode(),
"[ERP 模块 ensign-module-erp - 已禁用][参考 https://doc.iocoder.cn/erp/build/ 开启]");
}
@RequestMapping("/admin-api/crm/**")
public CommonResult<Boolean> crm404() {
return CommonResult.error(NOT_IMPLEMENTED.getCode(),
"[CRM 模块 ensign-module-crm - 已禁用][参考 https://doc.iocoder.cn/crm/build/ 开启]");
}
@RequestMapping(value = {"/admin-api/report/**"})
public CommonResult<Boolean> report404() {
return CommonResult.error(NOT_IMPLEMENTED.getCode(),
"[报表模块 ensign-module-report - 已禁用][参考 https://doc.iocoder.cn/report/ 开启]");
}
@RequestMapping(value = {"/admin-api/pay/**"})
public CommonResult<Boolean> pay404() {
return CommonResult.error(NOT_IMPLEMENTED.getCode(),
"[支付模块 ensign-module-pay - 已禁用][参考 https://doc.iocoder.cn/pay/build/ 开启]");
}
@RequestMapping(value = {"/admin-api/ai/**"})
public CommonResult<Boolean> ai404() {
return CommonResult.error(NOT_IMPLEMENTED.getCode(),
"[AI 大模型 ensign-module-ai - 已禁用][参考 https://doc.iocoder.cn/ai/build/ 开启]");
}
}

View File

@ -0,0 +1,74 @@
package com.ensign.crm.server.controller;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
/**
* @Description: TODO
* @Date: 2024/9/20 18:28
* @Created: by ZZSLL
*/
@RestController
@RequestMapping("/ensign/crm")
public class ProxyController {
private String targetAddr = "http://10.64.112.152:8022";
@RequestMapping(value = "/proxy/**", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public void proxy(HttpServletRequest request, HttpServletResponse response) throws IOException, URISyntaxException {
URI uri = new URI(request.getRequestURI());
String path = uri.getPath();
String query = request.getQueryString();
String target = targetAddr + path.replace("/ensign/crm/proxy", "");
if (query != null && !query.isEmpty() && !query.equals("null")) {
target = target + "?" + query;
}
URI newUri = new URI(target);
// 执行代理查询
String methodName = request.getMethod();
HttpMethod httpMethod = HttpMethod.resolve(methodName);
if (httpMethod == null) {
return;
}
ClientHttpRequest delegate = new SimpleClientHttpRequestFactory().createRequest(newUri, httpMethod);
Enumeration<String> headerNames = request.getHeaderNames();
// 设置请求头
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
Enumeration<String> v = request.getHeaders(headerName);
List<String> arr = new ArrayList<>();
while (v.hasMoreElements()) {
arr.add(v.nextElement());
}
delegate.getHeaders().addAll(headerName, arr);
}
StreamUtils.copy(request.getInputStream(), delegate.getBody());
// 执行远程调用
ClientHttpResponse clientHttpResponse = delegate.execute();
response.setStatus(clientHttpResponse.getStatusCode().value());
// 设置响应头
clientHttpResponse.getHeaders().forEach((key, value) -> value.forEach(it -> {
response.setHeader(key, it);
}));
StreamUtils.copy(clientHttpResponse.getBody(), response.getOutputStream());
}
}

View File

@ -0,0 +1,13 @@
package com.ensign.crm.server.exception;
/**
* @Description: TODO
* @Date: 2024/9/20 18:33
* @Created: by ZZSLL
*/
public class AllKingdeeException extends Exception {
public AllKingdeeException(String message) {
super(message);
}
}

View File

@ -0,0 +1,85 @@
package com.ensign.crm.server.utils;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ensign.crm.server.exception.AllKingdeeException;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @Description: TODO
* @Date: 2024/9/20 18:33
* @Created: by ZZSLL
*/
@Slf4j
public class ProxyUtils {
public void initAppToken() throws IOException, AllKingdeeException
{
final String END_POINT = "/ierp/api/getAppToken.do";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(END_POINT);
Map<String, String> body = new HashMap<>();
body.put("appId", "");
body.put("appSecuret", "");
body.put("tenantid", "");
body.put("accountId", "");
body.put("language", "");
HttpEntity reqEntity = new StringEntity(JSONUtil.toJsonStr(body), "utf-8");
post.setEntity(reqEntity);
post.setConfig(RequestConf.initRequestConfig());
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity resEntity = response.getEntity();
String responseData = EntityUtils.toString(resEntity, "utf-8");
JSONObject jsonObject = JSONUtil.parseObj(responseData);
Boolean status = (Boolean) jsonObject.get("status");
String state = (String) jsonObject.get("state");
if (status && "success".equals(state)) {
JSONObject data = (JSONObject) jsonObject.get("data");
String appToken = (String) data.get("app_token");
if (appToken != null && !appToken.isEmpty()) {
Long expireTime = (Long) data.get("expire_time");
return;
}
}
log.error("获取appToken失败 {}", jsonObject);
throw new AllKingdeeException("获取appToken失败");
}
log.error("获取appToken失败 {}", EntityUtils.toString(response.getEntity(), "utf-8"));
throw new AllKingdeeException("获取appToken失败");
}
}

View File

@ -0,0 +1,22 @@
package com.ensign.crm.server.utils;
import org.apache.http.client.config.RequestConfig;
import org.springframework.stereotype.Component;
/**
* @Description: TODO
* @Date: 2024/9/20 18:32
* @Created: by ZZSLL
*/
@Component
public class RequestConf
{
public static RequestConfig initRequestConfig() {
return RequestConfig.custom()
.setConnectTimeout(5000)//连接超时connectionTimeout-->指的是连接一个url的连接等待时间
.setSocketTimeout(5000)// 读取数据超时SocketTimeout-->指的是连接上一个url获取response的返回等待时间
.setConnectionRequestTimeout(5000)
.build();
}
}

View File

@ -108,7 +108,6 @@ aj:
cache-number: 1000 # local 缓存的阈值,达到这个值,清除缓存
timing-clear: 180 # local定时清除过期缓存(单位秒),设置为0代表不执行
type: blockPuzzle # 验证码类型 default两种都实例化。 blockPuzzle 滑块拼图 clickWord 文字点选
water-mark: 芋道源码 # 右下角水印文字(我的水印),可使用 https://tool.chinaz.com/tools/unicode.aspx 中文转 UnicodeLinux 可能需要转 unicode
interference-options: 0 # 滑动干扰项(0/1/2)
req-frequency-limit-enable: false # 接口请求次数一分钟限制是否开启 true|false
req-get-lock-limit: 5 # 验证失败 5 次get接口锁定
@ -197,6 +196,7 @@ ensign:
- /admin-api/pay/notify/** # 支付回调通知,不携带租户编号
- /jmreport/* # 积木报表,无法携带租户编号
- /admin-api/mp/open/** # 微信公众号开放平台,微信回调接口,无法携带租户编号
- /admin-api/crm/**
ignore-tables:
- system_tenant
- system_tenant_package