Commit bb6a4cb9f4b78112e9e6cff791bd2abd076a90db
1 parent
de675214
移除API日志记录ApiLogAspect类中非必要代码,移除HttpUtils工具类,统一使用OKHttpUtils
Signed-off-by: TanYibin <5491541@qq.com>
Showing
11 changed files
with
415 additions
and
597 deletions
huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/acs/controller/AcsController.java
1 | 1 | package org.jeecg.modules.wms.api.acs.controller; |
2 | 2 | |
3 | +import java.util.Map; | |
4 | + | |
3 | 5 | import javax.annotation.Resource; |
6 | +import javax.servlet.http.HttpServletRequest; | |
4 | 7 | |
5 | 8 | import org.jeecg.common.api.vo.Result; |
6 | 9 | import org.jeecg.modules.wms.api.acs.entity.AcsStatus; |
... | ... | @@ -11,7 +14,11 @@ import org.jeecg.modules.wms.task.agvTask.service.IAgvTaskService; |
11 | 14 | import org.jeecg.utils.StringUtils; |
12 | 15 | import org.jeecg.utils.constant.QuantityConstant; |
13 | 16 | import org.springframework.transaction.annotation.Transactional; |
14 | -import org.springframework.web.bind.annotation.*; | |
17 | +import org.springframework.web.bind.annotation.PostMapping; | |
18 | +import org.springframework.web.bind.annotation.RequestBody; | |
19 | +import org.springframework.web.bind.annotation.RequestMapping; | |
20 | +import org.springframework.web.bind.annotation.ResponseBody; | |
21 | +import org.springframework.web.bind.annotation.RestController; | |
15 | 22 | |
16 | 23 | import io.swagger.annotations.ApiOperation; |
17 | 24 | |
... | ... | @@ -23,6 +30,17 @@ public class AcsController extends HuahengBaseController { |
23 | 30 | private IAcsService acsService; |
24 | 31 | @Resource |
25 | 32 | private IAgvTaskService agvTaskService; |
33 | + | |
34 | +// @PassApiAuthentication | |
35 | + @ApiLogger(apiName = "API接口第三方Token校验测试", from = "TEST") | |
36 | + @ResponseBody | |
37 | + @PostMapping(value = "/testTokenCheck") | |
38 | + public Result<?> testTokenCheck(@RequestBody Map<String, String> paramMap, HttpServletRequest request) { | |
39 | + | |
40 | + | |
41 | + return new Result<>(); | |
42 | + } | |
43 | + | |
26 | 44 | |
27 | 45 | @PostMapping("/notifyAGVTask") |
28 | 46 | @ApiOperation("更新AGV状态") |
... | ... |
huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/acs/service/impl/AcsServiceImpl.java
... | ... | @@ -57,7 +57,7 @@ public class AcsServiceImpl implements IAcsService { |
57 | 57 | } |
58 | 58 | } |
59 | 59 | } |
60 | - Location toLocation = locationService.getLocationByCode(toPort, warehouseCode); | |
60 | +// Location toLocation = locationService.getLocationByCode(toPort, warehouseCode); | |
61 | 61 | agvTask.setPreTaskNo(preTaskNo); |
62 | 62 | boolean success = agvTaskService.updateById(agvTask); |
63 | 63 | if (!success) { |
... | ... | @@ -75,7 +75,7 @@ public class AcsServiceImpl implements IAcsService { |
75 | 75 | String url = addressService.getUrlByParam(QuantityConstant.ADDRESS_AGV_TASK_ASSIGN); |
76 | 76 | String jsonParam = JSON.toJSONString(agvEntity); |
77 | 77 | System.out.println(jsonParam); |
78 | - String body = OkHttpUtils.bodypost(url, jsonParam); | |
78 | + String body = OkHttpUtils.sendPostByJsonStr(url, jsonParam); | |
79 | 79 | if (StringUtils.isEmpty(body)) { |
80 | 80 | throw new ServiceException("接口地址错误或返回为空"); |
81 | 81 | } |
... | ... | @@ -92,7 +92,7 @@ public class AcsServiceImpl implements IAcsService { |
92 | 92 | agvEntity.setTaskNo(agvTask.getId()); |
93 | 93 | String url = addressService.getUrlByParam(QuantityConstant.ADDRESS_AGV_TASK_CANCEL); |
94 | 94 | String jsonParam = JSON.toJSONString(agvEntity); |
95 | - String body = OkHttpUtils.bodypost(url, jsonParam); | |
95 | + String body = OkHttpUtils.sendPostByJsonStr(url, jsonParam); | |
96 | 96 | if (StringUtils.isEmpty(body)) { |
97 | 97 | throw new ServiceException("接口地址错误或返回为空"); |
98 | 98 | } |
... | ... | @@ -110,7 +110,7 @@ public class AcsServiceImpl implements IAcsService { |
110 | 110 | agvEntity.setPriority(agvTask.getPriority()); |
111 | 111 | String url = addressService.getUrlByParam(QuantityConstant.ADDRESS_AGV_TASK_UPDATE); |
112 | 112 | String jsonParam = JSON.toJSONString(agvEntity); |
113 | - String body = OkHttpUtils.bodypost(url, jsonParam); | |
113 | + String body = OkHttpUtils.sendPostByJsonStr(url, jsonParam); | |
114 | 114 | if (StringUtils.isEmpty(body)) { |
115 | 115 | throw new ServiceException("接口地址错误或返回为空"); |
116 | 116 | } |
... | ... |
huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/erp/service/impl/ErpServiceImpl.java
... | ... | @@ -427,7 +427,7 @@ public class ErpServiceImpl implements IErpService { |
427 | 427 | erpBackReceipt.setReceiptDetailList(receiptDetailList); |
428 | 428 | String jsonParam = JSON.toJSONString(erpBackReceipt); |
429 | 429 | System.out.println(jsonParam); |
430 | - String body = OkHttpUtils.bodypost(url, jsonParam); | |
430 | + String body = OkHttpUtils.sendPostByJsonStr(url, jsonParam); | |
431 | 431 | if (StringUtils.isEmpty(body)) { |
432 | 432 | throw new ServiceException("接口地址错误或返回为空"); |
433 | 433 | } |
... | ... | @@ -459,7 +459,7 @@ public class ErpServiceImpl implements IErpService { |
459 | 459 | erpBackShipment.setShipmentDetailList(shipmentDetailList); |
460 | 460 | String jsonParam = JSON.toJSONString(erpBackShipment); |
461 | 461 | System.out.println(jsonParam); |
462 | - String body = OkHttpUtils.bodypost(url, jsonParam); | |
462 | + String body = OkHttpUtils.sendPostByJsonStr(url, jsonParam); | |
463 | 463 | if (StringUtils.isEmpty(body)) { |
464 | 464 | throw new ServiceException("接口地址错误或返回为空"); |
465 | 465 | } |
... | ... |
huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/mes/servuce/impl/MesServiceImpl.java
... | ... | @@ -258,7 +258,7 @@ public class MesServiceImpl implements IMesService { |
258 | 258 | String url = addressService.getUrlByParam(QuantityConstant.ADDRESS_MES_BACK_RECEIPT); |
259 | 259 | String jsonParam = JSON.toJSONString(mesBackReceiptList); |
260 | 260 | System.out.println(jsonParam); |
261 | - String body = OkHttpUtils.bodypost(url, jsonParam); | |
261 | + String body = OkHttpUtils.sendPostByJsonStr(url, jsonParam); | |
262 | 262 | if (StringUtils.isEmpty(body)) { |
263 | 263 | throw new ServiceException("接口地址错误或返回为空"); |
264 | 264 | } |
... | ... | @@ -327,7 +327,7 @@ public class MesServiceImpl implements IMesService { |
327 | 327 | String url = addressService.getUrlByParam(QuantityConstant.ADDRESS_MES_BACK_SHIPMENT); |
328 | 328 | String jsonParam = JSON.toJSONString(mesBackShipmentList); |
329 | 329 | System.out.println(jsonParam); |
330 | - String body = OkHttpUtils.bodypost(url, jsonParam); | |
330 | + String body = OkHttpUtils.sendPostByJsonStr(url, jsonParam); | |
331 | 331 | if (StringUtils.isEmpty(body)) { |
332 | 332 | throw new ServiceException("接口地址错误或返回为空"); |
333 | 333 | } |
... | ... |
huaheng-wms-core/src/main/java/org/jeecg/modules/wms/api/wcs/service/WcsServiceImpl.java
... | ... | @@ -256,7 +256,7 @@ public class WcsServiceImpl implements WcsService { |
256 | 256 | String fromLocationCode = taskHeader.getFromLocationCode(); |
257 | 257 | String toLocationCode = taskHeader.getToLocationCode(); |
258 | 258 | Location fromLocation = locationService.getLocationByCode(fromLocationCode, warehouseCode); |
259 | - Location toLocation = locationService.getLocationByCode(toLocationCode, warehouseCode); | |
259 | +// Location toLocation = locationService.getLocationByCode(toLocationCode, warehouseCode); | |
260 | 260 | boolean direction = true; // true 执行时是入库动作, false 执行时是出库动作 |
261 | 261 | switch (taskType) { |
262 | 262 | case QuantityConstant.TASK_TYPE_SUPPLEMENTRECEIPT: |
... | ... | @@ -378,7 +378,7 @@ public class WcsServiceImpl implements WcsService { |
378 | 378 | wcsTask = switchTaskTypeToWcs(wcsTask); |
379 | 379 | String jsonParam = JSON.toJSONString(wcsTask); |
380 | 380 | System.out.println(jsonParam); |
381 | - String body = OkHttpUtils.bodypost(url, jsonParam); | |
381 | + String body = OkHttpUtils.sendPostByJsonStr(url, jsonParam); | |
382 | 382 | if (StringUtils.isEmpty(body)) { |
383 | 383 | throw new ServiceException("接口地址错误或返回为空"); |
384 | 384 | } |
... | ... |
huaheng-wms-core/src/main/java/org/jeecg/modules/wms/framework/aspectj/ApiAuthenticationAspect.java
0 → 100644
1 | +package org.jeecg.modules.wms.framework.aspectj; | |
2 | + | |
3 | +import java.lang.reflect.Method; | |
4 | + | |
5 | +import javax.servlet.http.HttpServletRequest; | |
6 | + | |
7 | +import org.aspectj.lang.JoinPoint; | |
8 | +import org.aspectj.lang.annotation.Aspect; | |
9 | +import org.aspectj.lang.annotation.Before; | |
10 | +import org.aspectj.lang.annotation.Pointcut; | |
11 | +import org.aspectj.lang.reflect.MethodSignature; | |
12 | +import org.springframework.scheduling.annotation.EnableAsync; | |
13 | +import org.springframework.stereotype.Component; | |
14 | +import org.springframework.web.context.request.RequestContextHolder; | |
15 | +import org.springframework.web.context.request.ServletRequestAttributes; | |
16 | + | |
17 | +import lombok.extern.slf4j.Slf4j; | |
18 | + | |
19 | +/** | |
20 | + * 第三方系统调用接口身份认证Aspect | |
21 | + * @author TanYibin | |
22 | + * @createDate 2023年2月14日 | |
23 | + */ | |
24 | +@Slf4j | |
25 | +@Aspect | |
26 | +@Component | |
27 | +@EnableAsync | |
28 | +public class ApiAuthenticationAspect { | |
29 | + | |
30 | + @Pointcut("execution(* org.jeecg.modules.wms.api..*.*(..)) " | |
31 | + + "&& (@annotation(org.springframework.web.bind.annotation.RequestMapping) " | |
32 | + + "|| @annotation(org.springframework.web.bind.annotation.GetMapping) " | |
33 | + + "|| @annotation(org.springframework.web.bind.annotation.PostMapping))") | |
34 | + public void executeController() {} | |
35 | + | |
36 | + /** | |
37 | + * API Token 验证 | |
38 | + * @author TanYibin | |
39 | + * @createDate 2023年2月14日 | |
40 | + * @param joinPoint | |
41 | + * @throws NoSuchMethodException | |
42 | + * @throws Throwable | |
43 | + */ | |
44 | + @Before("executeController()") | |
45 | + public void doBefore(JoinPoint joinPoint) throws NoSuchMethodException, Throwable { | |
46 | + Method method = this.getTargetMethod(joinPoint); | |
47 | + // 检查是否有passtoken注释,有则跳过认证 | |
48 | + if (method.isAnnotationPresent(PassApiAuthentication.class)) { | |
49 | + PassApiAuthentication passApiAuthentication = method.getAnnotation(PassApiAuthentication.class); | |
50 | + if (passApiAuthentication.required()) { | |
51 | + return; | |
52 | + } | |
53 | + } | |
54 | + // 获取request对象 | |
55 | + ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); | |
56 | + HttpServletRequest request = attributes.getRequest(); | |
57 | + String token = request.getHeader("token"); | |
58 | + log.info("开始校验Token:{}", token); | |
59 | + } | |
60 | + | |
61 | + /** | |
62 | + * 基于连接点信息获取目标方法对象 | |
63 | + * @author TanYibin | |
64 | + * @createDate 2023年2月14日 | |
65 | + * @param joinPoint | |
66 | + * @return | |
67 | + * @throws NoSuchMethodException | |
68 | + */ | |
69 | + private Method getTargetMethod(JoinPoint joinPoint) throws NoSuchMethodException { | |
70 | + // 获取目标类对象 | |
71 | + Class<?> clazz = joinPoint.getTarget().getClass(); | |
72 | + // 获取方法签名信息,方法名和参数列表 | |
73 | + MethodSignature signature = (MethodSignature)joinPoint.getSignature(); | |
74 | + // 获取目标方法对象 | |
75 | + return clazz.getDeclaredMethod(signature.getName(), signature.getParameterTypes()); | |
76 | + } | |
77 | + | |
78 | + /** | |
79 | + * 获取方法类全名+方法名 | |
80 | + * @author TanYibin | |
81 | + * @createDate 2023年2月14日 | |
82 | + * @param method | |
83 | + * @return | |
84 | + */ | |
85 | + private String getClassAndMethodName(Method method) { | |
86 | + // 获取类全名 | |
87 | + String className = method.getDeclaringClass().getName(); | |
88 | + // 获取方法名 | |
89 | + String methodName = method.getName(); | |
90 | + return new StringBuffer(className).append(".").append(methodName).toString(); | |
91 | + } | |
92 | +} | |
... | ... |
huaheng-wms-core/src/main/java/org/jeecg/modules/wms/framework/aspectj/ApiLogAspect.java
1 | 1 | package org.jeecg.modules.wms.framework.aspectj; |
2 | 2 | |
3 | -import com.alibaba.fastjson.JSON; | |
4 | -import com.alibaba.fastjson.JSONObject; | |
5 | -import okhttp3.Request; | |
6 | -import okhttp3.Response; | |
3 | +import java.net.InetAddress; | |
4 | +import java.net.URL; | |
5 | +import java.net.UnknownHostException; | |
6 | +import java.util.ArrayList; | |
7 | +import java.util.Collection; | |
8 | +import java.util.Date; | |
9 | +import java.util.Enumeration; | |
10 | +import java.util.HashMap; | |
11 | +import java.util.Iterator; | |
12 | +import java.util.Set; | |
13 | + | |
14 | +import javax.servlet.http.HttpServletRequest; | |
15 | +import javax.servlet.http.HttpServletResponse; | |
16 | + | |
7 | 17 | import org.apache.commons.lang.exception.ExceptionUtils; |
8 | 18 | import org.aspectj.lang.ProceedingJoinPoint; |
9 | 19 | import org.aspectj.lang.annotation.Around; |
... | ... | @@ -24,21 +34,18 @@ import org.jeecg.utils.constant.QuantityConstant; |
24 | 34 | import org.slf4j.Logger; |
25 | 35 | import org.slf4j.LoggerFactory; |
26 | 36 | import org.springframework.beans.factory.annotation.Autowired; |
27 | -import org.springframework.http.HttpHeaders; | |
28 | 37 | import org.springframework.scheduling.annotation.Async; |
29 | 38 | import org.springframework.scheduling.annotation.EnableAsync; |
30 | 39 | import org.springframework.stereotype.Component; |
31 | -import org.springframework.web.context.request.RequestContextHolder; | |
32 | -import org.springframework.web.context.request.ServletRequestAttributes; | |
33 | 40 | |
34 | -import javax.servlet.http.HttpServletRequest; | |
35 | -import javax.servlet.http.HttpServletResponse; | |
36 | -import java.net.HttpURLConnection; | |
37 | -import java.net.URL; | |
38 | -import java.util.*; | |
41 | +import com.alibaba.fastjson.JSON; | |
42 | +import com.alibaba.fastjson.JSONObject; | |
43 | + | |
44 | +import okhttp3.Request; | |
45 | +import okhttp3.Response; | |
39 | 46 | |
40 | 47 | /** |
41 | - * Api调用日志记录处理 | |
48 | + * API调用日志记录处理 | |
42 | 49 | * @author huaheng |
43 | 50 | */ |
44 | 51 | @Aspect |
... | ... | @@ -65,181 +72,173 @@ public class ApiLogAspect { |
65 | 72 | @Pointcut("@annotation(org.jeecg.modules.wms.framework.aspectj.lang.annotation.ApiLogger)") |
66 | 73 | public void logPointCut() {} |
67 | 74 | |
68 | - @Around("logPointCut() && @annotation(apiLogger)") | |
75 | + @Around("logPointCut() && @annotation(apiLogger)") | |
69 | 76 | public Object around(ProceedingJoinPoint point, ApiLogger apiLogger) throws Throwable { |
70 | - // 实际上静态方法上的Aop注解无法拦截到 | |
71 | - if ("WMS".equalsIgnoreCase(apiLogger.from())) { | |
72 | - return aroundWms2XXX(point, apiLogger); | |
73 | - } else { | |
74 | - return aroundXXX2Wms(point, apiLogger); | |
75 | - } | |
77 | + return aroundXXX2Wms(point, apiLogger); | |
76 | 78 | } |
77 | 79 | |
78 | - /** 处理xxx调用wms接口的日志 **/ | |
80 | + /** 记录第三方系统调用WMS接口的日志 **/ | |
79 | 81 | private Object aroundXXX2Wms(ProceedingJoinPoint point, ApiLogger apiLogger) { |
80 | 82 | Object ret = null; |
81 | - ApiLog log = initApiLog(apiLogger, point); | |
83 | + ApiLog apiLog = initApiLog(apiLogger, point); | |
82 | 84 | try { |
83 | 85 | ret = point.proceed(); |
84 | 86 | } catch (Throwable e) { |
85 | - setApiLogThrowable(log, e); | |
87 | + setApiLogThrowable(apiLog, e); | |
86 | 88 | ret = Result.error(e.getMessage()); |
87 | 89 | return ret; |
88 | 90 | } finally { |
89 | 91 | if (ret != null) { |
90 | - finishApiLog(log, ret); | |
92 | + finishApiLog(apiLog, ret); | |
91 | 93 | } |
92 | 94 | } |
93 | 95 | return ret; |
94 | 96 | } |
95 | 97 | |
96 | - /** 处理WMS调用xxx接口的日志 **/ | |
97 | - private Object aroundWms2XXX(ProceedingJoinPoint point, ApiLogger apiLogger) { | |
98 | - Object ret = null; | |
99 | - ApiLog log = new ApiLog(); | |
100 | - | |
101 | - HttpURLConnection connection = null; | |
102 | - String body = null; | |
103 | - | |
104 | - try { | |
105 | -// HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); | |
106 | - connection = (HttpURLConnection)point.getArgs()[0]; | |
107 | - body = (String)point.getArgs()[1]; | |
108 | - initApiLog(connection, body); | |
109 | - ret = point.proceed(); | |
110 | - } catch (Throwable e) { | |
111 | - setApiLogThrowable(log, e); | |
112 | - ret = Result.error(e.getMessage()); | |
113 | - } finally { | |
114 | - if (ret != null) { | |
115 | - finishApiLog(log, connection, ret.toString()); | |
116 | - } | |
117 | - } | |
118 | - return ret; | |
119 | - } | |
98 | +// /** 记录WMS调用第三方接口的日志 **/ | |
99 | +// private Object aroundWms2XXX(ProceedingJoinPoint point, ApiLogger apiLogger) { | |
100 | +// Object ret = null; | |
101 | +// ApiLog log = new ApiLog(); | |
102 | +// | |
103 | +// HttpURLConnection connection = null; | |
104 | +// String body = null; | |
105 | +// | |
106 | +// try { | |
107 | +// connection = (HttpURLConnection)point.getArgs()[0]; | |
108 | +// body = (String)point.getArgs()[1]; | |
109 | +// initApiLog(connection, body); | |
110 | +// ret = point.proceed(); | |
111 | +// } catch (Throwable e) { | |
112 | +// setApiLogThrowable(log, e); | |
113 | +// ret = Result.error(e.getMessage()); | |
114 | +// } finally { | |
115 | +// if (ret != null) { | |
116 | +// finishApiLog(log, connection, ret.toString()); | |
117 | +// } | |
118 | +// } | |
119 | +// return ret; | |
120 | +// } | |
121 | + | |
122 | +// /** | |
123 | +// * 记录响应头信息,保存日志到数据库 | |
124 | +// */ | |
125 | +// public static void finishApiLog(ApiLog log, HttpURLConnection connection, String body) { | |
126 | +// try { | |
127 | +// log.setResponseBody(body); | |
128 | +// log.setResponseTime(new Date()); | |
129 | +// Long duration = log.getResponseTime().getTime() - log.getRequestTime().getTime(); | |
130 | +// log.setDuration(duration.intValue()); | |
131 | +// log.setHttpCode(connection.getResponseCode()); | |
132 | +// | |
133 | +// // 响应头 | |
134 | +// Set<String> keyset = connection.getHeaderFields().keySet(); | |
135 | +// ArrayList<String> headerList = new ArrayList<>(); | |
136 | +// Iterator<String> it = keyset.iterator(); | |
137 | +// while (it.hasNext()) { | |
138 | +// String name = it.next(); | |
139 | +// String header = connection.getHeaderField(name); | |
140 | +// if (name == null || "".equals(name)) { | |
141 | +// // 第一行没有name | |
142 | +// // HTTP/1.1 200 OK | |
143 | +// headerList.add(header); | |
144 | +// } else { | |
145 | +// headerList.add(name + ": " + header); | |
146 | +// } | |
147 | +// } | |
148 | +// log.setResponseHeader(org.apache.commons.lang3.StringUtils.join(headerList, "\n")); | |
149 | +// Result json = JSON.parseObject(body, Result.class); | |
150 | +// log.setRetCode(json.getCode()); | |
151 | +// } catch (Exception e) { | |
152 | +// e.printStackTrace(); | |
153 | +// } finally { | |
154 | +// SpringUtils.getBean(ApiLogAspect.class).saveApiLog(log); | |
155 | +// } | |
156 | +// } | |
157 | + | |
158 | +// /** | |
159 | +// * 根据url,从address表中判断调用的去向 | |
160 | +// */ | |
161 | +// public static void parseUrl(ApiLog log, URL url, String warehouseCode) { | |
162 | +// try { | |
163 | +// String[] spList = url.toString().split("/"); | |
164 | +// String apiName = spList[spList.length - 1]; | |
165 | +// String ip = JeecgSystemApplication.getLocalHostExactAddress().getHostAddress(); | |
166 | +// Address address = addressService.getAddressByUrl(url.toString(), warehouseCode); | |
167 | +// log.setApiName(apiName); | |
168 | +// log.setRequestFrom("WMS"); | |
169 | +// log.setIp(ip); | |
170 | +// log.setResponseBy(address.getParam().toUpperCase()); | |
171 | +// } catch (Exception e) { | |
172 | +// e.printStackTrace(); | |
173 | +// } | |
174 | +// } | |
175 | + | |
176 | +// /** | |
177 | +// * 记录WMS调用第三方系统接口的请求信息 | |
178 | +// * 在HttpUtils.body POST方法中直接调用本类static方法 | |
179 | +// **/ | |
180 | +// public static ApiLog initApiLog(String Method, String urlStr, String body, HttpHeaders headers, String warehouseCode) { | |
181 | +// ApiLog log = new ApiLog(); | |
182 | +// try { | |
183 | +// URL url = new URL(urlStr); | |
184 | +// log.setApiMethod(Method); | |
185 | +// log.setUrl(urlStr); | |
186 | +// log.setRequestTime(new Date()); | |
187 | +// parseUrl(log, url, warehouseCode); | |
188 | +// | |
189 | +// // 请求头 | |
190 | +// Set<String> keySet = headers.keySet(); | |
191 | +// ArrayList<String> headerList = new ArrayList<>(); | |
192 | +// Iterator<String> it = keySet.iterator(); | |
193 | +// while (it.hasNext()) { | |
194 | +// String name = it.next(); | |
195 | +// String header = String.valueOf(headers.getContentType()); | |
196 | +// headerList.add(name + ": " + header); | |
197 | +// } | |
198 | +// | |
199 | +// log.setRequestHeader(org.apache.commons.lang3.StringUtils.join(headerList, "\n")); | |
200 | +// log.setRequestBody(body); | |
201 | +// } catch (Exception e) { | |
202 | +// e.printStackTrace(); | |
203 | +// } | |
204 | +// | |
205 | +// return log; | |
206 | +// } | |
207 | + | |
208 | +// /** | |
209 | +// * 记录WMS调用第三方系统接口的请求信息 | |
210 | +// * 在HttpUtils.body POST方法中直接调用本类static方法 | |
211 | +// **/ | |
212 | +// public static ApiLog initApiLog(HttpURLConnection connection, String body) { | |
213 | +// ApiLog log = new ApiLog(); | |
214 | +// try { | |
215 | +// log.setApiMethod(connection.getRequestMethod()); | |
216 | +// log.setUrl(connection.getURL().toString()); | |
217 | +// log.setRequestTime(new Date()); | |
218 | +// parseUrl(log, connection.getURL()); | |
219 | +// | |
220 | +// // 请求头 | |
221 | +// Set<String> keySet = connection.getRequestProperties().keySet(); | |
222 | +// ArrayList<String> headerList = new ArrayList<>(); | |
223 | +// Iterator<String> it = keySet.iterator(); | |
224 | +// while (it.hasNext()) { | |
225 | +// String name = it.next(); | |
226 | +// String header = connection.getRequestProperty(name); | |
227 | +// headerList.add(name + ":" + header); | |
228 | +// } | |
229 | +// | |
230 | +// log.setRequestHeader(org.apache.commons.lang3.StringUtils.join(headerList, "\n")); | |
231 | +// log.setRequestBody(body); | |
232 | +// } catch (Exception e) { | |
233 | +// e.printStackTrace(); | |
234 | +// } | |
235 | +// | |
236 | +// return log; | |
237 | +// } | |
120 | 238 | |
121 | 239 | /** |
122 | - * 记录响应头信息 | |
123 | - **/ | |
124 | - public static void finishApiLog(ApiLog log, HttpURLConnection connection, String body) { | |
125 | - try { | |
126 | - log.setResponseBody(body); | |
127 | - log.setResponseTime(new Date()); | |
128 | - Long duration = log.getResponseTime().getTime() - log.getRequestTime().getTime(); | |
129 | - log.setDuration(duration.intValue()); | |
130 | - log.setHttpCode(connection.getResponseCode()); | |
131 | - | |
132 | - // 响应头 | |
133 | - Set<String> keyset = connection.getHeaderFields().keySet(); | |
134 | - ArrayList<String> headerList = new ArrayList<>(); | |
135 | - Iterator<String> it = keyset.iterator(); | |
136 | - while (it.hasNext()) { | |
137 | - String name = it.next(); | |
138 | - String header = connection.getHeaderField(name); | |
139 | - if (name == null || "".equals(name)) { | |
140 | - // 第一行没有name | |
141 | - // HTTP/1.1 200 OK | |
142 | - headerList.add(header); | |
143 | - } else { | |
144 | - headerList.add(name + ": " + header); | |
145 | - } | |
146 | - } | |
147 | - log.setResponseHeader(org.apache.commons.lang3.StringUtils.join(headerList, "\n")); | |
148 | - Result json = JSON.parseObject(body, Result.class); | |
149 | - log.setRetCode(json.getCode()); | |
150 | - } catch (Exception e) { | |
151 | - e.printStackTrace(); | |
152 | - } finally { | |
153 | - SpringUtils.getBean(ApiLogAspect.class).saveApiLog(log); | |
154 | - } | |
155 | - } | |
156 | - | |
157 | - /** | |
158 | - * 根据url,从address表中判断调用的去向 | |
159 | - */ | |
160 | - public static void parseUrl(ApiLog log, URL url, String warehouseCode) { | |
161 | - try { | |
162 | - String[] spList = url.toString().split("/"); | |
163 | - String apiName = spList[spList.length - 1]; | |
164 | -// int index = url.toString().lastIndexOf(apiName); | |
165 | -// String addUrl = url.toString().substring(0, index); | |
166 | - String ip = JeecgSystemApplication.getLocalHostExactAddress().getHostAddress(); | |
167 | - Address address = addressService.getAddressByUrl(url.toString(), warehouseCode); | |
168 | - log.setApiName(apiName); | |
169 | - log.setRequestFrom("WMS"); | |
170 | - log.setIp(ip); | |
171 | - log.setResponseBy(address.getParam().toUpperCase()); | |
172 | - } catch (Exception e) { | |
173 | - e.printStackTrace(); | |
174 | - } | |
175 | - } | |
176 | - | |
177 | - /** | |
178 | - * 记录WMS调用外接口的请求信息 | |
179 | - * 在HttpUtils.body POST方法中直接调用本类static方法 | |
180 | - **/ | |
181 | - public static ApiLog initApiLog(String Method, String urlStr, String body, HttpHeaders headers, String warehouseCode) { | |
182 | - ApiLog log = new ApiLog(); | |
183 | - try { | |
184 | - URL url = new URL(urlStr); | |
185 | - log.setApiMethod(Method); | |
186 | - log.setUrl(urlStr); | |
187 | - log.setRequestTime(new Date()); | |
188 | - parseUrl(log, url, warehouseCode); | |
189 | - | |
190 | - // 请求头 | |
191 | - Set<String> keySet = headers.keySet(); | |
192 | - ArrayList<String> headerList = new ArrayList<>(); | |
193 | - Iterator<String> it = keySet.iterator(); | |
194 | - while (it.hasNext()) { | |
195 | - String name = it.next(); | |
196 | - String header = String.valueOf(headers.getContentType()); | |
197 | - headerList.add(name + ": " + header); | |
198 | - } | |
199 | - | |
200 | - log.setRequestHeader(org.apache.commons.lang3.StringUtils.join(headerList, "\n")); | |
201 | - log.setRequestBody(body); | |
202 | - } catch (Exception e) { | |
203 | - e.printStackTrace(); | |
204 | - } | |
205 | - | |
206 | - return log; | |
207 | - } | |
208 | - | |
209 | - /** | |
210 | - * 记录WMS调用外接口的请求信息 | |
211 | - * 在HttpUtils.body POST方法中直接调用本类static方法 | |
212 | - **/ | |
213 | - public static ApiLog initApiLog(HttpURLConnection connection, String body) { | |
214 | - ApiLog log = new ApiLog(); | |
215 | - try { | |
216 | - log.setApiMethod(connection.getRequestMethod()); | |
217 | - log.setUrl(connection.getURL().toString()); | |
218 | - log.setRequestTime(new Date()); | |
219 | - parseUrl(log, connection.getURL()); | |
220 | - | |
221 | - // 请求头 | |
222 | - Set<String> keySet = connection.getRequestProperties().keySet(); | |
223 | - ArrayList<String> headerList = new ArrayList<>(); | |
224 | - Iterator<String> it = keySet.iterator(); | |
225 | - while (it.hasNext()) { | |
226 | - String name = it.next(); | |
227 | - String header = connection.getRequestProperty(name); | |
228 | - headerList.add(name + ":" + header); | |
229 | - } | |
230 | - | |
231 | - log.setRequestHeader(org.apache.commons.lang3.StringUtils.join(headerList, "\n")); | |
232 | - log.setRequestBody(body); | |
233 | - } catch (Exception e) { | |
234 | - e.printStackTrace(); | |
235 | - } | |
236 | - | |
237 | - return log; | |
238 | - } | |
239 | - | |
240 | - /** | |
241 | - * 记录WMS调用外接口的请求信息 | |
242 | - * 在HttpUtils.bodypost方法中直接调用本类static方法 | |
240 | + * 记录WMS调用第三方系统接口的请求信息 | |
241 | + * 在OKHttpUtils.bodypost方法中直接调用本类static方法 | |
243 | 242 | **/ |
244 | 243 | public static ApiLog initApiLog(Request request, String body) { |
245 | 244 | ApiLog log = new ApiLog(); |
... | ... | @@ -294,8 +293,6 @@ public class ApiLogAspect { |
294 | 293 | try { |
295 | 294 | String[] spList = url.toString().split("/"); |
296 | 295 | String apiName = spList[spList.length - 1]; |
297 | -// int index = url.toString().lastIndexOf(apiName); | |
298 | -// String addUrl = url.toString().substring(0, index); | |
299 | 296 | String ip = JeecgSystemApplication.getLocalHostExactAddress().getHostAddress(); |
300 | 297 | Address address = addressService.getAddressByUrl(url.toString(), QuantityConstant.DEFAULT_WAREHOUSE); |
301 | 298 | log.setApiName(apiName); |
... | ... | @@ -307,40 +304,40 @@ public class ApiLogAspect { |
307 | 304 | } |
308 | 305 | } |
309 | 306 | |
310 | - /** | |
311 | - * 记录响应头信息 | |
312 | - **/ | |
313 | - public static void finishApiLog(ApiLog log, HttpHeaders headers, String body) { | |
314 | - try { | |
315 | - log.setResponseBody(body); | |
316 | - log.setResponseTime(new Date()); | |
317 | - Long duration = log.getResponseTime().getTime() - log.getRequestTime().getTime(); | |
318 | - log.setDuration(duration.intValue()); | |
319 | - log.setHttpCode(200); | |
320 | - | |
321 | - // 响应头 | |
322 | - Set<String> keyset = headers.keySet(); | |
323 | - ArrayList<String> headerList = new ArrayList<>(); | |
324 | - Iterator<String> it = keyset.iterator(); | |
325 | - while (it.hasNext()) { | |
326 | - String name = it.next(); | |
327 | - String header = String.valueOf(headers.getContentType()); | |
328 | - if (name == null || "".equals(name)) | |
329 | - // 第一行没有name | |
330 | - // HTTP/1.1 200 OK | |
331 | - headerList.add(header); | |
332 | - else | |
333 | - headerList.add(name + ": " + header); | |
334 | - } | |
335 | - log.setResponseHeader(org.apache.commons.lang3.StringUtils.join(headerList, "\n")); | |
336 | - Result json = JSON.parseObject(body, Result.class); | |
337 | - log.setRetCode(json.getCode()); | |
338 | - } catch (Exception e) { | |
339 | - e.printStackTrace(); | |
340 | - } finally { | |
341 | - SpringUtils.getBean(ApiLogAspect.class).saveApiLog(log); | |
342 | - } | |
343 | - } | |
307 | +// /** | |
308 | +// * 记录响应头信息 | |
309 | +// **/ | |
310 | +// public static void finishApiLog(ApiLog log, HttpHeaders headers, String body) { | |
311 | +// try { | |
312 | +// log.setResponseBody(body); | |
313 | +// log.setResponseTime(new Date()); | |
314 | +// Long duration = log.getResponseTime().getTime() - log.getRequestTime().getTime(); | |
315 | +// log.setDuration(duration.intValue()); | |
316 | +// log.setHttpCode(200); | |
317 | +// | |
318 | +// // 响应头 | |
319 | +// Set<String> keyset = headers.keySet(); | |
320 | +// ArrayList<String> headerList = new ArrayList<>(); | |
321 | +// Iterator<String> it = keyset.iterator(); | |
322 | +// while (it.hasNext()) { | |
323 | +// String name = it.next(); | |
324 | +// String header = String.valueOf(headers.getContentType()); | |
325 | +// if (name == null || "".equals(name)) | |
326 | +// // 第一行没有name | |
327 | +// // HTTP/1.1 200 OK | |
328 | +// headerList.add(header); | |
329 | +// else | |
330 | +// headerList.add(name + ": " + header); | |
331 | +// } | |
332 | +// log.setResponseHeader(org.apache.commons.lang3.StringUtils.join(headerList, "\n")); | |
333 | +// Result json = JSON.parseObject(body, Result.class); | |
334 | +// log.setRetCode(json.getCode()); | |
335 | +// } catch (Exception e) { | |
336 | +// e.printStackTrace(); | |
337 | +// } finally { | |
338 | +// SpringUtils.getBean(ApiLogAspect.class).saveApiLog(log); | |
339 | +// } | |
340 | +// } | |
344 | 341 | |
345 | 342 | private ApiLog initApiLog(ApiLogger apiLogger, ProceedingJoinPoint point) { |
346 | 343 | ApiLog log = new ApiLog(); |
... | ... | @@ -351,19 +348,16 @@ public class ApiLogAspect { |
351 | 348 | log.setApiName(apiLogger.apiName()); |
352 | 349 | |
353 | 350 | HttpServletRequest request = ServletUtils.getRequest(); |
354 | - | |
355 | 351 | String qryStr = request.getQueryString(); |
356 | 352 | String url = request.getRequestURL().toString(); |
357 | 353 | if (StringUtils.isNotEmpty(qryStr)) { |
358 | 354 | url = url + "?" + qryStr; |
359 | 355 | } |
360 | 356 | log.setUrl(url); |
361 | - | |
362 | 357 | log.setApiMethod(request.getMethod()); |
363 | - log.setIp(request.getRemoteAddr()); | |
358 | + log.setIp(this.getIpAddr(request)); | |
364 | 359 | |
365 | 360 | rebuildRequestHeader(log); |
366 | - | |
367 | 361 | rebuildRequestBody(log, request); |
368 | 362 | |
369 | 363 | // 如果reqeust中取不到post参数,就从接口方法参数中取json对象 |
... | ... | @@ -410,7 +404,7 @@ public class ApiLogAspect { |
410 | 404 | ex.printStackTrace(); |
411 | 405 | } |
412 | 406 | } |
413 | - | |
407 | + | |
414 | 408 | public static void setApiLogThrowable(ApiLog log, Throwable e) { |
415 | 409 | try { |
416 | 410 | String exception = ExceptionUtils.getFullStackTrace(e); |
... | ... | @@ -429,7 +423,7 @@ public class ApiLogAspect { |
429 | 423 | while (names.hasMoreElements()) { |
430 | 424 | String name = (String)names.nextElement(); |
431 | 425 | String header = req.getHeader(name); |
432 | - headerList.add(name + ": " + header); | |
426 | + headerList.add(name + ":" + header); | |
433 | 427 | } |
434 | 428 | String headers = org.apache.commons.lang3.StringUtils.join(headerList, "\n"); |
435 | 429 | log.setRequestHeader(headers); |
... | ... | @@ -527,4 +521,35 @@ public class ApiLogAspect { |
527 | 521 | e.printStackTrace(); |
528 | 522 | } |
529 | 523 | } |
524 | + | |
525 | + public String getIpAddr(HttpServletRequest request) { | |
526 | + String ipAddress = request.getHeader("x-forwarded-for"); | |
527 | + if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { | |
528 | + ipAddress = request.getHeader("Proxy-Client-IP"); | |
529 | + } | |
530 | + if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { | |
531 | + ipAddress = request.getHeader("WL-Proxy-Client-IP"); | |
532 | + } | |
533 | + if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { | |
534 | + ipAddress = request.getRemoteAddr(); | |
535 | + if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) { | |
536 | + // 根据网卡取本机配置的IP | |
537 | + InetAddress inet = null; | |
538 | + try { | |
539 | + inet = InetAddress.getLocalHost(); | |
540 | + } catch (UnknownHostException e) { | |
541 | + e.printStackTrace(); | |
542 | + } | |
543 | + ipAddress = inet.getHostAddress(); | |
544 | + } | |
545 | + } | |
546 | + // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割 | |
547 | + // "***.***.***.***".length() = 15 | |
548 | + if (ipAddress != null && ipAddress.length() > 15) { | |
549 | + if (ipAddress.indexOf(",") > 0) { | |
550 | + ipAddress = ipAddress.substring(0, ipAddress.indexOf(",")); | |
551 | + } | |
552 | + } | |
553 | + return ipAddress; | |
554 | + } | |
530 | 555 | } |
... | ... |
huaheng-wms-core/src/main/java/org/jeecg/modules/wms/framework/aspectj/PassApiAuthentication.java
0 → 100644
1 | +package org.jeecg.modules.wms.framework.aspectj; | |
2 | + | |
3 | +import java.lang.annotation.ElementType; | |
4 | +import java.lang.annotation.Retention; | |
5 | +import java.lang.annotation.RetentionPolicy; | |
6 | +import java.lang.annotation.Target; | |
7 | + | |
8 | +/** | |
9 | + * 跳过API身份验证注解 | |
10 | + * @author TanYibin | |
11 | + * @createDate 2023年2月13日 | |
12 | + */ | |
13 | +@Target({ElementType.METHOD, ElementType.TYPE}) | |
14 | +@Retention(RetentionPolicy.RUNTIME) | |
15 | +public @interface PassApiAuthentication { | |
16 | + boolean required() default true; | |
17 | +} | |
... | ... |
huaheng-wms-core/src/main/java/org/jeecg/modules/wms/framework/controller/TestController.java
... | ... | @@ -43,14 +43,14 @@ public class TestController extends HuahengBaseController { |
43 | 43 | private ISysDataLogService sysDataLogService; |
44 | 44 | |
45 | 45 | |
46 | - @ApiLogger(apiName = "API接口第三方Token校验测试", from = "TEST") | |
47 | - @ResponseBody | |
48 | - @PostMapping(value = "/api/wms/testTokenCheck") | |
49 | - public Result<?> testTokenCheck(@RequestBody Map<String, String> paramMap, HttpServletRequest request) { | |
50 | - | |
51 | - | |
52 | - return new Result<>(); | |
53 | - } | |
46 | +// @ApiLogger(apiName = "API接口第三方Token校验测试", from = "TEST") | |
47 | +// @ResponseBody | |
48 | +// @PostMapping(value = "/api/wms/testTokenCheck") | |
49 | +// public Result<?> testTokenCheck(@RequestBody Map<String, String> paramMap, HttpServletRequest request) { | |
50 | +// | |
51 | +// | |
52 | +// return new Result<>(); | |
53 | +// } | |
54 | 54 | |
55 | 55 | |
56 | 56 | @AutoLog(value = "TestController-testRedis") |
... | ... |
huaheng-wms-core/src/main/java/org/jeecg/utils/http/HttpUtils.java deleted
1 | -package org.jeecg.utils.http; | |
2 | - | |
3 | -import org.apache.commons.io.IOUtils; | |
4 | -import org.apache.http.HttpResponse; | |
5 | -import org.apache.http.HttpStatus; | |
6 | -import org.apache.http.client.methods.HttpPost; | |
7 | -import org.apache.http.entity.StringEntity; | |
8 | -import org.apache.http.impl.client.CloseableHttpClient; | |
9 | -import org.apache.http.impl.client.HttpClients; | |
10 | -import org.jeecg.modules.wms.framework.aspectj.ApiLogAspect; | |
11 | -import org.jeecg.modules.wms.monitor.apiLog.entity.ApiLog; | |
12 | -import org.slf4j.Logger; | |
13 | -import org.slf4j.LoggerFactory; | |
14 | - | |
15 | -import javax.net.ssl.*; | |
16 | -import java.io.*; | |
17 | -import java.net.*; | |
18 | -import java.security.cert.X509Certificate; | |
19 | - | |
20 | -/** | |
21 | - * 通用http发送方法 | |
22 | - * @author huaheng | |
23 | - */ | |
24 | -public class HttpUtils { | |
25 | - | |
26 | - private static final Logger log = LoggerFactory.getLogger(HttpUtils.class); | |
27 | - | |
28 | - /** | |
29 | - * 向指定 URL 发送GET方法的请求 | |
30 | - * @param url 发送请求的 URL | |
31 | - * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 | |
32 | - * @return 所代表远程资源的响应结果 | |
33 | - */ | |
34 | - public static String sendGet(String url, String param) { | |
35 | - StringBuilder result = new StringBuilder(); | |
36 | - BufferedReader in = null; | |
37 | - try { | |
38 | - String urlNameString = url + "?" + param; | |
39 | - log.info("sendGet - {}", urlNameString); | |
40 | - URL realUrl = new URL(urlNameString); | |
41 | - URLConnection connection = realUrl.openConnection(); | |
42 | - connection.setRequestProperty("accept", "*/*"); | |
43 | - connection.setRequestProperty("connection", "Keep-Alive"); | |
44 | - connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); | |
45 | - connection.connect(); | |
46 | - in = new BufferedReader(new InputStreamReader(connection.getInputStream())); | |
47 | - String line; | |
48 | - while ((line = in.readLine()) != null) { | |
49 | - result.append(line); | |
50 | - } | |
51 | - log.info("recv - {}", result); | |
52 | - } catch (ConnectException e) { | |
53 | - log.error("调用HttpUtils.sendGet ConnectException, url=" + url + ",param=" + param, e.getMessage()); | |
54 | - } catch (SocketTimeoutException e) { | |
55 | - log.error("调用HttpUtils.sendGet SocketTimeoutException, url=" + url + ",param=" + param, e.getMessage()); | |
56 | - } catch (IOException e) { | |
57 | - log.error("调用HttpUtils.sendGet IOException, url=" + url + ",param=" + param, e.getMessage()); | |
58 | - } catch (Exception e) { | |
59 | - log.error("调用HttpsUtil.sendGet Exception, url=" + url + ",param=" + param, e.getMessage()); | |
60 | - } finally { | |
61 | - try { | |
62 | - if (in != null) { | |
63 | - in.close(); | |
64 | - } | |
65 | - } catch (Exception ex) { | |
66 | - log.error("调用in.close Exception, url=" + url + ",param=" + param, ex.getMessage()); | |
67 | - } | |
68 | - } | |
69 | - return result.toString(); | |
70 | - } | |
71 | - | |
72 | - // post请求 为erp回传定制 | |
73 | - public static String erppost(String strURL, String params, String key) { | |
74 | - HttpURLConnection connection = null; | |
75 | - InputStream is = null; | |
76 | - OutputStreamWriter out = null; | |
77 | - String result = null; | |
78 | - ApiLog log = null; | |
79 | - | |
80 | - try { | |
81 | - URL url = new URL(strURL);// 创建连接 | |
82 | -// String ur = "" + url; | |
83 | - connection = (HttpURLConnection)url.openConnection(); | |
84 | - connection.setDoOutput(true); | |
85 | - connection.setDoInput(true); | |
86 | - connection.setUseCaches(false); | |
87 | - connection.setInstanceFollowRedirects(true); | |
88 | - connection.setRequestMethod("POST");// 设置请求方式 | |
89 | - connection.setRequestProperty("Accept", "application/json");// 设置接收数据的格式 | |
90 | - connection.setRequestProperty("Content-Type", "application/json");// 设置发送数据的格式 | |
91 | - connection.setRequestProperty("apiKey", key);// 测试环境key | |
92 | - log = ApiLogAspect.initApiLog(connection, params); | |
93 | - connection.connect(); | |
94 | - out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");// utf-8编码 | |
95 | - out.append(params); | |
96 | - out.flush(); | |
97 | - out.close(); // 读取响应 | |
98 | - int length = (int)connection.getContentLength();// 获取长度 | |
99 | - is = connection.getInputStream(); | |
100 | -// BufferedReader br = new BufferedReader(new InputStreamReader(is)); | |
101 | - length = 10000; | |
102 | - if (length > 0) { | |
103 | - byte[] data = new byte[length]; | |
104 | - byte[] temp = new byte[512]; | |
105 | - int readLen = 0; | |
106 | - int destPos = 0; | |
107 | - while ((readLen = is.read(temp)) > 0) { | |
108 | - System.arraycopy(temp, 0, data, destPos, readLen); | |
109 | - destPos += readLen; | |
110 | - } | |
111 | - result = new String(data, "UTF-8"); | |
112 | - System.out.println(result); | |
113 | - } | |
114 | - } catch (Exception e) { | |
115 | - ApiLogAspect.setApiLogException(log, e); | |
116 | - e.printStackTrace(); | |
117 | - } finally { | |
118 | - ApiLogAspect.finishApiLog(log, connection, result); | |
119 | - if (connection != null) | |
120 | - connection.disconnect(); | |
121 | - IOUtils.closeQuietly(out, is); | |
122 | - } | |
123 | - return result; | |
124 | - } | |
125 | - | |
126 | - /** | |
127 | - * 向指定 URL 发送POST方法的请求 | |
128 | - * @param url 发送请求的 URL | |
129 | - * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 | |
130 | - * @return 所代表远程资源的响应结果 | |
131 | - */ | |
132 | - public static String sendPost(String url, String param) { | |
133 | - PrintWriter out = null; | |
134 | - BufferedReader in = null; | |
135 | - StringBuilder result = new StringBuilder(); | |
136 | - try { | |
137 | - String urlNameString = url + "?" + param; | |
138 | - log.info("sendPost - {}", urlNameString); | |
139 | - URL realUrl = new URL(urlNameString); | |
140 | - URLConnection conn = realUrl.openConnection(); | |
141 | - conn.setRequestProperty("accept", "*/*"); | |
142 | - conn.setRequestProperty("connection", "Keep-Alive"); | |
143 | - conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); | |
144 | - conn.setRequestProperty("Accept-Charset", "utf-8"); | |
145 | - conn.setRequestProperty("contentType", "utf-8"); | |
146 | - conn.setDoOutput(true); | |
147 | - conn.setDoInput(true); | |
148 | - out = new PrintWriter(conn.getOutputStream()); | |
149 | - out.print(param); | |
150 | - out.flush(); | |
151 | - if (((HttpURLConnection)conn).getResponseCode() >= 390) { | |
152 | - in = new BufferedReader(new InputStreamReader(((HttpURLConnection)conn).getErrorStream())); | |
153 | - } else { | |
154 | - in = new BufferedReader(new InputStreamReader(conn.getInputStream())); | |
155 | - } | |
156 | -// in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); | |
157 | - String line; | |
158 | - while ((line = in.readLine()) != null) { | |
159 | - result.append(line); | |
160 | - } | |
161 | - log.info("recv - {}", result); | |
162 | - } catch (ConnectException e) { | |
163 | - log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e.getMessage()); | |
164 | - } catch (SocketTimeoutException e) { | |
165 | - log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e.getMessage()); | |
166 | - } catch (IOException e) { | |
167 | - log.error("调用HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e.getMessage()); | |
168 | - } catch (Exception e) { | |
169 | - log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e.getMessage()); | |
170 | - } finally { | |
171 | - try { | |
172 | - if (out != null) { | |
173 | - out.close(); | |
174 | - } | |
175 | - if (in != null) { | |
176 | - in.close(); | |
177 | - } | |
178 | - } catch (IOException ex) { | |
179 | - log.error("调用in.close Exception, url=" + url + ",param=" + param, ex.getMessage()); | |
180 | - } | |
181 | - } | |
182 | - return result.toString(); | |
183 | - } | |
184 | - | |
185 | - public static String sendSSLPost(String url, String param) { | |
186 | - StringBuilder result = new StringBuilder(); | |
187 | - String urlNameString = url + "?" + param; | |
188 | - try { | |
189 | - log.info("sendSSLPost - {}", urlNameString); | |
190 | - SSLContext sc = SSLContext.getInstance("SSL"); | |
191 | - sc.init(null, new TrustManager[] {new TrustAnyTrustManager()}, new java.security.SecureRandom()); | |
192 | - URL console = new URL(urlNameString); | |
193 | - HttpsURLConnection conn = (HttpsURLConnection)console.openConnection(); | |
194 | - conn.setRequestProperty("accept", "*/*"); | |
195 | - conn.setRequestProperty("connection", "Keep-Alive"); | |
196 | - conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); | |
197 | - conn.setRequestProperty("Accept-Charset", "utf-8"); | |
198 | - conn.setRequestProperty("contentType", "utf-8"); | |
199 | - conn.setDoOutput(true); | |
200 | - conn.setDoInput(true); | |
201 | - | |
202 | - conn.setSSLSocketFactory(sc.getSocketFactory()); | |
203 | - conn.setHostnameVerifier(new TrustAnyHostnameVerifier()); | |
204 | - conn.connect(); | |
205 | - InputStream is = conn.getInputStream(); | |
206 | - BufferedReader br = new BufferedReader(new InputStreamReader(is)); | |
207 | - String ret = ""; | |
208 | - while ((ret = br.readLine()) != null) { | |
209 | - if (ret != null && !"".equals(ret.trim())) { | |
210 | - result.append(new String(ret.getBytes("ISO-8859-1"), "utf-8")); | |
211 | - } | |
212 | - } | |
213 | - log.info("recv - {}", result); | |
214 | - conn.disconnect(); | |
215 | - br.close(); | |
216 | - } catch (ConnectException e) { | |
217 | - log.error("调用HttpUtils.sendSSLPost ConnectException, url=" + url + ",param=" + param, e.getMessage()); | |
218 | - } catch (SocketTimeoutException e) { | |
219 | - log.error("调用HttpUtils.sendSSLPost SocketTimeoutException, url=" + url + ",param=" + param, e.getMessage()); | |
220 | - } catch (IOException e) { | |
221 | - log.error("调用HttpUtils.sendSSLPost IOException, url=" + url + ",param=" + param, e.getMessage()); | |
222 | - } catch (Exception e) { | |
223 | - log.error("调用HttpsUtil.sendSSLPost Exception, url=" + url + ",param=" + param, e.getMessage()); | |
224 | - } | |
225 | - return result.toString(); | |
226 | - } | |
227 | - | |
228 | - // 此方法是将参数以body形式发送post请求 | |
229 | - public static String bodypost(String strURL, String params) { | |
230 | - System.out.println(strURL); | |
231 | - System.out.println(params); | |
232 | - HttpURLConnection connection = null; | |
233 | - InputStream is = null; | |
234 | - OutputStreamWriter out = null; | |
235 | - String result = null; | |
236 | - ApiLog log = null; | |
237 | - | |
238 | - try { | |
239 | - URL url = new URL(strURL);// 创建连接 | |
240 | - String stringUrl = "" + url; | |
241 | - connection = (HttpURLConnection)url.openConnection(); | |
242 | - connection.setDoOutput(true); | |
243 | - connection.setDoInput(true); | |
244 | - connection.setUseCaches(false); | |
245 | - connection.setInstanceFollowRedirects(true); | |
246 | - connection.setRequestMethod("POST");// 设置请求方式 | |
247 | - connection.setRequestProperty("Accept", "application/json");// 设置接收数据的格式 | |
248 | - connection.setRequestProperty("Content-Type", "application/json");// 设置发送数据的格式 | |
249 | - if ((stringUrl.contains("10.0.15.19"))) { | |
250 | - connection.setRequestProperty("apiKey", "c687ef505557428595e6a596fba5de6c");// 测试环境key | |
251 | - } | |
252 | - log = ApiLogAspect.initApiLog(connection, params); | |
253 | - connection.connect(); | |
254 | - out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");// utf-8编码 | |
255 | - out.append(params); | |
256 | - out.flush(); | |
257 | - out.close(); // 读取响应 | |
258 | - int length = (int)connection.getContentLength();// 获取长度 | |
259 | - is = connection.getInputStream(); | |
260 | -// BufferedReader br = new BufferedReader(new InputStreamReader(is)); | |
261 | - length = 10000; | |
262 | - if (length > 0) { | |
263 | - byte[] data = new byte[length]; | |
264 | - byte[] temp = new byte[512]; | |
265 | - int readLen = 0; | |
266 | - int destPos = 0; | |
267 | - while ((readLen = is.read(temp)) > 0) { | |
268 | - System.arraycopy(temp, 0, data, destPos, readLen); | |
269 | - destPos += readLen; | |
270 | - } | |
271 | - result = new String(data, "UTF-8"); | |
272 | - System.out.println(result); | |
273 | - } | |
274 | - } catch (Exception e) { | |
275 | - ApiLogAspect.setApiLogException(log, e); | |
276 | - e.printStackTrace(); | |
277 | - } finally { | |
278 | - ApiLogAspect.finishApiLog(log, connection, result); | |
279 | - if (connection != null) | |
280 | - connection.disconnect(); | |
281 | - IOUtils.closeQuietly(out, is); | |
282 | - } | |
283 | - return result; | |
284 | - } | |
285 | - | |
286 | - /** | |
287 | - * 发送以json为参数的POST请求 | |
288 | - * @param json | |
289 | - * @param url | |
290 | - * @return | |
291 | - */ | |
292 | - public static String sendJsonPostToken(String json, String url) { | |
293 | - String result = ""; | |
294 | - HttpPost post = new HttpPost(url); | |
295 | - try { | |
296 | - CloseableHttpClient httpClient = HttpClients.createDefault(); | |
297 | - | |
298 | - post.setHeader("Content-Type", "application/json;charset=utf-8"); | |
299 | - post.addHeader("Authorization", "Basic YWRtaW46"); | |
300 | - StringEntity postingString = new StringEntity(json, "utf-8"); | |
301 | - post.setEntity(postingString); | |
302 | - HttpResponse response = httpClient.execute(post); | |
303 | - | |
304 | - InputStream in = response.getEntity().getContent(); | |
305 | - BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8")); | |
306 | - StringBuilder strber = new StringBuilder(); | |
307 | - String line = null; | |
308 | - while ((line = br.readLine()) != null) { | |
309 | - strber.append(line + '\n'); | |
310 | - } | |
311 | - br.close(); | |
312 | - in.close(); | |
313 | - result = strber.toString(); | |
314 | - if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { | |
315 | - result = "服务器异常"; | |
316 | - } | |
317 | - } catch (Exception e) { | |
318 | - System.out.println("请求异常"); | |
319 | - throw new RuntimeException(e); | |
320 | - } finally { | |
321 | - post.abort(); | |
322 | - } | |
323 | - return result; | |
324 | - } | |
325 | - | |
326 | - private static class TrustAnyTrustManager implements X509TrustManager { | |
327 | - @Override | |
328 | - public void checkClientTrusted(X509Certificate[] chain, String authType) {} | |
329 | - | |
330 | - @Override | |
331 | - public void checkServerTrusted(X509Certificate[] chain, String authType) {} | |
332 | - | |
333 | - @Override | |
334 | - public X509Certificate[] getAcceptedIssuers() { | |
335 | - return new X509Certificate[] {}; | |
336 | - } | |
337 | - } | |
338 | - | |
339 | - private static class TrustAnyHostnameVerifier implements HostnameVerifier { | |
340 | - @Override | |
341 | - public boolean verify(String hostname, SSLSession session) { | |
342 | - return true; | |
343 | - } | |
344 | - } | |
345 | - | |
346 | -} |
huaheng-wms-core/src/main/java/org/jeecg/utils/http/OkHttpUtils.java
... | ... | @@ -3,6 +3,8 @@ package org.jeecg.utils.http; |
3 | 3 | import com.alibaba.fastjson.JSON; |
4 | 4 | import com.alibaba.fastjson.TypeReference; |
5 | 5 | import com.alibaba.fastjson.parser.Feature; |
6 | + | |
7 | +import net.bytebuddy.asm.Advice.This; | |
6 | 8 | import okhttp3.*; |
7 | 9 | import org.jeecg.modules.wms.framework.aspectj.ApiLogAspect; |
8 | 10 | import org.jeecg.modules.wms.monitor.apiLog.entity.ApiLog; |
... | ... | @@ -76,54 +78,68 @@ public class OkHttpUtils { |
76 | 78 | * @return 所代表远程资源的响应结果 |
77 | 79 | */ |
78 | 80 | public static String sendGet(String url, String param) { |
81 | + ApiLog apiLog = null; | |
79 | 82 | // headers 请求头 |
80 | 83 | Map<String, String> headers = new HashMap<>(); |
81 | 84 | // 请求URI |
82 | 85 | String urlNameString = url + "?" + param; |
83 | - | |
84 | 86 | Request.Builder builder = new Request.Builder(); |
85 | 87 | buildHeader(builder, headers); |
86 | 88 | |
87 | 89 | Request request = builder.url(urlNameString).get().build(); |
88 | 90 | Response response = null; |
91 | + String result = null; | |
89 | 92 | try { |
93 | + apiLog = ApiLogAspect.initApiLog(request, param); | |
90 | 94 | response = HTTP_CLIENT.newCall(request).execute(); |
91 | 95 | if (response.isSuccessful() && Objects.nonNull(response.body())) { |
92 | - String result = response.body().string(); | |
93 | - log.info("执行GET请求,url:{},header:{},param:{} 成功,返回数据:{}", url, JSON.toJSONString(headers), param, result); | |
94 | - return result; | |
96 | + result = response.body().string(); | |
97 | + log.info("执行GET请求,url:{},header:{},param:{} 成功,返回结果:{}", url, JSON.toJSONString(headers), param, result); | |
95 | 98 | } |
96 | 99 | } catch (IOException e) { |
97 | 100 | log.error("执行GET请求,url:{},header:{},param:{} 失败!", url, JSON.toJSONString(headers), param, e); |
101 | + ApiLogAspect.setApiLogException(apiLog, e); | |
102 | + } finally { | |
103 | + ApiLogAspect.finishApiLog(apiLog, response, result); | |
98 | 104 | } |
99 | - return ""; | |
105 | + return result; | |
100 | 106 | } |
101 | 107 | |
102 | 108 | /** |
103 | 109 | * 向指定 URL 发送POST方法的请求 |
104 | - * @param url 发送请求的 URL // * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 | |
105 | - * @return 所代表远程资源的响应结果 | |
110 | + * @param url 发送请求的 URL | |
111 | + * @param param 请求参数,请求参数是 name1=value1&name2=value2 的形式。 | |
112 | + * @return 远程资源的响应结果 | |
113 | + * @throws IOException | |
106 | 114 | */ |
107 | 115 | public static String sendPost(String url, String param) { |
116 | + ApiLog apiLog = null; | |
108 | 117 | FormBody.Builder builder = new FormBody.Builder(); |
109 | 118 | String urlNameString = url + "?" + param; |
110 | 119 | FormBody body = builder.build(); |
111 | 120 | Request request = new Request.Builder().url(urlNameString).post(body).build(); |
112 | 121 | Response response = null; |
122 | + String result = null; | |
113 | 123 | try { |
124 | + apiLog = ApiLogAspect.initApiLog(request, param); | |
114 | 125 | response = HTTP_CLIENT.newCall(request).execute(); |
115 | 126 | // 调用成功 |
116 | 127 | if (response.isSuccessful() && response.body() != null) { |
117 | - return response.body().string(); | |
128 | + result = response.body().string(); | |
129 | + log.info("执行GET请求,url:{},param:{} 成功,返回结果:{}", url, param, result); | |
118 | 130 | } |
119 | 131 | } catch (IOException e) { |
120 | - e.printStackTrace(); | |
132 | + log.error("执行GET请求,url:{},param:{} 失败!", url, param, e); | |
133 | + ApiLogAspect.setApiLogException(apiLog, e); | |
134 | + } finally { | |
135 | + ApiLogAspect.finishApiLog(apiLog, response, result); | |
121 | 136 | } |
122 | - return ""; | |
137 | + return result; | |
123 | 138 | } |
124 | 139 | |
125 | - // 此方法是将参数以body形式发送post请求 | |
126 | - public static String bodypost(String url, String json) { | |
140 | + /** JSONString形式发送POST请求 | |
141 | + * @throws IOException */ | |
142 | + public static String sendPostByJsonStr(String url, String json) { | |
127 | 143 | ApiLog apiLog = null; |
128 | 144 | // using above json body as a input to post API call |
129 | 145 | RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, json); |
... | ... | @@ -144,8 +160,8 @@ public class OkHttpUtils { |
144 | 160 | log.info("执行POST请求,url:{},header:{},param:{} 成功,返回结果:{}", url, JSON.toJSONString(headers), json, result); |
145 | 161 | } |
146 | 162 | } catch (IOException e) { |
147 | - ApiLogAspect.setApiLogException(apiLog, e); | |
148 | 163 | log.error("执行POST请求,url:{},header:{},param:{} 失败!", url, JSON.toJSONString(headers), json, e); |
164 | + ApiLogAspect.setApiLogException(apiLog, e); | |
149 | 165 | } finally { |
150 | 166 | ApiLogAspect.finishApiLog(apiLog, response, result); |
151 | 167 | } |
... | ... | @@ -168,19 +184,15 @@ public class OkHttpUtils { |
168 | 184 | } |
169 | 185 | |
170 | 186 | /** |
171 | - * 支持嵌套泛型的post请求。 | |
172 | - * | |
173 | - * <pre> | |
174 | - * Type type = new TypeToken<Results<User>>() {}.getType(); | |
175 | - * | |
176 | - * <pre/> | |
187 | + * 支持嵌套泛型的POST请求 | |
177 | 188 | * @param url 链接 |
178 | 189 | * @param json 请求json |
179 | 190 | * @param type 嵌套泛型 |
180 | 191 | * @return 响应对象, 可进行强转。 |
192 | + * @throws IOException | |
181 | 193 | */ |
182 | - public static <T> T post(String url, String json, TypeReference<T> type) { | |
183 | - String result = bodypost(url, json); | |
194 | + public static <T> T sendPostByJsonStr(String url, String jsonString, TypeReference<T> type) throws IOException { | |
195 | + String result = sendPostByJsonStr(url, jsonString); | |
184 | 196 | if (Objects.nonNull(result) && Objects.nonNull(type)) { |
185 | 197 | return JSON.parseObject(result, type); |
186 | 198 | } |
... | ... |