From 0195e7ec548b7e8d11d809c88a5e727ea08657f7 Mon Sep 17 00:00:00 2001 From: wxz Date: Wed, 28 Oct 2020 11:17:21 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=86=85=E5=A4=96?= =?UTF-8?q?=E9=83=A8=E8=AE=A4=E8=AF=81=EF=BC=8C=E6=94=AF=E6=8C=81=E5=90=8C?= =?UTF-8?q?=E6=97=B6=E4=BD=BF=E7=94=A8=E5=86=85=E5=A4=96=E9=83=A8=E8=AE=A4?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/epmet/auth/AuthProcessor.java | 10 +--------- .../com/epmet/auth/ExternalAuthProcessor.java | 10 ++++++---- .../com/epmet/auth/InternalAuthProcessor.java | 15 +++++++++------ .../com/epmet/constant/AuthTypeConstant.java | 1 + .../epmet/filter/CpAuthGatewayFilterFactory.java | 16 +++++++++++++--- 5 files changed, 30 insertions(+), 22 deletions(-) diff --git a/epmet-gateway/src/main/java/com/epmet/auth/AuthProcessor.java b/epmet-gateway/src/main/java/com/epmet/auth/AuthProcessor.java index 4ef47a2eae..e5a2027e56 100644 --- a/epmet-gateway/src/main/java/com/epmet/auth/AuthProcessor.java +++ b/epmet-gateway/src/main/java/com/epmet/auth/AuthProcessor.java @@ -13,14 +13,6 @@ import java.nio.charset.StandardCharsets; public abstract class AuthProcessor { - abstract Mono auth(ServerWebExchange exchange, GatewayFilterChain chain); - - protected Mono response(ServerWebExchange exchange, Object object) { - String json = JSON.toJSONString(object); - DataBuffer buffer = exchange.getResponse().bufferFactory().wrap(json.getBytes(StandardCharsets.UTF_8)); - exchange.getResponse().getHeaders().setContentType(MediaType.APPLICATION_JSON_UTF8); - exchange.getResponse().setStatusCode(HttpStatus.OK); - return exchange.getResponse().writeWith(Flux.just(buffer)); - } + abstract ServerWebExchange auth(ServerWebExchange exchange, GatewayFilterChain chain); } diff --git a/epmet-gateway/src/main/java/com/epmet/auth/ExternalAuthProcessor.java b/epmet-gateway/src/main/java/com/epmet/auth/ExternalAuthProcessor.java index baf5151766..18854a8776 100644 --- a/epmet-gateway/src/main/java/com/epmet/auth/ExternalAuthProcessor.java +++ b/epmet-gateway/src/main/java/com/epmet/auth/ExternalAuthProcessor.java @@ -50,7 +50,7 @@ public class ExternalAuthProcessor extends AuthProcessor { private CpProperty cpProperty; @Override - public Mono auth(ServerWebExchange exchange, GatewayFilterChain chain) { + public ServerWebExchange auth(ServerWebExchange exchange, GatewayFilterChain chain) { ServerHttpRequest request = exchange.getRequest(); // 只有在外部应用urls中的url才会允许外部应用访问,否则不允许访问 @@ -92,12 +92,14 @@ public class ExternalAuthProcessor extends AuthProcessor { throw new RenException(EpmetErrorCode.OPER_EXTERNAL_APP_AUTH_ERROR.getCode(), "未知的外部认证类型"); } } catch (RenException e) { - return response(exchange, new Result<>().error(e.getCode(), e.getMsg())); + //return response(exchange, new Result<>().error(e.getCode(), e.getMsg())); + throw new RenException(e.getCode(),e.getMsg()); } catch (Exception e) { logger.error("外部应用请求认证发生未知错误:" + ExceptionUtils.getErrorStackTrace(e)); - return response(exchange, new Result<>().error("外部应用请求认证发生未知错误")); + //return response(exchange, new Result<>().error("外部应用请求认证发生未知错误")); + throw new RenException("外部应用请求认证发生未知错误"); } - return chain.filter(exchange); + return exchange; } } diff --git a/epmet-gateway/src/main/java/com/epmet/auth/InternalAuthProcessor.java b/epmet-gateway/src/main/java/com/epmet/auth/InternalAuthProcessor.java index c87f9dbe6b..6690bb2374 100644 --- a/epmet-gateway/src/main/java/com/epmet/auth/InternalAuthProcessor.java +++ b/epmet-gateway/src/main/java/com/epmet/auth/InternalAuthProcessor.java @@ -44,7 +44,7 @@ public class InternalAuthProcessor extends AuthProcessor { private CpProperty cpProperty; @Override - public Mono auth(ServerWebExchange exchange, GatewayFilterChain chain) { + public ServerWebExchange auth(ServerWebExchange exchange, GatewayFilterChain chain) { ServerHttpRequest request = exchange.getRequest(); String requestUri = request.getPath().pathWithinApplication().value(); @@ -56,7 +56,8 @@ public class InternalAuthProcessor extends AuthProcessor { try{ baseTokenDto = getBaseTokenDto(token, jwtTokenUtils); }catch(RenException e){ - return response(exchange,new Result<>().error(e.getCode(),e.getMsg())); + //return response(exchange,new Result<>().error(e.getCode(),e.getMsg())); + throw new RenException(e.getCode(), e.getMsg()); } }else{ baseTokenDto = null; @@ -92,12 +93,14 @@ public class InternalAuthProcessor extends AuthProcessor { if (needAuth(requestUri)) { // 校验token if (StringUtils.isBlank(token)) { - return response(exchange, new Result<>().error(EpmetErrorCode.ERR10005.getCode(), EpmetErrorCode.ERR10005.getMsg())); + //return response(exchange, new Result<>().error(EpmetErrorCode.ERR10005.getCode(), EpmetErrorCode.ERR10005.getMsg())); + throw new RenException(EpmetErrorCode.ERR10005.getCode(), EpmetErrorCode.ERR10005.getMsg()); } try { validateTokenDto(baseTokenDto, token); } catch (RenException e) { - return response(exchange, new Result<>().error(e.getCode(), e.getMsg())); + //return response(exchange, new Result<>().error(e.getCode(), e.getMsg())); + throw new RenException(e.getCode(), e.getMsg()); } } @@ -119,10 +122,10 @@ public class InternalAuthProcessor extends AuthProcessor { exchange.getRequest().mutate().header(AppClientConstant.CUSTOMER_ID, customerId); } ServerHttpRequest build = exchange.getRequest().mutate().build(); - return chain.filter(exchange.mutate().request(build).build()); + return exchange.mutate().request(build).build(); } - return chain.filter(exchange); + return exchange; } /** diff --git a/epmet-gateway/src/main/java/com/epmet/constant/AuthTypeConstant.java b/epmet-gateway/src/main/java/com/epmet/constant/AuthTypeConstant.java index d2123966f6..5b75030ae7 100644 --- a/epmet-gateway/src/main/java/com/epmet/constant/AuthTypeConstant.java +++ b/epmet-gateway/src/main/java/com/epmet/constant/AuthTypeConstant.java @@ -1,6 +1,7 @@ package com.epmet.constant; public class AuthTypeConstant { + public static final String AUTH_TYPE_ALL = "all"; public static final String AUTH_TYPE_INTERNAL = "internal"; public static final String AUTH_TYPE_EXTERNAL = "external"; public static final String AUTH_TYPE_NO_NEED = "no_need"; diff --git a/epmet-gateway/src/main/java/com/epmet/filter/CpAuthGatewayFilterFactory.java b/epmet-gateway/src/main/java/com/epmet/filter/CpAuthGatewayFilterFactory.java index 971f4938b1..9051c001d8 100644 --- a/epmet-gateway/src/main/java/com/epmet/filter/CpAuthGatewayFilterFactory.java +++ b/epmet-gateway/src/main/java/com/epmet/filter/CpAuthGatewayFilterFactory.java @@ -74,10 +74,13 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory().error(e.getCode(), e.getMessage())); @@ -105,7 +108,14 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory Date: Wed, 28 Oct 2020 14:04:16 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../epmet/dto/form/AddToTemplateFormDTO.java | 16 ++++ .../epmet/dto/form/DeleteTemplateFormDTO.java | 16 ++++ .../result/TemplateDraftListResultDTO.java | 34 ++++++++ .../com/epmet/controller/CodeController.java | 39 +++++++++ .../java/com/epmet/service/CodeService.java | 27 ++++++ .../epmet/service/impl/CodeServiceImpl.java | 71 ++++++++++++++++ .../wxapi/constant/WxMaCodeConstant.java | 15 ++++ .../epmet/wxapi/param/WxAddToTemplateReq.java | 18 ++++ .../wxapi/param/WxDeleteTemplateReq.java | 18 ++++ .../result/WxTemplateDraftListResult.java | 51 ++++++++++++ .../epmet/wxapi/service/WxMaCodeService.java | 29 +++++++ .../service/impl/WxMaCodeServiceImpl.java | 83 +++++++++++++++++++ 12 files changed, 417 insertions(+) create mode 100644 epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/AddToTemplateFormDTO.java create mode 100644 epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/DeleteTemplateFormDTO.java create mode 100644 epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/result/TemplateDraftListResultDTO.java create mode 100644 epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/param/WxAddToTemplateReq.java create mode 100644 epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/param/WxDeleteTemplateReq.java create mode 100644 epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/result/WxTemplateDraftListResult.java diff --git a/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/AddToTemplateFormDTO.java b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/AddToTemplateFormDTO.java new file mode 100644 index 0000000000..10de3b3d28 --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/AddToTemplateFormDTO.java @@ -0,0 +1,16 @@ +package com.epmet.dto.form; + +import lombok.Data; + +import java.io.Serializable; + +/** + * @author zhaoqifeng + * @dscription + * @date 2020/10/28 9:55 + */ +@Data +public class AddToTemplateFormDTO implements Serializable { + private static final long serialVersionUID = -7759275805971446460L; + private String draftId; +} diff --git a/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/DeleteTemplateFormDTO.java b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/DeleteTemplateFormDTO.java new file mode 100644 index 0000000000..843d17a0eb --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/DeleteTemplateFormDTO.java @@ -0,0 +1,16 @@ +package com.epmet.dto.form; + +import lombok.Data; + +import java.io.Serializable; + +/** + * @author zhaoqifeng + * @dscription + * @date 2020/10/28 9:57 + */ +@Data +public class DeleteTemplateFormDTO implements Serializable { + private static final long serialVersionUID = -6516706760708801090L; + private String templateId; +} diff --git a/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/result/TemplateDraftListResultDTO.java b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/result/TemplateDraftListResultDTO.java new file mode 100644 index 0000000000..f5908d773d --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/result/TemplateDraftListResultDTO.java @@ -0,0 +1,34 @@ +package com.epmet.dto.result; + +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +/** + * @author zhaoqifeng + * @dscription + * @date 2020/10/28 9:52 + */ +@NoArgsConstructor +@Data +public class TemplateDraftListResultDTO implements Serializable { + + private static final long serialVersionUID = 8249448254837721428L; + /** + * 开发者上传草稿时间戳 + */ + private String createTime; + /** + * 版本号,开发者自定义字段 + */ + private String userVersion; + /** + * 版本描述 开发者自定义字段 + */ + private String userDesc; + /** + * 草稿 id + */ + private String draftId; +} diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/controller/CodeController.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/controller/CodeController.java index cdaa2a6742..c7457e048f 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/controller/CodeController.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/controller/CodeController.java @@ -251,5 +251,44 @@ public class CodeController { return new Result<>(); } + /** + * 获取代码草稿列表 + * @author zhaoqifeng + * @date 2020/10/28 9:59 + * @param + * @return com.epmet.commons.tools.utils.Result> + */ + @PostMapping("gettemplatedraftlist") + public Result> getTemplateDraftList() { + List list = codeService.getTemplateDraftList(); + return new Result>().ok(list); + } + + /** + * 将草稿添加到代码模板库 + * @author zhaoqifeng + * @date 2020/10/28 10:00 + * @param formDTO + * @return com.epmet.commons.tools.utils.Result + */ + @PostMapping("addtotemplate") + public Result addToTemplate(@RequestBody AddToTemplateFormDTO formDTO) { + codeService.addToTemplate(formDTO); + return new Result<>(); + } + + /** + * 删除指定代码模板 + * @author zhaoqifeng + * @date 2020/10/28 10:03 + * @param formDTO + * @return com.epmet.commons.tools.utils.Result + */ + @PostMapping("deletetemplate") + public Result deleteTemplate(@RequestBody DeleteTemplateFormDTO formDTO) { + codeService.deleteTemplate(formDTO); + return new Result<>(); + } + } diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/CodeService.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/CodeService.java index 38e409d79b..32fce29360 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/CodeService.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/CodeService.java @@ -217,4 +217,31 @@ public interface CodeService { */ void revertCodeRelease(CodeCommonFormDTO formDTO); + /** + * 获取代码草稿列表 + * @author zhaoqifeng + * @date 2020/10/28 10:02 + * @param + * @return java.util.List + */ + List getTemplateDraftList(); + + /** + * 将草稿添加到代码模板库 + * @author zhaoqifeng + * @date 2020/10/28 10:02 + * @param formDTO + * @return void + */ + void addToTemplate(AddToTemplateFormDTO formDTO); + + /** + * 删除指定代码模板 + * @author zhaoqifeng + * @date 2020/10/28 10:03 + * @param formDTO + * @return void + */ + void deleteTemplate(DeleteTemplateFormDTO formDTO); + } diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/CodeServiceImpl.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/CodeServiceImpl.java index c8f51e8e40..7e1208dcc4 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/CodeServiceImpl.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/CodeServiceImpl.java @@ -741,6 +741,77 @@ public class CodeServiceImpl implements CodeService { codeCustomerService.deleteById(codeCustomerDTO.getId()); } + /** + * 获取代码草稿列表 + * + * @return java.util.List + * @author zhaoqifeng + * @date 2020/10/28 10:02 + */ + @Override + public List getTemplateDraftList() { + List resultList = new ArrayList<>(); + //获取COMPONENT_ACCESS_TOKEN + String accessToken = componentAccessTokenDao.getComponentAccessToken(); + //调用微信API获取模板列表 + WxResult wxResult = wxMaCodeService.getTemplateDraftList(accessToken); + if (!wxResult.success()) { + throw new RenException(wxResult.getErrorCode(), wxResult.getErrorMsg()); + } + if (null == wxResult.getData() || wxResult.getData().getDraftList().size() == NumConstant.ZERO) { + return resultList; + } + wxResult.getData().getDraftList().forEach(temp -> { + TemplateDraftListResultDTO dto = new TemplateDraftListResultDTO(); + dto.setDraftId(temp.getDraftId()); + dto.setUserVersion(temp.getUserVersion()); + dto.setUserDesc(temp.getUserDesc()); + dto.setCreateTime(DateUtils.formatTimestamp(temp.getCreateTime(), DateUtils.DATE_TIME_PATTERN)); + resultList.add(dto); + }); + return resultList; + } + + /** + * 将草稿添加到代码模板库 + * + * @param formDTO + * @return void + * @author zhaoqifeng + * @date 2020/10/28 10:02 + */ + @Override + public void addToTemplate(AddToTemplateFormDTO formDTO) { + //获取COMPONENT_ACCESS_TOKEN + String accessToken = componentAccessTokenDao.getComponentAccessToken(); + WxAddToTemplateReq request = new WxAddToTemplateReq(); + request.setDraftId(formDTO.getDraftId()); + WxResult result = wxMaCodeService.addToTemplate(accessToken, request); + if (!result.success()) { + throw new RenException(result.getErrorCode(), result.getErrorMsg()); + } + } + + /** + * 删除指定代码模板 + * + * @param formDTO + * @return void + * @author zhaoqifeng + * @date 2020/10/28 10:03 + */ + @Override + public void deleteTemplate(DeleteTemplateFormDTO formDTO) { + //获取COMPONENT_ACCESS_TOKEN + String accessToken = componentAccessTokenDao.getComponentAccessToken(); + WxDeleteTemplateReq request = new WxDeleteTemplateReq(); + request.setTemplateId(formDTO.getTemplateId()); + WxResult result = wxMaCodeService.deleteTemplate(accessToken, request); + if (!result.success()) { + throw new RenException(result.getErrorCode(), result.getErrorMsg()); + } + } + private void saveOperation(String customerId, String clientType, String codeId, String version, String operation, String describe) { CodeOperationHistoryDTO operationDTO = new CodeOperationHistoryDTO(); operationDTO.setCustomerId(customerId); diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/constant/WxMaCodeConstant.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/constant/WxMaCodeConstant.java index c0d11dca5b..7d1263e3b3 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/constant/WxMaCodeConstant.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/constant/WxMaCodeConstant.java @@ -185,4 +185,19 @@ public interface WxMaCodeConstant { * 加急审核申请 */ String SPEED_UP_AUDIT_URL = "https://api.weixin.qq.com/wxa/speedupaudit"; + + /** + * 获取代码草稿列表 + */ + String GET_TEMPLATE_DRAFT_LIST_URL = "https://api.weixin.qq.com/wxa/gettemplatedraftlist"; + + /** + * 将草稿添加到代码模板库 + */ + String ADD_TO_TEMPLATE_URL = "https://api.weixin.qq.com/wxa/addtotemplate"; + + /** + * 获取代码草稿列表 + */ + String DELETE_TEMPLATE_URL = "https://api.weixin.qq.com/wxa/deletetemplate"; } diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/param/WxAddToTemplateReq.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/param/WxAddToTemplateReq.java new file mode 100644 index 0000000000..997f959342 --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/param/WxAddToTemplateReq.java @@ -0,0 +1,18 @@ +package com.epmet.wxapi.param; + +import com.google.gson.annotations.SerializedName; +import lombok.Data; + +import java.io.Serializable; + +/** + * @author zhaoqifeng + * @dscription + * @date 2020/10/28 10:20 + */ +@Data +public class WxAddToTemplateReq implements Serializable { + private static final long serialVersionUID = -4667328954236033227L; + @SerializedName("draft_id") + private String draftId; +} diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/param/WxDeleteTemplateReq.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/param/WxDeleteTemplateReq.java new file mode 100644 index 0000000000..1a70b18e9e --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/param/WxDeleteTemplateReq.java @@ -0,0 +1,18 @@ +package com.epmet.wxapi.param; + +import com.google.gson.annotations.SerializedName; +import lombok.Data; + +import java.io.Serializable; + +/** + * @author zhaoqifeng + * @dscription + * @date 2020/10/28 10:21 + */ +@Data +public class WxDeleteTemplateReq implements Serializable { + private static final long serialVersionUID = 3099244896895201612L; + @SerializedName("template_id") + private String templateId; +} diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/result/WxTemplateDraftListResult.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/result/WxTemplateDraftListResult.java new file mode 100644 index 0000000000..e77437af53 --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/result/WxTemplateDraftListResult.java @@ -0,0 +1,51 @@ +package com.epmet.wxapi.result; + +import com.google.gson.annotations.SerializedName; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.util.List; + +/** + * @author zhaoqifeng + * @dscription + * @date 2020/10/28 10:12 + */ + +@NoArgsConstructor +@Data +public class WxTemplateDraftListResult implements Serializable { + private static final long serialVersionUID = -7316579770340890368L; + /** + * 错误码 + */ + private Integer errcode; + /** + * 错误信息 + */ + private String errmsg; + @SerializedName("draft_list") + private List draftList; + + public boolean success(){ + return errcode == 0; + } + + @NoArgsConstructor + @Data + public static class DraftListBean { + /** + * create_time + */ + @SerializedName("create_time") + private Long createTime; + @SerializedName("user_version") + private String userVersion; + @SerializedName("user_desc") + private String userDesc; + @SerializedName("draft_id") + private String draftId; + } + +} diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/service/WxMaCodeService.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/service/WxMaCodeService.java index 1918e79603..452ad866e1 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/service/WxMaCodeService.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/service/WxMaCodeService.java @@ -202,4 +202,33 @@ public interface WxMaCodeService { * @return com.epmet.wxapi.result.WxResult */ WxResult revertCodeRelease(String accessToken); + + /** + * 获取代码草稿列表 + * @author zhaoqifeng + * @date 2020/10/28 10:18 + * @param accessToken + * @return com.epmet.wxapi.result.WxResult + */ + WxResult getTemplateDraftList(String accessToken); + + /** + * 将草稿添加到代码模板库 + * @author zhaoqifeng + * @date 2020/10/28 10:23 + * @param accessToken + * @param request + * @return com.epmet.wxapi.result.WxResult + */ + WxResult addToTemplate(String accessToken, WxAddToTemplateReq request); + + /** + * 删除指定代码模板 + * @author zhaoqifeng + * @date 2020/10/28 10:23 + * @param accessToken + * @param request + * @return com.epmet.wxapi.result.WxResult + */ + WxResult deleteTemplate(String accessToken, WxDeleteTemplateReq request); } diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/service/impl/WxMaCodeServiceImpl.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/service/impl/WxMaCodeServiceImpl.java index 1ce1c55b68..8c859debec 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/service/impl/WxMaCodeServiceImpl.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/service/impl/WxMaCodeServiceImpl.java @@ -387,6 +387,89 @@ public class WxMaCodeServiceImpl implements WxMaCodeService { return result; } + /** + * 获取代码草稿列表 + * + * @param accessToken + * @return com.epmet.wxapi.result.WxResult + * @author zhaoqifeng + * @date 2020/10/28 10:18 + */ + @Override + public WxResult getTemplateDraftList(String accessToken) { + WxResult result = new WxResult<>(); + String url = WxMaCodeConstant.GET_TEMPLATE_DRAFT_LIST_URL + "?" + "access_token=" + accessToken; + Result templateListResult = HttpClientManager.getInstance().sendGet(url, null); + if (!templateListResult.success()) { + result.setErrorCode(templateListResult.getCode()); + result.setErrorMsg(templateListResult.getMsg()); + return result; + } + Gson gson = new Gson(); + WxTemplateDraftListResult templateList = gson.fromJson(templateListResult.getData(), WxTemplateDraftListResult.class); + result.setErrorCode(templateList.getErrcode()); + result.setErrorMsg(WxMaErrorMsgEnum.findMsgByCode(templateList.getErrcode())); + result.setData(templateList); + WxTemplateDraftListResult draftListResult = JSONObject.parseObject(templateListResult.getData(), WxTemplateDraftListResult.class); + if (!draftListResult.success()) { + result.setErrorCode(draftListResult.getErrcode()); + result.setErrorMsg(WxMaErrorMsgEnum.findMsgByCode(draftListResult.getErrcode())); + return result; + } + result.ok(draftListResult); + return result; + } + + /** + * 将草稿添加到代码模板库 + * + * @param accessToken + * @param request + * @return com.epmet.wxapi.result.WxResult + * @author zhaoqifeng + * @date 2020/10/28 10:23 + */ + @Override + public WxResult addToTemplate(String accessToken, WxAddToTemplateReq request) { + WxResult result = new WxResult(); + String url = WxMaCodeConstant.ADD_TO_TEMPLATE_URL + "?" + "access_token=" + accessToken; + Result submitResult = HttpClientManager.getInstance().sendPostByJSON(url, toJson(request)); + if (!submitResult.success()) { + result.setErrorCode(submitResult.getCode()); + result.setErrorMsg(submitResult.getMsg()); + return result; + } + JSONObject jsonObject = JSONObject.parseObject(submitResult.getData()); + result.setErrorCode(jsonObject.getInteger(ERR_CODE)); + result.setErrorMsg(WxMaErrorMsgEnum.findMsgByCode(jsonObject.getInteger(ERR_CODE))); + return result; + } + + /** + * 删除指定代码模板 + * + * @param accessToken + * @param request + * @return com.epmet.wxapi.result.WxResult + * @author zhaoqifeng + * @date 2020/10/28 10:23 + */ + @Override + public WxResult deleteTemplate(String accessToken, WxDeleteTemplateReq request) { + WxResult result = new WxResult(); + String url = WxMaCodeConstant.DELETE_TEMPLATE_URL + "?" + "access_token=" + accessToken; + Result submitResult = HttpClientManager.getInstance().sendPostByJSON(url, toJson(request)); + if (!submitResult.success()) { + result.setErrorCode(submitResult.getCode()); + result.setErrorMsg(submitResult.getMsg()); + return result; + } + JSONObject jsonObject = JSONObject.parseObject(submitResult.getData()); + result.setErrorCode(jsonObject.getInteger(ERR_CODE)); + result.setErrorMsg(WxMaErrorMsgEnum.findMsgByCode(jsonObject.getInteger(ERR_CODE))); + return result; + } + private String toJson(Object object) { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.setPrettyPrinting(); From e0e61800c80cb44e758b74d4b7f0b81e13faf56e Mon Sep 17 00:00:00 2001 From: jianjun Date: Wed, 28 Oct 2020 15:38:08 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AFIp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tools/aspect/BaseRequestLogAspect.java | 1 - .../epmet/commons/tools/utils/IpUtils.java | 73 ++++++++++++++++--- .../com/epmet/auth/InternalAuthProcessor.java | 3 - .../filter/CpAuthGatewayFilterFactory.java | 6 +- 4 files changed, 64 insertions(+), 19 deletions(-) diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aspect/BaseRequestLogAspect.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aspect/BaseRequestLogAspect.java index 3e7af3ced6..b8a38908a0 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aspect/BaseRequestLogAspect.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aspect/BaseRequestLogAspect.java @@ -12,7 +12,6 @@ import org.springframework.dao.DuplicateKeyException; import javax.servlet.http.HttpServletRequest; import java.time.Duration; import java.time.LocalDateTime; -import java.util.UUID; /** * 日志切面 diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/IpUtils.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/IpUtils.java index b100daac7a..d051717d8d 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/IpUtils.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/IpUtils.java @@ -8,12 +8,17 @@ package com.epmet.commons.tools.utils; +import com.epmet.commons.tools.constant.StrConstant; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.http.HttpHeaders; +import org.springframework.http.server.reactive.ServerHttpRequest; import javax.servlet.http.HttpServletRequest; import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.Optional; import java.util.regex.Pattern; /** @@ -24,43 +29,87 @@ import java.util.regex.Pattern; */ public class IpUtils { private static Logger logger = LoggerFactory.getLogger(IpUtils.class); - public static final String LOCALHOST = "127.0.0.1"; + private static final String LOCALHOST = "127.0.0.1"; - public static final String ANYHOST = "0.0.0.0"; + private static final String ANYHOST = "0.0.0.0"; + + private static final String IP_UNKNOWN = "unknown"; private static final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$"); public static String getIpAddr(HttpServletRequest request) { + String ip = null; try { ip = request.getHeader("x-forwarded-for"); - logger.debug("x-forwarded-for:"+ip); - if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { + logger.debug("x-forwarded-for:" + ip); + if (StringUtils.isEmpty(ip) || IP_UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } - if (StringUtils.isEmpty(ip) || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + if (StringUtils.isEmpty(ip) || ip.length() == 0 || IP_UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } - if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { + if (StringUtils.isEmpty(ip) || IP_UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_CLIENT_IP"); } - if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { + if (StringUtils.isEmpty(ip) || IP_UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); } - if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { + if (StringUtils.isEmpty(ip) || IP_UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getHeader("X-Real-IP"); } - if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) { + if (StringUtils.isEmpty(ip) || IP_UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } //对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割 if (ip != null) { //"***.***.***.***".length() = 15 - if (ip.indexOf(",") > 0) { - ip = ip.substring(0, ip.indexOf(",")); + if (ip.indexOf(StrConstant.COMMA) > 0) { + ip = ip.split(StrConstant.COMMA)[0]; + ; + } + } + } catch (Exception e) { + logger.error("IpUtils getIpAddr ERROR ", e); + } + + return ip; + } + + public static String getClientIp(ServerHttpRequest request) { + String ip = null; + try { + HttpHeaders headers = request.getHeaders(); + String ipAddress = headers.getFirst("x-forwarded-for"); + if (ipAddress == null || ipAddress.length() == 0 || IP_UNKNOWN.equalsIgnoreCase(ipAddress)) { + ipAddress = headers.getFirst("Proxy-Client-IP"); + } + if (ipAddress == null || ipAddress.length() == 0 || IP_UNKNOWN.equalsIgnoreCase(ipAddress)) { + ipAddress = headers.getFirst("WL-Proxy-Client-IP"); + } + if (ipAddress == null || ipAddress.length() == 0 || IP_UNKNOWN.equalsIgnoreCase(ipAddress)) { + ipAddress = Optional.ofNullable(request.getRemoteAddress()) + .map(address -> address.getAddress().getHostAddress()) + .orElse(""); + if (LOCALHOST.equals(ipAddress)) { + // 根据网卡取本机配置的IP + try { + InetAddress inet = InetAddress.getLocalHost(); + ipAddress = inet.getHostAddress(); + } catch (UnknownHostException e) { + // ignore + } + } + } + + // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割 + if (ip != null) { //"***.***.***.***".length() = 15 + if (ip.indexOf(StrConstant.COMMA) > 0) { + ip = ip.substring(0, ip.indexOf(StrConstant.COMMA)); } } + return ipAddress; } catch (Exception e) { - logger.error("IpUtils ERROR ", e); + logger.error("IpUtils getIpAddr ERROR ", e); } return ip; diff --git a/epmet-gateway/src/main/java/com/epmet/auth/InternalAuthProcessor.java b/epmet-gateway/src/main/java/com/epmet/auth/InternalAuthProcessor.java index 6690bb2374..43895ce9af 100644 --- a/epmet-gateway/src/main/java/com/epmet/auth/InternalAuthProcessor.java +++ b/epmet-gateway/src/main/java/com/epmet/auth/InternalAuthProcessor.java @@ -8,7 +8,6 @@ import com.epmet.commons.tools.security.dto.BaseTokenDto; import com.epmet.commons.tools.security.dto.GovTokenDto; import com.epmet.commons.tools.security.dto.TokenDto; import com.epmet.commons.tools.utils.CpUserDetailRedis; -import com.epmet.commons.tools.utils.Result; import com.epmet.filter.CpProperty; import com.epmet.jwt.JwtTokenUtils; import io.jsonwebtoken.Claims; @@ -22,7 +21,6 @@ import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.stereotype.Component; import org.springframework.util.AntPathMatcher; import org.springframework.web.server.ServerWebExchange; -import reactor.core.publisher.Mono; /** * 内部认证处理器 @@ -48,7 +46,6 @@ public class InternalAuthProcessor extends AuthProcessor { ServerHttpRequest request = exchange.getRequest(); String requestUri = request.getPath().pathWithinApplication().value(); - logger.info("CpAuthGatewayFilterFactory当前requestUri=[" + requestUri + "]CpAuthGatewayFilterFactory拦截成功"); String token = getTokenFromRequest(request); //BaseTokenDto baseTokenDto = StringUtils.isNotBlank(token) ? getBaseTokenDto(token, jwtTokenUtils) : null; BaseTokenDto baseTokenDto; diff --git a/epmet-gateway/src/main/java/com/epmet/filter/CpAuthGatewayFilterFactory.java b/epmet-gateway/src/main/java/com/epmet/filter/CpAuthGatewayFilterFactory.java index 9051c001d8..72139e59e1 100644 --- a/epmet-gateway/src/main/java/com/epmet/filter/CpAuthGatewayFilterFactory.java +++ b/epmet-gateway/src/main/java/com/epmet/filter/CpAuthGatewayFilterFactory.java @@ -5,9 +5,8 @@ import com.alibaba.fastjson.JSON; import com.epmet.auth.ExternalAuthProcessor; import com.epmet.auth.InternalAuthProcessor; import com.epmet.commons.tools.constant.AppClientConstant; -import com.epmet.commons.tools.constant.Constant; -import com.epmet.commons.tools.exception.EpmetErrorCode; import com.epmet.commons.tools.exception.RenException; +import com.epmet.commons.tools.utils.IpUtils; import com.epmet.commons.tools.utils.Result; import com.epmet.constant.AuthTypeConstant; import com.epmet.constant.TokenHeaderKeyConstant; @@ -23,7 +22,6 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.stereotype.Component; -import org.springframework.util.AntPathMatcher; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -71,7 +69,9 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory