From e24e1216f185bc6df66eac882a352843dddd22bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=98=95?= Date: Mon, 30 Oct 2023 19:14:47 +0800 Subject: [PATCH] =?UTF-8?q?OCT=201.=20SpringBoot=20=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=88=87=E6=8D=A2=E5=88=B0=20SpringBoot2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 13 +- .../java/com/cmhi/gds/common/ErrorCode.java | 35 ++- .../com/cmhi/gds/common/GlobalConfigure.java | 11 + .../GameDatabaseInfoController.java | 79 ++++++- .../gds/exception/GlobalExceptionHandler.java | 23 +- .../interceptor/RequestBodyCacheWrapper.java | 106 +++++++++ .../gds/interceptor/RequestBodyFilter.java | 52 +++++ .../interceptor/RequestProtocolSecurity.java | 104 +++++++++ .../interceptor/ResponseProtocolSecurity.java | 83 +++++++ .../manager/GameDatabaseServiceManager.java | 5 +- .../impl/GameDatabaseServiceManagerImpl.java | 10 +- .../com/cmhi/gds/mapper/GameTableMapper.java | 6 +- .../dto/protocol/base/BaseProtocolDTO.java | 1 - .../dto/protocol/info/GameInfoContent.java | 12 +- .../dto/protocol/info/GameInfoDetail.java | 1 + .../protocol/info/GetGameInfoDetailReq.java | 23 ++ ...erverReq.java => ReportGameServerReq.java} | 6 +- .../com/cmhi/gds/pojo/entry/GameInfo.java | 2 - .../com/cmhi/gds/pojo/entry/GameServer.java | 3 +- .../com/cmhi/gds/pojo/entry/GameTable.java | 2 - .../java/com/cmhi/gds/pojo/vo/GameDetail.java | 22 ++ .../gds/pojo/{entry => vo}/GameSummary.java | 2 +- .../gds/pojo/vo/GameTableItemsDetail.java | 24 ++ ...eItems.java => GameTableItemsSummary.java} | 2 +- .../pojo/{entry => vo}/GameTableSummary.java | 3 +- .../service/GameDatabaseServiceService.java | 7 +- .../gds/service/ProtocolSecurityService.java | 51 ++++ .../impl/GameDatabaseServiceServiceImpl.java | 20 +- .../impl/ProtocolSecurityServiceImpl.java | 221 ++++++++++++++++++ .../valids/impl/ValidBaseEnumImpl.java | 3 +- src/main/resources/mappers/GameTable.xml | 28 ++- 31 files changed, 889 insertions(+), 71 deletions(-) create mode 100644 src/main/java/com/cmhi/gds/common/GlobalConfigure.java create mode 100644 src/main/java/com/cmhi/gds/interceptor/RequestBodyCacheWrapper.java create mode 100644 src/main/java/com/cmhi/gds/interceptor/RequestBodyFilter.java create mode 100644 src/main/java/com/cmhi/gds/interceptor/RequestProtocolSecurity.java create mode 100644 src/main/java/com/cmhi/gds/interceptor/ResponseProtocolSecurity.java create mode 100644 src/main/java/com/cmhi/gds/pojo/dto/protocol/info/GetGameInfoDetailReq.java rename src/main/java/com/cmhi/gds/pojo/dto/protocol/info/{GameServerReq.java => ReportGameServerReq.java} (74%) create mode 100644 src/main/java/com/cmhi/gds/pojo/vo/GameDetail.java rename src/main/java/com/cmhi/gds/pojo/{entry => vo}/GameSummary.java (88%) create mode 100644 src/main/java/com/cmhi/gds/pojo/vo/GameTableItemsDetail.java rename src/main/java/com/cmhi/gds/pojo/vo/{GameTableItems.java => GameTableItemsSummary.java} (91%) rename src/main/java/com/cmhi/gds/pojo/{entry => vo}/GameTableSummary.java (80%) create mode 100644 src/main/java/com/cmhi/gds/service/ProtocolSecurityService.java create mode 100644 src/main/java/com/cmhi/gds/service/impl/ProtocolSecurityServiceImpl.java diff --git a/pom.xml b/pom.xml index f7d4a06..558441a 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 3.1.5 + 2.7.17 com.cmhi @@ -14,7 +14,7 @@ game_database_service game_database_service - 21 + 1.8 @@ -64,7 +64,7 @@ org.mybatis mybatis-spring - 3.0.2 + 2.1.1 javax.servlet @@ -130,13 +130,18 @@ org.mybatis.spring.boot mybatis-spring-boot-starter - 3.0.2 + 2.3.1 org.yaml snakeyaml 2.1 + + io.projectreactor + reactor-core + 3.4.16 + diff --git a/src/main/java/com/cmhi/gds/common/ErrorCode.java b/src/main/java/com/cmhi/gds/common/ErrorCode.java index 80e3b9e..fcb1417 100644 --- a/src/main/java/com/cmhi/gds/common/ErrorCode.java +++ b/src/main/java/com/cmhi/gds/common/ErrorCode.java @@ -334,16 +334,31 @@ public enum ErrorCode implements BaseEnum { * @return the http code */ public int getHttpCode() { - return switch (this) { - case ERR_OK -> HttpServletResponse.SC_OK; - case ERR_SYSTEMEXCEPTION, ERR_PARAMEXCEPTION -> HttpServletResponse.SC_EXPECTATION_FAILED; - case ERR_TOKENTIMEOUT, ERR_REQTIMEOUT -> HttpServletResponse.SC_REQUEST_TIMEOUT; - case ERR_UNTRUSTTOKEN, ERR_UNTRUSTHOST, ERR_LOGOUT -> HttpServletResponse.SC_UNAUTHORIZED; - case ERR_MISSAUTHHEAD, ERR_PARAMS, ERR_INPUTFORMAT, ERR_INPUTMISS -> HttpServletResponse.SC_BAD_REQUEST; - case ERR_UNSUPPORT -> HttpServletResponse.SC_METHOD_NOT_ALLOWED; - case ERR_UNKNOWNINTERFACE -> HttpServletResponse.SC_NOT_FOUND; - default -> HttpServletResponse.SC_INTERNAL_SERVER_ERROR; - }; + switch (this) { + case ERR_OK: + return HttpServletResponse.SC_OK; + case ERR_SYSTEMEXCEPTION: + case ERR_PARAMEXCEPTION: + return HttpServletResponse.SC_EXPECTATION_FAILED; + case ERR_TOKENTIMEOUT: + case ERR_REQTIMEOUT: + return HttpServletResponse.SC_REQUEST_TIMEOUT; + case ERR_UNTRUSTTOKEN: + case ERR_UNTRUSTHOST: + case ERR_LOGOUT: + return HttpServletResponse.SC_UNAUTHORIZED; + case ERR_MISSAUTHHEAD: + case ERR_PARAMS: + case ERR_INPUTFORMAT: + case ERR_INPUTMISS: + return HttpServletResponse.SC_BAD_REQUEST; + case ERR_UNSUPPORT: + return HttpServletResponse.SC_METHOD_NOT_ALLOWED; + case ERR_UNKNOWNINTERFACE: + return HttpServletResponse.SC_NOT_FOUND; + default: + return HttpServletResponse.SC_INTERNAL_SERVER_ERROR; + } } /** diff --git a/src/main/java/com/cmhi/gds/common/GlobalConfigure.java b/src/main/java/com/cmhi/gds/common/GlobalConfigure.java new file mode 100644 index 0000000..2f3a4ec --- /dev/null +++ b/src/main/java/com/cmhi/gds/common/GlobalConfigure.java @@ -0,0 +1,11 @@ +package com.cmhi.gds.common; + +public class GlobalConfigure { + private GlobalConfigure() { + throw new AssertionError("Instantiating utility class."); + } + + public static volatile boolean CHECK_PROTO_REQUEST_TIMEOUT = false; + + public static volatile long REQUEST_TIMEOUT_MS = 5 * 1000; +} diff --git a/src/main/java/com/cmhi/gds/controller/GameDatabaseInfoController.java b/src/main/java/com/cmhi/gds/controller/GameDatabaseInfoController.java index 1b76714..076c5f1 100644 --- a/src/main/java/com/cmhi/gds/controller/GameDatabaseInfoController.java +++ b/src/main/java/com/cmhi/gds/controller/GameDatabaseInfoController.java @@ -3,16 +3,20 @@ package com.cmhi.gds.controller; import com.cmhi.gds.common.ErrorCode; import com.cmhi.gds.pojo.dto.protocol.base.ProtocolReqDTO; import com.cmhi.gds.pojo.dto.protocol.base.ProtocolRespDTO; +import com.cmhi.gds.pojo.dto.protocol.info.GameInfoContent; +import com.cmhi.gds.pojo.dto.protocol.info.GameInfoDetail; import com.cmhi.gds.pojo.dto.protocol.info.GameListSummary; -import com.cmhi.gds.pojo.dto.protocol.info.GameServerReq; import com.cmhi.gds.pojo.dto.protocol.info.GameServerRsp; +import com.cmhi.gds.pojo.dto.protocol.info.GetGameInfoDetailReq; +import com.cmhi.gds.pojo.dto.protocol.info.GetGamesInfoRsp; import com.cmhi.gds.pojo.dto.protocol.info.GetGamesSummaryRsp; -import com.cmhi.gds.pojo.vo.GameTableItems; +import com.cmhi.gds.pojo.dto.protocol.info.ReportGameServerReq; +import com.cmhi.gds.pojo.vo.GameTableItemsDetail; +import com.cmhi.gds.pojo.vo.GameTableItemsSummary; import com.cmhi.gds.security.annotation.Decryption; import com.cmhi.gds.security.annotation.Encryption; import com.cmhi.gds.service.GameDatabaseServiceService; import com.cmhi.gds.validation.group.ValidGroups; -import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; @@ -23,8 +27,12 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; +import javax.annotation.Resource; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; @Controller @RequestMapping(value = "/gameinfo") @@ -40,17 +48,16 @@ public class GameDatabaseInfoController { @GetMapping("gamelistsummary") @ResponseBody - public ProtocolRespDTO getGameListSummary() { GetGamesSummaryRsp rspInfo = GetGamesSummaryRsp.builder() .items(new ArrayList<>()) .build(); - List gameList = gameDatabaseServiceService.getGameTableSummaryItems(); + List gameList = gameDatabaseServiceService.getGameTableSummaryItems(); if (!gameList.isEmpty()) { - for (GameTableItems v : gameList) { + for (GameTableItemsSummary v : gameList) { GameListSummary item = GameListSummary.builder() .gameId(v.getGameId()) .build(); @@ -74,11 +81,69 @@ public class GameDatabaseInfoController { return ProtocolRespDTO.result(ErrorCode.ERR_OK, rspInfo); } + @PostMapping("gamelistDetails") + @ResponseBody + public ProtocolRespDTO getGameInfoDetails(@Validated(ValidGroups.AddGameServiceValid.class) + @RequestBody ProtocolReqDTO mr) { + GetGamesInfoRsp rspInfo = GetGamesInfoRsp.builder().build(); + List gameContent = new ArrayList<>(); + + //获取TaskId列表 + List reqTaskIds = Optional.ofNullable(mr.getMsgContent().getItems()) + .orElse(new ArrayList<>()); + + List ret = gameDatabaseServiceService.getGameTableItems(reqTaskIds); + + // 不存在的ID + reqTaskIds.stream() + .filter(v -> ret.stream().noneMatch(k -> k.getGameId().equals(v))) + .forEach(v -> { + GameInfoContent gi = GameInfoContent.builder().build(); + gi.setGameId(v); + gi.setStatus(ErrorCode.ERR_NOSUCHTASK.getCode()); + gi.setMessage(new String[]{ErrorCode.ERR_NOSUCHTASK.getMsg()}); + gameContent.add(gi); + }); + + for (GameTableItemsDetail v : ret) { + GameInfoContent gi = GameInfoContent.builder() + .gameId(v.getGameId()) + .company(v.getCompany()) + .name(v.getName()) + .gamesInfo(new ArrayList<>()) + .build(); + + gi.setStatus(ErrorCode.ERR_OK.getCode()); + gi.setMessage(new String[]{ErrorCode.ERR_OK.getMsg()}); + + v.getGameDetails().forEach(k -> { + GameInfoDetail gd = GameInfoDetail.builder() + .gameInfoId(k.getGameInfoId()) + .version(k.getVersion()) + .gameFileName(k.getFilename()) + .md5sum(k.getMd5sum()) + .build(); + + gi.getGamesInfo().add(gd); + }); + + gameContent.add(gi); + } + + rspInfo.getItems().addAll(gameContent.stream().sorted(Comparator.comparingLong(GameInfoContent::getGameId)) + .collect(Collectors.toList())); + + rspInfo.setStatus(ErrorCode.ERR_OK.getCode()); + rspInfo.setMessage(new String[]{ErrorCode.ERR_OK.getMsg()}); + + return ProtocolRespDTO.result(ErrorCode.ERR_OK, rspInfo); + } + @PostMapping("/gamedata") @ResponseBody public ProtocolRespDTO reportGameServiceData( @Validated(ValidGroups.AddGameServiceValid.class) - @RequestBody ProtocolReqDTO mr) { + @RequestBody ProtocolReqDTO mr) { GameServerRsp rspInfo = new GameServerRsp(); rspInfo.setItems(new ArrayList<>()); // diff --git a/src/main/java/com/cmhi/gds/exception/GlobalExceptionHandler.java b/src/main/java/com/cmhi/gds/exception/GlobalExceptionHandler.java index 2669cf9..58098ba 100644 --- a/src/main/java/com/cmhi/gds/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/cmhi/gds/exception/GlobalExceptionHandler.java @@ -44,7 +44,6 @@ public class GlobalExceptionHandler { String reqPath = req.getRequestURI(); String reqIp = req.getRemoteAddr(); String reqToken = req.getHeader("Authorization"); - if (reqToken != null && !reqToken.isEmpty()) { reqToken = reqToken.replace(ConstValue.STRING_HTTP_AUTH_HEAD, ""); } @@ -66,10 +65,9 @@ public class GlobalExceptionHandler { } sb.append("]"); - log.error(""" - Interface [{}] request <{}> from {}, token = <{}> - +++ Request: {} - --- Verify params failed: {}""", + log.error("Interface [{}] request <{}> from {}, token = <{}>\n" + + "+++ Request: {}\n" + + "--- Verify params failed: {}", reqType, reqPath, reqIp, reqToken, Helper.inputStream2String(req.getInputStream()), sb); } catch (Exception ignored) { } @@ -109,14 +107,12 @@ public class GlobalExceptionHandler { String reqPath = req.getRequestURI(); String reqIp = req.getRemoteAddr(); String reqToken = req.getHeader("Authorization"); - if (reqToken != null && !reqToken.isEmpty()) { reqToken = reqToken.replace(ConstValue.STRING_HTTP_AUTH_HEAD, ""); } - log.error(""" - Interface [{}] request <{}> from {}, token = <{}> - +++ Request: {} - --- Verify params failed: {}""", + log.error("Interface [{}] request <{}> from {}, token = <{}>\n" + + "+++ Request: {}\n" + + "--- Verify params failed: {}", reqType, reqPath, reqIp, reqToken, Helper.inputStream2String(req.getInputStream()), ex.getMessage() == null ? ex.getDescription() : ex.getMessage()); } catch (Exception ignored) { @@ -163,10 +159,9 @@ public class GlobalExceptionHandler { if (reqToken != null && !reqToken.isEmpty()) { reqToken = reqToken.replace(ConstValue.STRING_HTTP_AUTH_HEAD, ""); } - log.error(""" - Interface [{}] request <{}> from {}, token = <{}> - +++ Request: {} - --- Verify params failed: {}""", + log.error("Interface [{}] request <{}> from {}, token = <{}>\n" + + "+++ Request: {}\n" + + "--- Verify params failed: {}", reqType, reqPath, reqIp, reqToken, Helper.inputStream2String(req.getInputStream()), ex.getMessage()); } catch (Exception ignored) { diff --git a/src/main/java/com/cmhi/gds/interceptor/RequestBodyCacheWrapper.java b/src/main/java/com/cmhi/gds/interceptor/RequestBodyCacheWrapper.java new file mode 100644 index 0000000..4482b3f --- /dev/null +++ b/src/main/java/com/cmhi/gds/interceptor/RequestBodyCacheWrapper.java @@ -0,0 +1,106 @@ +package com.cmhi.gds.interceptor; + +import com.cmhi.gds.misc.Helper; + +import javax.servlet.ReadListener; +import javax.servlet.ServletInputStream; +import javax.servlet.ServletRequest; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.Charset; + +/** + * The type Request body cache wrapper. + * + * @author + */ +public class RequestBodyCacheWrapper extends HttpServletRequestWrapper { + /** + * The Body. + */ + private final byte[] body; + + /** + * Instantiates a new Request body cache wrapper. + * + * @param request the request + */ + public RequestBodyCacheWrapper(HttpServletRequest request) { + super(request); + // 将body数据存储起来 + String bodyStr = getBodyString(request); + body = bodyStr.getBytes(Charset.defaultCharset()); + } + + /** + * Gets body string. + * + * @param request the request + * @return the body string + */ + public String getBodyString(final ServletRequest request) { + try { + return Helper.inputStream2String(request.getInputStream()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + /** + * Gets body string. + * + * @return the body string + */ + public String getBodyString() { + final InputStream inputStream = new ByteArrayInputStream(body); + + return Helper.inputStream2String(inputStream); + } + + /** + * Gets reader. + * + * @return the reader + */ + @Override + public BufferedReader getReader() { + return new BufferedReader(new InputStreamReader(getInputStream())); + } + + /** + * Gets input stream. + * + * @return the input stream + */ + @Override + public ServletInputStream getInputStream() { + + final ByteArrayInputStream inputStream = new ByteArrayInputStream(body); + + return new ServletInputStream() { + @Override + public int read() { + return inputStream.read(); + } + + @Override + public boolean isFinished() { + return false; + } + + @Override + public boolean isReady() { + return false; + } + + @Override + public void setReadListener(ReadListener readListener) { + } + }; + } +} diff --git a/src/main/java/com/cmhi/gds/interceptor/RequestBodyFilter.java b/src/main/java/com/cmhi/gds/interceptor/RequestBodyFilter.java new file mode 100644 index 0000000..ea6c269 --- /dev/null +++ b/src/main/java/com/cmhi/gds/interceptor/RequestBodyFilter.java @@ -0,0 +1,52 @@ +package com.cmhi.gds.interceptor; + + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.annotation.WebFilter; +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; + +/** + * The type Request body filter. + * + * @author + */ + +@WebFilter(filterName = "RequestBodyFilter", urlPatterns = "/*") +public class RequestBodyFilter implements Filter { + /** + * Init. + * + * @param filterConfig the filter config + */ + @Override + public void init(FilterConfig filterConfig) { + } + + /** + * Destroy. + */ + @Override + public void destroy() { + } + + /** + * Do filter. + * + * @param request the request + * @param response the response + * @param filterChain the filter chain + * @throws IOException the io exception + * @throws ServletException the servlet exception + */ + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { + ServletRequest requestWrapper = new RequestBodyCacheWrapper((HttpServletRequest) request); + filterChain.doFilter(requestWrapper, response); + } +} diff --git a/src/main/java/com/cmhi/gds/interceptor/RequestProtocolSecurity.java b/src/main/java/com/cmhi/gds/interceptor/RequestProtocolSecurity.java new file mode 100644 index 0000000..6247ff1 --- /dev/null +++ b/src/main/java/com/cmhi/gds/interceptor/RequestProtocolSecurity.java @@ -0,0 +1,104 @@ +package com.cmhi.gds.interceptor; + +import com.cmhi.gds.security.annotation.Decryption; +import com.cmhi.gds.service.ProtocolSecurityService; +import lombok.extern.slf4j.Slf4j; +import org.jetbrains.annotations.NotNull; +import org.springframework.core.MethodParameter; +import org.springframework.http.HttpInputMessage; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice; + +import javax.annotation.Resource; +import java.io.IOException; +import java.lang.reflect.Type; + +/** + * The type Request protocol security. + * + * @author + */ +@Slf4j +@RestControllerAdvice +public class RequestProtocolSecurity implements RequestBodyAdvice { + + /** + * The Protocol security service. + */ + @Resource + private ProtocolSecurityService protocolSecurityService; + + /** + * Supports boolean. + * + * @param methodParameter the method parameter + * @param type the type + * @param aClass the a class + * @return the boolean + */ + @Override + public boolean supports(@NotNull MethodParameter methodParameter, + @NotNull Type type, + @NotNull Class> aClass) { + return methodParameter.getContainingClass().isAnnotationPresent(Decryption.class) + || methodParameter.hasMethodAnnotation(Decryption.class); + } + + /** + * Before body read http input message. + * + * @param httpInputMessage the http input message + * @param methodParameter the method parameter + * @param type the type + * @param aClass the a class + * @return the http input message + */ + @Override + @NotNull + public HttpInputMessage beforeBodyRead(@NotNull HttpInputMessage httpInputMessage, + @NotNull MethodParameter methodParameter, + @NotNull Type type, + @NotNull Class> aClass) throws IOException { + return protocolSecurityService.decryptProtocol(httpInputMessage); + } + + /** + * Handle empty body object. + * + * @param o the o + * @param httpInputMessage the http input message + * @param methodParameter the method parameter + * @param type the type + * @param aClass the a class + * @return the object + */ + @Override + public Object handleEmptyBody(Object o, + @NotNull HttpInputMessage httpInputMessage, + @NotNull MethodParameter methodParameter, + @NotNull Type type, + @NotNull Class> aClass) { + return o; + } + + /** + * After body read object. + * + * @param o the o + * @param httpInputMessage the http input message + * @param methodParameter the method parameter + * @param type the type + * @param aClass the a class + * @return the object + */ + @Override + @NotNull + public Object afterBodyRead(@NotNull Object o, + @NotNull HttpInputMessage httpInputMessage, + @NotNull MethodParameter methodParameter, + @NotNull Type type, + @NotNull Class> aClass) { + return o; + } +} diff --git a/src/main/java/com/cmhi/gds/interceptor/ResponseProtocolSecurity.java b/src/main/java/com/cmhi/gds/interceptor/ResponseProtocolSecurity.java new file mode 100644 index 0000000..1311e6a --- /dev/null +++ b/src/main/java/com/cmhi/gds/interceptor/ResponseProtocolSecurity.java @@ -0,0 +1,83 @@ +package com.cmhi.gds.interceptor; + +import com.cmhi.gds.common.ProtoCryptoType; +import com.cmhi.gds.common.SecurityConfigValue; +import com.cmhi.gds.pojo.dto.protocol.base.ProtocolRespDTO; +import com.cmhi.gds.security.annotation.Encryption; +import com.cmhi.gds.service.ProtocolSecurityService; +import lombok.extern.slf4j.Slf4j; +import org.jetbrains.annotations.NotNull; +import org.springframework.core.MethodParameter; +import org.springframework.http.MediaType; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.server.ServerHttpRequest; +import org.springframework.http.server.ServerHttpResponse; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; + +import javax.annotation.Resource; + +/** + * The type Response protocol security. + * + * @author + */ +@Slf4j +@RestControllerAdvice +public class ResponseProtocolSecurity implements ResponseBodyAdvice { + + /** + * The Protocol security service. + */ + @Resource + private ProtocolSecurityService protocolSecurityService; + + /** + * Supports boolean. + * + * @param methodParameter the method parameter + * @param aClass the a class + * @return the boolean + */ + @Override + public boolean supports(@NotNull MethodParameter methodParameter, + @NotNull Class> aClass) { + + return methodParameter.getContainingClass().isAnnotationPresent(Encryption.class) + || methodParameter.hasMethodAnnotation(Encryption.class); + } + + /** + * Before body write object. + * + * @param o the o + * @param methodParameter the method parameter + * @param mediaType the media type + * @param aClass the a class + * @param serverHttpRequest the server http request + * @param serverHttpResponse the server http response + * @return the object + */ + @Override + @NotNull + public Object beforeBodyWrite(Object o, + @NotNull MethodParameter methodParameter, + @NotNull MediaType mediaType, + @NotNull Class> aClass, + @NotNull ServerHttpRequest serverHttpRequest, + @NotNull ServerHttpResponse serverHttpResponse) { + if (SecurityConfigValue.SECURITY_PROTOCOL_TYPE == ProtoCryptoType.CRYPTO_NONE.getCode()) { + if (o instanceof ProtocolRespDTO) { + serverHttpResponse.setStatusCode(org.springframework.http.HttpStatus.valueOf(((ProtocolRespDTO) o).getCode())); + } + + return o; + } else { + ProtocolRespDTO rspInfo = protocolSecurityService.encryptProtocol(o, + SecurityConfigValue.SECURITY_PROTOCOL_TYPE); + serverHttpResponse.setStatusCode(org.springframework.http.HttpStatus.valueOf(rspInfo.getCode())); + return rspInfo; + } + + } +} diff --git a/src/main/java/com/cmhi/gds/manager/GameDatabaseServiceManager.java b/src/main/java/com/cmhi/gds/manager/GameDatabaseServiceManager.java index 90b9376..6202777 100644 --- a/src/main/java/com/cmhi/gds/manager/GameDatabaseServiceManager.java +++ b/src/main/java/com/cmhi/gds/manager/GameDatabaseServiceManager.java @@ -1,9 +1,12 @@ package com.cmhi.gds.manager; -import com.cmhi.gds.pojo.entry.GameTableSummary; +import com.cmhi.gds.pojo.vo.GameTableItemsDetail; +import com.cmhi.gds.pojo.vo.GameTableSummary; import java.util.List; public interface GameDatabaseServiceManager { List getGameTableSummary(); + + List getGameTableDetails(List gameId); } diff --git a/src/main/java/com/cmhi/gds/manager/impl/GameDatabaseServiceManagerImpl.java b/src/main/java/com/cmhi/gds/manager/impl/GameDatabaseServiceManagerImpl.java index 5ec84f2..f62bd6a 100644 --- a/src/main/java/com/cmhi/gds/manager/impl/GameDatabaseServiceManagerImpl.java +++ b/src/main/java/com/cmhi/gds/manager/impl/GameDatabaseServiceManagerImpl.java @@ -4,11 +4,12 @@ import com.cmhi.gds.manager.GameDatabaseServiceManager; import com.cmhi.gds.mapper.GameInfoMapper; import com.cmhi.gds.mapper.GameServerMapper; import com.cmhi.gds.mapper.GameTableMapper; -import com.cmhi.gds.pojo.entry.GameTableSummary; -import jakarta.annotation.Resource; +import com.cmhi.gds.pojo.vo.GameTableItemsDetail; +import com.cmhi.gds.pojo.vo.GameTableSummary; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; +import javax.annotation.Resource; import java.util.List; @Component @@ -27,4 +28,9 @@ public class GameDatabaseServiceManagerImpl implements GameDatabaseServiceManage public List getGameTableSummary() { return gameTableMapper.selectAllGameSummary(); } + + @Override + public List getGameTableDetails(List gameId) { + return gameTableMapper.selectAllGameDetailById(gameId); + } } diff --git a/src/main/java/com/cmhi/gds/mapper/GameTableMapper.java b/src/main/java/com/cmhi/gds/mapper/GameTableMapper.java index 71ff570..1aadb5e 100644 --- a/src/main/java/com/cmhi/gds/mapper/GameTableMapper.java +++ b/src/main/java/com/cmhi/gds/mapper/GameTableMapper.java @@ -1,7 +1,9 @@ package com.cmhi.gds.mapper; import com.cmhi.gds.pojo.entry.GameTable; -import com.cmhi.gds.pojo.entry.GameTableSummary; +import com.cmhi.gds.pojo.vo.GameTableItemsDetail; +import com.cmhi.gds.pojo.vo.GameTableSummary; +import org.apache.ibatis.annotations.Param; import java.util.List; @@ -9,4 +11,6 @@ public interface GameTableMapper { List selectAll(); List selectAllGameSummary(); + + List selectAllGameDetailById(@Param("idList") List idList); } diff --git a/src/main/java/com/cmhi/gds/pojo/dto/protocol/base/BaseProtocolDTO.java b/src/main/java/com/cmhi/gds/pojo/dto/protocol/base/BaseProtocolDTO.java index 364d2c5..d95c577 100644 --- a/src/main/java/com/cmhi/gds/pojo/dto/protocol/base/BaseProtocolDTO.java +++ b/src/main/java/com/cmhi/gds/pojo/dto/protocol/base/BaseProtocolDTO.java @@ -18,7 +18,6 @@ import javax.validation.constraints.NotNull; */ @Data @NoArgsConstructor - @JsonPropertyOrder({"ver", "cryptoType", "timeStamp", "msgContent"}) public class BaseProtocolDTO { /** diff --git a/src/main/java/com/cmhi/gds/pojo/dto/protocol/info/GameInfoContent.java b/src/main/java/com/cmhi/gds/pojo/dto/protocol/info/GameInfoContent.java index 24f6127..4153115 100644 --- a/src/main/java/com/cmhi/gds/pojo/dto/protocol/info/GameInfoContent.java +++ b/src/main/java/com/cmhi/gds/pojo/dto/protocol/info/GameInfoContent.java @@ -1,22 +1,26 @@ package com.cmhi.gds.pojo.dto.protocol.info; +import com.cmhi.gds.pojo.dto.protocol.base.BaseRespStatus; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import java.util.List; +@EqualsAndHashCode(callSuper = true) @Data @Builder @NoArgsConstructor @AllArgsConstructor -@JsonPropertyOrder({"gameId", "gamesInfo"}) +@JsonPropertyOrder({"gameId", "name", "company", "gamesInfo", "status", "message"}) @JsonInclude(JsonInclude.Include.NON_NULL) -public class GameInfoContent { - private Long gameId; - +public class GameInfoContent extends BaseRespStatus { List gamesInfo; + private Long gameId; + private String name; + private String company; } diff --git a/src/main/java/com/cmhi/gds/pojo/dto/protocol/info/GameInfoDetail.java b/src/main/java/com/cmhi/gds/pojo/dto/protocol/info/GameInfoDetail.java index 49b72a5..351a246 100644 --- a/src/main/java/com/cmhi/gds/pojo/dto/protocol/info/GameInfoDetail.java +++ b/src/main/java/com/cmhi/gds/pojo/dto/protocol/info/GameInfoDetail.java @@ -14,6 +14,7 @@ import lombok.NoArgsConstructor; @JsonPropertyOrder({"gameFileName", "md5sum", "version"}) @JsonInclude(JsonInclude.Include.NON_NULL) public class GameInfoDetail { + private Long gameInfoId; private String gameFileName; private String md5sum; private String version; diff --git a/src/main/java/com/cmhi/gds/pojo/dto/protocol/info/GetGameInfoDetailReq.java b/src/main/java/com/cmhi/gds/pojo/dto/protocol/info/GetGameInfoDetailReq.java new file mode 100644 index 0000000..aa04cdf --- /dev/null +++ b/src/main/java/com/cmhi/gds/pojo/dto/protocol/info/GetGameInfoDetailReq.java @@ -0,0 +1,23 @@ +package com.cmhi.gds.pojo.dto.protocol.info; + +import com.cmhi.gds.validation.group.ValidGroups; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.validation.Valid; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; +import java.util.List; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class GetGameInfoDetailReq { + @NotNull(message = "items 设备列表不能为空", groups = ValidGroups.AddGameServiceValid.class) + @Size(min = 1, message = "items 至少需要有一个元素", groups = ValidGroups.AddGameServiceValid.class) + @Valid + private List items; +} diff --git a/src/main/java/com/cmhi/gds/pojo/dto/protocol/info/GameServerReq.java b/src/main/java/com/cmhi/gds/pojo/dto/protocol/info/ReportGameServerReq.java similarity index 74% rename from src/main/java/com/cmhi/gds/pojo/dto/protocol/info/GameServerReq.java rename to src/main/java/com/cmhi/gds/pojo/dto/protocol/info/ReportGameServerReq.java index 24f1ddf..ff7aa51 100644 --- a/src/main/java/com/cmhi/gds/pojo/dto/protocol/info/GameServerReq.java +++ b/src/main/java/com/cmhi/gds/pojo/dto/protocol/info/ReportGameServerReq.java @@ -1,8 +1,6 @@ package com.cmhi.gds.pojo.dto.protocol.info; import com.cmhi.gds.validation.group.ValidGroups; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonPropertyOrder; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -17,9 +15,7 @@ import java.util.List; @Builder @NoArgsConstructor @AllArgsConstructor -@JsonPropertyOrder({"gameId", "services"}) -@JsonInclude(JsonInclude.Include.NON_NULL) -public class GameServerReq { +public class ReportGameServerReq { @NotNull(message = "items 设备列表不能为空", groups = ValidGroups.AddGameServiceValid.class) @Size(min = 1, message = "items 至少需要有一个元素", groups = ValidGroups.AddGameServiceValid.class) @Valid diff --git a/src/main/java/com/cmhi/gds/pojo/entry/GameInfo.java b/src/main/java/com/cmhi/gds/pojo/entry/GameInfo.java index 721240e..cfbcae3 100644 --- a/src/main/java/com/cmhi/gds/pojo/entry/GameInfo.java +++ b/src/main/java/com/cmhi/gds/pojo/entry/GameInfo.java @@ -12,7 +12,6 @@ import tk.mybatis.mapper.code.Style; import javax.persistence.Id; import javax.persistence.Table; -import java.io.Serial; import java.io.Serializable; @Data @@ -27,7 +26,6 @@ public class GameInfo implements Serializable { /** * The constant serialVersionUID. */ - @Serial private static final long serialVersionUID = 1L; /** diff --git a/src/main/java/com/cmhi/gds/pojo/entry/GameServer.java b/src/main/java/com/cmhi/gds/pojo/entry/GameServer.java index 5b17fa4..934afed 100644 --- a/src/main/java/com/cmhi/gds/pojo/entry/GameServer.java +++ b/src/main/java/com/cmhi/gds/pojo/entry/GameServer.java @@ -13,7 +13,6 @@ import tk.mybatis.mapper.code.Style; import javax.persistence.Id; import javax.persistence.Table; -import java.io.Serial; import java.io.Serializable; @Data @@ -28,7 +27,7 @@ public class GameServer implements Serializable { /** * The constant serialVersionUID. */ - @Serial + private static final long serialVersionUID = 1L; /** diff --git a/src/main/java/com/cmhi/gds/pojo/entry/GameTable.java b/src/main/java/com/cmhi/gds/pojo/entry/GameTable.java index 3911fbe..7535a56 100644 --- a/src/main/java/com/cmhi/gds/pojo/entry/GameTable.java +++ b/src/main/java/com/cmhi/gds/pojo/entry/GameTable.java @@ -12,7 +12,6 @@ import tk.mybatis.mapper.code.Style; import javax.persistence.Id; import javax.persistence.Table; -import java.io.Serial; import java.io.Serializable; @Data @@ -27,7 +26,6 @@ public class GameTable implements Serializable { /** * The constant serialVersionUID. */ - @Serial private static final long serialVersionUID = 1L; /** diff --git a/src/main/java/com/cmhi/gds/pojo/vo/GameDetail.java b/src/main/java/com/cmhi/gds/pojo/vo/GameDetail.java new file mode 100644 index 0000000..f5b0f2c --- /dev/null +++ b/src/main/java/com/cmhi/gds/pojo/vo/GameDetail.java @@ -0,0 +1,22 @@ +package com.cmhi.gds.pojo.vo; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +@JsonInclude(JsonInclude.Include.NON_NULL) +public class GameDetail { + private Long gameInfoId; + + private String version; + + private String filename; + + private String md5sum; +} diff --git a/src/main/java/com/cmhi/gds/pojo/entry/GameSummary.java b/src/main/java/com/cmhi/gds/pojo/vo/GameSummary.java similarity index 88% rename from src/main/java/com/cmhi/gds/pojo/entry/GameSummary.java rename to src/main/java/com/cmhi/gds/pojo/vo/GameSummary.java index 3afe5c0..88daf39 100644 --- a/src/main/java/com/cmhi/gds/pojo/entry/GameSummary.java +++ b/src/main/java/com/cmhi/gds/pojo/vo/GameSummary.java @@ -1,4 +1,4 @@ -package com.cmhi.gds.pojo.entry; +package com.cmhi.gds.pojo.vo; import lombok.AllArgsConstructor; import lombok.Builder; diff --git a/src/main/java/com/cmhi/gds/pojo/vo/GameTableItemsDetail.java b/src/main/java/com/cmhi/gds/pojo/vo/GameTableItemsDetail.java new file mode 100644 index 0000000..3bdf401 --- /dev/null +++ b/src/main/java/com/cmhi/gds/pojo/vo/GameTableItemsDetail.java @@ -0,0 +1,24 @@ +package com.cmhi.gds.pojo.vo; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +@JsonInclude(JsonInclude.Include.NON_NULL) +public class GameTableItemsDetail { + List gameDetails; + + private Long gameId; + + private String name; + + private String company; +} diff --git a/src/main/java/com/cmhi/gds/pojo/vo/GameTableItems.java b/src/main/java/com/cmhi/gds/pojo/vo/GameTableItemsSummary.java similarity index 91% rename from src/main/java/com/cmhi/gds/pojo/vo/GameTableItems.java rename to src/main/java/com/cmhi/gds/pojo/vo/GameTableItemsSummary.java index 8fa8a73..0bd1794 100644 --- a/src/main/java/com/cmhi/gds/pojo/vo/GameTableItems.java +++ b/src/main/java/com/cmhi/gds/pojo/vo/GameTableItemsSummary.java @@ -13,7 +13,7 @@ import java.util.List; @NoArgsConstructor @Builder @JsonInclude(JsonInclude.Include.NON_NULL) -public class GameTableItems { +public class GameTableItemsSummary { private Long gameId; private List gameFilename; } diff --git a/src/main/java/com/cmhi/gds/pojo/entry/GameTableSummary.java b/src/main/java/com/cmhi/gds/pojo/vo/GameTableSummary.java similarity index 80% rename from src/main/java/com/cmhi/gds/pojo/entry/GameTableSummary.java rename to src/main/java/com/cmhi/gds/pojo/vo/GameTableSummary.java index 1f49d4c..0b50369 100644 --- a/src/main/java/com/cmhi/gds/pojo/entry/GameTableSummary.java +++ b/src/main/java/com/cmhi/gds/pojo/vo/GameTableSummary.java @@ -1,5 +1,6 @@ -package com.cmhi.gds.pojo.entry; +package com.cmhi.gds.pojo.vo; +import com.cmhi.gds.pojo.vo.GameSummary; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; diff --git a/src/main/java/com/cmhi/gds/service/GameDatabaseServiceService.java b/src/main/java/com/cmhi/gds/service/GameDatabaseServiceService.java index 980a823..77a05ce 100644 --- a/src/main/java/com/cmhi/gds/service/GameDatabaseServiceService.java +++ b/src/main/java/com/cmhi/gds/service/GameDatabaseServiceService.java @@ -1,10 +1,13 @@ package com.cmhi.gds.service; -import com.cmhi.gds.pojo.vo.GameTableItems; +import com.cmhi.gds.pojo.vo.GameTableItemsDetail; +import com.cmhi.gds.pojo.vo.GameTableItemsSummary; import java.util.List; public interface GameDatabaseServiceService { - List getGameTableSummaryItems(); + List getGameTableSummaryItems(); + + List getGameTableItems(List gameIds); } diff --git a/src/main/java/com/cmhi/gds/service/ProtocolSecurityService.java b/src/main/java/com/cmhi/gds/service/ProtocolSecurityService.java new file mode 100644 index 0000000..3d98dc4 --- /dev/null +++ b/src/main/java/com/cmhi/gds/service/ProtocolSecurityService.java @@ -0,0 +1,51 @@ +package com.cmhi.gds.service; + +import com.cmhi.gds.pojo.dto.protocol.base.ProtocolRespDTO; +import com.cmhi.gds.security.protocol.DecryptRequestProtocol; +import com.fasterxml.jackson.core.JsonProcessingException; +import org.springframework.http.HttpInputMessage; + +import java.io.IOException; + +/** + * The interface Protocol security service. + * + * @author + */ +public interface ProtocolSecurityService { + /** + * Decrypt protocol string. + * + * @param ciphertext the ciphertext + * @return the string + * @throws JsonProcessingException the json processing exception + */ + String decryptProtocol(String ciphertext) throws JsonProcessingException; + + /** + * Decrypt protocol decrypt request protocol. + * + * @param httpInputMessage the http input message + * @return the decrypt request protocol + * @throws IOException the io exception + */ + DecryptRequestProtocol decryptProtocol(HttpInputMessage httpInputMessage) throws IOException; + + /** + * Encrypt protocol string string. + * + * @param plainText the plain text + * @param cryptoType the crypto type + * @return the string + */ + String encryptProtocolString(String plainText, int cryptoType); + + /** + * Encrypt protocol protocol resp dto. + * + * @param orgProtocol the org protocol + * @param cryptoType the crypto type + * @return the protocol resp dto + */ + ProtocolRespDTO encryptProtocol(Object orgProtocol, int cryptoType); +} diff --git a/src/main/java/com/cmhi/gds/service/impl/GameDatabaseServiceServiceImpl.java b/src/main/java/com/cmhi/gds/service/impl/GameDatabaseServiceServiceImpl.java index 3769738..460a931 100644 --- a/src/main/java/com/cmhi/gds/service/impl/GameDatabaseServiceServiceImpl.java +++ b/src/main/java/com/cmhi/gds/service/impl/GameDatabaseServiceServiceImpl.java @@ -1,14 +1,15 @@ package com.cmhi.gds.service.impl; import com.cmhi.gds.manager.GameDatabaseServiceManager; -import com.cmhi.gds.pojo.entry.GameSummary; -import com.cmhi.gds.pojo.entry.GameTableSummary; -import com.cmhi.gds.pojo.vo.GameTableItems; +import com.cmhi.gds.pojo.vo.GameSummary; +import com.cmhi.gds.pojo.vo.GameTableItemsDetail; +import com.cmhi.gds.pojo.vo.GameTableItemsSummary; +import com.cmhi.gds.pojo.vo.GameTableSummary; import com.cmhi.gds.service.GameDatabaseServiceService; -import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import javax.annotation.Resource; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -21,14 +22,14 @@ public class GameDatabaseServiceServiceImpl implements GameDatabaseServiceServic @Override - public List getGameTableSummaryItems() { - List items = new ArrayList<>(); + public List getGameTableSummaryItems() { + List items = new ArrayList<>(); List gs = gameDatabaseServiceManager.getGameTableSummary(); if (!gs.isEmpty()) { for (GameTableSummary v : gs) { - GameTableItems item = GameTableItems.builder() + GameTableItemsSummary item = GameTableItemsSummary.builder() .gameId(v.getId()) .build(); @@ -46,4 +47,9 @@ public class GameDatabaseServiceServiceImpl implements GameDatabaseServiceServic } return items; } + + @Override + public List getGameTableItems(List gameIds) { + return gameDatabaseServiceManager.getGameTableDetails(gameIds); + } } diff --git a/src/main/java/com/cmhi/gds/service/impl/ProtocolSecurityServiceImpl.java b/src/main/java/com/cmhi/gds/service/impl/ProtocolSecurityServiceImpl.java new file mode 100644 index 0000000..ec5a7e8 --- /dev/null +++ b/src/main/java/com/cmhi/gds/service/impl/ProtocolSecurityServiceImpl.java @@ -0,0 +1,221 @@ +package com.cmhi.gds.service.impl; + + +import com.cmhi.gds.common.ConstValue; +import com.cmhi.gds.common.ErrorCode; +import com.cmhi.gds.pojo.dto.protocol.base.ProtocolRespDTO; +import com.cmhi.gds.common.GlobalConfigure; +import com.cmhi.gds.common.ProtoCryptoType; +import com.cmhi.gds.common.SecurityConfigValue; +import com.cmhi.gds.exception.ControllerRequestTimeoutException; +import com.cmhi.gds.exception.SecurityProtocolException; +import com.cmhi.gds.pojo.dto.protocol.base.ProtocolReqDTO; +import com.cmhi.gds.security.arithmetic.CryptoHelper; +import com.cmhi.gds.security.protocol.DecryptRequestProtocol; +import com.cmhi.gds.service.ProtocolSecurityService; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.IOUtils; +import org.springframework.http.HttpInputMessage; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.io.IOException; +import java.lang.reflect.Method; +import java.nio.charset.StandardCharsets; + +/** + * The type Protocol security service. + * + * @author + */ +@Service +@Slf4j +public class ProtocolSecurityServiceImpl implements ProtocolSecurityService { + /** + * The Object mapper. + */ + @Resource + private ObjectMapper objectMapper; + + /** + * Decrypt protocol string. + * + * @param ciphertext the ciphertext + * @return the string + * @throws JsonProcessingException the json processing exception + */ + @Override + public String decryptProtocol(String ciphertext) throws JsonProcessingException { + JsonNode objRoot = objectMapper.readTree(ciphertext); + + // 判断协议请求是否超时 + if(GlobalConfigure.CHECK_PROTO_REQUEST_TIMEOUT) { + long timeStamp = objRoot.path("timeStamp").asLong(); + long current = System.currentTimeMillis(); + long diff = current - timeStamp; + + if(diff > GlobalConfigure.REQUEST_TIMEOUT_MS) { + String errMsg = String.format("Request times %d, current times %d, timeout configure %d ms, timeout %d", + timeStamp, current, GlobalConfigure.REQUEST_TIMEOUT_MS, diff); + + throw new ControllerRequestTimeoutException(ErrorCode.ERR_REQTIMEOUT, errMsg); + } + } + + + + int cryptoType = objRoot.path("cryptoType").asInt(); + + // 协议未加密 + if (cryptoType == ProtoCryptoType.CRYPTO_NONE.getCode()) { + return ciphertext; + } + + if (cryptoType > ProtoCryptoType.CRYPTO_AES256.getCode() || cryptoType < ProtoCryptoType.CRYPTO_NONE.getCode()) { + throw new SecurityProtocolException(ErrorCode.ERR_PARAMS, "cryptoType 字段取值为 [0, 4]"); + } + + // 反序列化为对象处理 + ProtocolReqDTO proReq = objectMapper + .readValue(ciphertext, new TypeReference>() { + }); + + // 首先对加密内容进行base64解码 + byte[] base64Decode = CryptoHelper.base64Decryption(proReq.getMsgContent()); + + // 解密后的内容 + byte[] decryptContent; + + // 加密类型为base64直接返回处理结果 + if (proReq.getCryptoType() == ProtoCryptoType.CRYPTO_BASE64.getCode()) { + decryptContent = base64Decode; + } else if (proReq.getCryptoType() == ProtoCryptoType.CRYPTO_AES128.getCode()) { + try { + decryptContent = CryptoHelper.aes128Decryption(base64Decode, SecurityConfigValue.AES_KEY); + } catch (Exception e) { + log.error("AES128 decode message error: {}", base64Decode); + throw new SecurityProtocolException(ErrorCode.ERR_DECRYPT_AES128); + } + } else if (proReq.getCryptoType() == ProtoCryptoType.CRYPTO_AES256.getCode()) { + try { + decryptContent = CryptoHelper.aes256Decryption(base64Decode, SecurityConfigValue.AES_KEY); + } catch (Exception e) { + log.error("AES256 decode message error: {}", base64Decode); + throw new SecurityProtocolException(ErrorCode.ERR_DECRYPT_AES256); + } + } else if (proReq.getCryptoType() == ProtoCryptoType.CRYPTO_DES.getCode()) { + try { + decryptContent = CryptoHelper.desDecryption(base64Decode, SecurityConfigValue.DES_KEY); + } catch (Exception e) { + log.error("DES decode message error: {}", base64Decode); + throw new SecurityProtocolException(ErrorCode.ERR_DECRYPT_3DES); + } + } else { + log.error("Unknown protocol security type: {}, {}", proReq.getCryptoType(), ciphertext); + throw new SecurityProtocolException(ErrorCode.ERR_DECRYPT_UNKNOWN); + } + + // 字节数组转换为字符串 + String decodeMsg = new String(decryptContent, StandardCharsets.UTF_8); + + return ciphertext.replace("\"" + proReq.getMsgContent() + "\"", decodeMsg); + } + + /** + * Decrypt protocol decrypt request protocol. + * + * @param httpInputMessage the http input message + * @return the decrypt request protocol + * @throws IOException the io exception + */ + @Override + public DecryptRequestProtocol decryptProtocol(HttpInputMessage httpInputMessage) throws IOException { + // 提取协议中的JSON字符串 + String reqMessage = IOUtils.toString(httpInputMessage.getBody(), String.valueOf(StandardCharsets.UTF_8)); + + return new DecryptRequestProtocol(httpInputMessage, decryptProtocol(reqMessage)); + } + + /** + * Encrypt protocol string string. + * + * @param plainText the plain text + * @param cryptoType the crypto type + * @return the string + */ + @Override + public String encryptProtocolString(String plainText, int cryptoType) { + String cipherText; + if (cryptoType == ProtoCryptoType.CRYPTO_BASE64.getCode()) { + cipherText = CryptoHelper.base64Encryption(plainText.getBytes(StandardCharsets.UTF_8)); + } else if (cryptoType == ProtoCryptoType.CRYPTO_AES128.getCode()) { + try { + byte[] encode = CryptoHelper.aes128Encryption(plainText.getBytes(StandardCharsets.UTF_8), + SecurityConfigValue.AES_KEY); + cipherText = CryptoHelper.base64Encryption(encode); + } catch (Exception e) { + log.error("AES128 encode message error: {}", plainText); + throw new SecurityProtocolException(ErrorCode.ERR_ENCRYPT_AES128); + } + } else if (cryptoType == ProtoCryptoType.CRYPTO_AES256.getCode()) { + try { + byte[] encode = CryptoHelper.aes256Encryption(plainText.getBytes(StandardCharsets.UTF_8), + SecurityConfigValue.AES_KEY); + cipherText = CryptoHelper.base64Encryption(encode); + } catch (Exception e) { + log.error("AES256 encode message error: {}", plainText); + throw new SecurityProtocolException(ErrorCode.ERR_ENCRYPT_AES256); + } + } else if (cryptoType == ProtoCryptoType.CRYPTO_DES.getCode()) { + try { + byte[] encode = CryptoHelper.desEncryption(plainText.getBytes(StandardCharsets.UTF_8), + SecurityConfigValue.DES_KEY); + cipherText = CryptoHelper.base64Encryption(encode); + } catch (Exception e) { + log.error("DES encode message error: {}", plainText); + throw new SecurityProtocolException(ErrorCode.ERR_ENCRYPT_3DES); + } + } else { + log.error("Unknown protocol security type: {}, {}", cryptoType, plainText); + throw new SecurityProtocolException(ErrorCode.ERR_ENCRYPT_UNKNOWN); + } + + return cipherText; + } + + + /** + * Encrypt protocol protocol resp dto. + * + * @param orgProtocol the org protocol + * @param cryptoType the crypto type + * @return the protocol resp dto + */ + @Override + public ProtocolRespDTO encryptProtocol(Object orgProtocol, int cryptoType) { + ProtocolRespDTO cryptoObject = new ProtocolRespDTO<>(); + cryptoObject.setVer(ConstValue.Protocol.VERSION); + cryptoObject.setCryptoType(SecurityConfigValue.SECURITY_PROTOCOL_TYPE); + cryptoObject.setTimeStamp(System.currentTimeMillis()); + + String msgContentJsonString; + + try { + Method getMsgMethod = orgProtocol.getClass().getMethod("getMsgContent"); + Method getCode = orgProtocol.getClass().getMethod("getCode"); + msgContentJsonString = new ObjectMapper().writeValueAsString(getMsgMethod.invoke(orgProtocol)); + cryptoObject.setCode((Integer) getCode.invoke(orgProtocol)); + } catch (Exception e) { + log.error("Json encode message error: {}", orgProtocol); + throw new SecurityProtocolException(ErrorCode.ERR_SYSTEMEXCEPTION); + } + + cryptoObject.setMsgContent(encryptProtocolString(msgContentJsonString, cryptoType)); + + return cryptoObject; + } +} diff --git a/src/main/java/com/cmhi/gds/validation/valids/impl/ValidBaseEnumImpl.java b/src/main/java/com/cmhi/gds/validation/valids/impl/ValidBaseEnumImpl.java index 836f3c7..2c59c57 100644 --- a/src/main/java/com/cmhi/gds/validation/valids/impl/ValidBaseEnumImpl.java +++ b/src/main/java/com/cmhi/gds/validation/valids/impl/ValidBaseEnumImpl.java @@ -11,6 +11,7 @@ import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; /** * The type Valid custom base enum. @@ -57,7 +58,7 @@ public class ValidBaseEnumImpl implements ConstraintValidator enumValues = Arrays.stream(enumClass.getEnumConstants()) - .map(BaseEnum::getValue).toList(); + .map(BaseEnum::getValue).collect(Collectors.toList()); String errMsg = this.message + " 字段 " + ((ConstraintValidatorContextImpl) ctx).getConstraintViolationCreationContexts() diff --git a/src/main/resources/mappers/GameTable.xml b/src/main/resources/mappers/GameTable.xml index c294bf6..e41f10a 100644 --- a/src/main/resources/mappers/GameTable.xml +++ b/src/main/resources/mappers/GameTable.xml @@ -7,20 +7,42 @@ - + - + + + + + + + + + + + + + + + \ No newline at end of file