diff --git a/mens-api/src/main/java/kr/xit/biz/nice/service/BizNiceCiService.java b/mens-api/src/main/java/kr/xit/biz/nice/service/BizNiceCiService.java index 8caed27..bf26db1 100644 --- a/mens-api/src/main/java/kr/xit/biz/nice/service/BizNiceCiService.java +++ b/mens-api/src/main/java/kr/xit/biz/nice/service/BizNiceCiService.java @@ -162,7 +162,7 @@ public class BizNiceCiService extends EgovAbstractServiceImpl implements IBizNic public PublickeyResponse requestPublickey(final NiceCiRequest reqDTO) { final NiceCiInfo niceDTO = CmmNiceCiUtils.getNiceCiInfo(reqDTO); if(ObjectUtils.isEmpty(niceDTO.getAccessToken())) throw BizRuntimeException.create(messageSource.getMessage("fail.api.nice.token.info")); - reqDTO.setTranId(CmmEnsUtils.generateLengthUuid(24)); + final PublickeyResponse pubResDTO = niceCiService.requestPublickey(reqDTO); //FIXME::공개키 정보 갱신 - Error code enum 전환 @@ -228,10 +228,8 @@ public class BizNiceCiService extends EgovAbstractServiceImpl implements IBizNic final String ffnlgCode = reqDTO.getFfnlgCode(); //FIXME::NICE 연계이후 메소드 교체 - final NiceCiInfo niceDTO = CmmNiceCiUtils.getPublickeyNiceCiInfo(reqDTO); - final SymkeyRegInfo symkeyRegInfo = CmmNiceCiUtils.getSymkeyRegInfo(niceDTO.getSiteCode()); - - SymmetrickeyResponse symResDTO = niceCiService.requestRegSymmetrickey(CmmEnsUtils.generateLengthUuid(24), symkeyRegInfo, niceDTO); + final SymkeyRegInfo symkeyRegInfo = CmmNiceCiUtils.getSymkeyRegInfo(); + SymmetrickeyResponse symResDTO = niceCiService.requestRegSymmetrickey(reqDTO, symkeyRegInfo); final ResponseDataHeader dataHeader = symResDTO.getDataHeader(); final SymmetrickeyResDataBody dataBody = symResDTO.getDataBody(); @@ -240,7 +238,10 @@ public class BizNiceCiService extends EgovAbstractServiceImpl implements IBizNic //FIXME:: "0007" 인 경우 확인 필요 - result_cd 0000, 0007일 경우 나감 (0007: key 중복 오류 (현재 및 직전에 사용한 Key 사용 불가) && dataBody.getResultCd().equals("0000")){ SymkeyStatInfo symkeyStatInfo = JsonUtils.toObject(dataBody.getSymkeyStatInfo(), SymkeyStatInfo.class); - + NiceCiInfo niceDTO = NiceCiInfo.builder() + .signguCode(reqDTO.getSignguCode()) + .ffnlgCode(reqDTO.getFfnlgCode()) + .build(); niceDTO.setBefSymkeyVersion(symkeyStatInfo.getBefSymkeyVersion()); niceDTO.setBefSymkeyValidDtim(symkeyStatInfo.getBefValidDtim()); niceDTO.setBefSymkeyKey(niceDTO.getCurSymkeyKey()); diff --git a/mens-api/src/main/java/kr/xit/core/spring/util/ApiSpringUtils.java b/mens-api/src/main/java/kr/xit/core/spring/util/ApiSpringUtils.java index e7c9aa5..a8d640a 100644 --- a/mens-api/src/main/java/kr/xit/core/spring/util/ApiSpringUtils.java +++ b/mens-api/src/main/java/kr/xit/core/spring/util/ApiSpringUtils.java @@ -2,10 +2,10 @@ package kr.xit.core.spring.util; import kr.xit.biz.cmm.service.CmmEnsCacheService; import kr.xit.biz.cmm.service.ICmmEnsCacheService; +import kr.xit.biz.nice.service.IBizNiceCiService; import kr.xit.core.spring.config.support.ApplicationContextProvider; import kr.xit.ens.kt.service.IKtMmsService; import kr.xit.ens.nice.service.INiceCiService; -import kr.xit.ens.nice.service.NiceCiDummyTestService; import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.springframework.context.ApplicationContext; @@ -54,11 +54,11 @@ public class ApiSpringUtils { return (INiceCiService)getBean(INiceCiService.class); } - public static IKtMmsService getKtMmsService(){ - return (IKtMmsService)getBean(IKtMmsService.class); + public static IBizNiceCiService getBizNiceCiService(){ + return (IBizNiceCiService)getBean(IBizNiceCiService.class); } - public static NiceCiDummyTestService getDummyNiceCiService(){ - return (NiceCiDummyTestService)getBean(NiceCiDummyTestService.class); + public static IKtMmsService getKtMmsService(){ + return (IKtMmsService)getBean(IKtMmsService.class); } } diff --git a/mens-api/src/main/java/kr/xit/ens/cmm/CmmEnsUtils.java b/mens-api/src/main/java/kr/xit/ens/cmm/CmmEnsUtils.java index 9e898f6..c536d00 100644 --- a/mens-api/src/main/java/kr/xit/ens/cmm/CmmEnsUtils.java +++ b/mens-api/src/main/java/kr/xit/ens/cmm/CmmEnsUtils.java @@ -4,8 +4,11 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; +import java.security.KeyFactory; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.X509EncodedKeySpec; import java.util.Base64; import java.util.List; import java.util.Locale; @@ -27,6 +30,7 @@ import javax.validation.Validator; import kr.xit.core.exception.BizRuntimeException; import lombok.AccessLevel; import lombok.NoArgsConstructor; +import org.springframework.util.Base64Utils; /** *
@@ -75,6 +79,44 @@ public class CmmEnsUtils {
         return String.valueOf(otp);
     }
 
+    /**
+     * 공개키로 암호화를 수행
+     *
+     * @param publicKeyString
+     * @param symkeyRegInfo
+     * @return String
+     * @throws NoSuchAlgorithmException
+     * @throws InvalidKeySpecException
+     * @throws NoSuchPaddingException
+     * @throws InvalidKeyException
+     * @throws IllegalBlockSizeException
+     * @throws BadPaddingException
+     */
+    public static String encSymkeyRegInfo(String publicKeyString, String symkeyRegInfo)  {
+        try {
+            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
+            byte[] cipherEnc = Base64.getDecoder().decode(publicKeyString);
+            X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(cipherEnc);
+            java.security.PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
+
+            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
+            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
+            byte[] bytePlain = cipher.doFinal(symkeyRegInfo.getBytes());
+
+            return Base64Utils.encodeToString(bytePlain);
+        } catch (NoSuchAlgorithmException | InvalidKeySpecException e){
+            throw BizRuntimeException.create(e.getMessage());
+        } catch (NoSuchPaddingException e) {
+            throw BizRuntimeException.create(e.getMessage());
+        } catch (IllegalBlockSizeException e) {
+            throw BizRuntimeException.create(e.getMessage());
+        } catch (BadPaddingException e) {
+            throw BizRuntimeException.create(e.getMessage());
+        } catch (InvalidKeyException e) {
+            throw BizRuntimeException.create(e.getMessage());
+        }
+    }
+
     /**
      * sha256 암호화
      *
diff --git a/mens-api/src/main/java/kr/xit/ens/nice/cmm/CmmNiceCiUtils.java b/mens-api/src/main/java/kr/xit/ens/nice/cmm/CmmNiceCiUtils.java
index 8dbe5f1..dde49e8 100644
--- a/mens-api/src/main/java/kr/xit/ens/nice/cmm/CmmNiceCiUtils.java
+++ b/mens-api/src/main/java/kr/xit/ens/nice/cmm/CmmNiceCiUtils.java
@@ -11,6 +11,7 @@ import kr.xit.biz.ens.model.nice.NiceCiDTO.NiceCiRequest;
 import kr.xit.biz.ens.model.nice.NiceCiDTO.ResponseDataHeader;
 import kr.xit.biz.ens.model.nice.NiceCiDTO.SymkeyRegInfo;
 import kr.xit.biz.ens.model.nice.NiceCiDTO.TokenRevokeResponse;
+import kr.xit.biz.nice.service.IBizNiceCiService;
 import kr.xit.core.exception.BizRuntimeException;
 import kr.xit.core.spring.util.ApiSpringUtils;
 import kr.xit.core.spring.util.CoreSpringUtils;
@@ -45,6 +46,7 @@ public class CmmNiceCiUtils {
     private static final EgovMessageSource messageSource = CoreSpringUtils.getMessageSource();
     private static final ICmmEnsCacheService cacheService = ApiSpringUtils.getCmmEnsCacheService();
     private static final INiceCiService niceCiService = ApiSpringUtils.getNiceCiService();
+    private static final IBizNiceCiService bizNiceService = ApiSpringUtils.getBizNiceCiService();
 
     /**
      * Nice 인증 정보 조회
@@ -76,15 +78,14 @@ public class CmmNiceCiUtils {
         if(DateUtils.getTodayAndNowTime("yyyyMMddHHmmss").compareTo(niceDTO.getValidDtim()) < 0)     return niceDTO;
 
         // 1. 토큰 폐기
-        TokenRevokeResponse revokeResDTO = niceCiService.revokeToken(reqDTO);
+        TokenRevokeResponse revokeResDTO = bizNiceService.revokeToken(reqDTO);
 
         if(!(revokeResDTO.getDataHeader().getGwRsltCd().equals("1200") && revokeResDTO.getDataBody().isResult())){
             throw BizRuntimeException.create(JsonUtils.toJson(revokeResDTO.getDataHeader()));
         }
 
-        // 2. 공개키 발급 요청
-        reqDTO.setTranId(CmmEnsUtils.generateLengthUuid(24));
-        niceCiService.requestPublickey(reqDTO);
+        // 2. 공개키 발급 처리
+        bizNiceService.requestPublickey(reqDTO);
 
         return cacheService.getNiceCiInfoCache(reqDTO.getSignguCode(), reqDTO.getFfnlgCode());
     }
@@ -103,8 +104,8 @@ public class CmmNiceCiUtils {
         final NiceCiInfo niceDTO = getPublickeyNiceCiInfo(reqDTO);
         if(DateUtils.getTodayAndNowTime("yyyyMMddHHmmss").compareTo(niceDTO.getCurSymkeyValidDtim()) < 0)   return niceDTO;
 
-        // 1. 토큰 폐기
-        niceCiService.requestRegSymmetrickey(CmmEnsUtils.generateLengthUuid(24), null, niceDTO);
+        // 대칭키 발행 등록
+        bizNiceService.requestRegSymmetrickey(reqDTO);
 
         return cacheService.getNiceCiInfoCache(reqDTO.getSignguCode(), reqDTO.getFfnlgCode());
     }
@@ -112,13 +113,13 @@ public class CmmNiceCiUtils {
     /**
      * 
      * 대칭키(symmetrickey) 등록 요청시 symkey_reg_info JSON 데이타 생성
-     * @param siteCode
-     * @return
+     * -> siteCode는 DB 정보 set
+     * @return SymkeyRegInfo
      * 
*/ - public static SymkeyRegInfo getSymkeyRegInfo(String siteCode) { + public static SymkeyRegInfo getSymkeyRegInfo() { return SymkeyRegInfo.builder() - .siteCode(siteCode) + //.siteCode() .requestNo(CmmEnsUtils.generateLengthUuid(30)) .key(CmmEnsUtils.generateLengthUuid(32)) .iv(CmmEnsUtils.generateLengthUuid(16)) @@ -154,8 +155,8 @@ public class CmmNiceCiUtils { *
*/ public static T checkTranIdAndReturn(final String tranId, final ResponseDataHeader resHeader, T t){ - if(!resHeader.getGwRsltCd().equals("1200")) return t; if(!tranId.equals(resHeader.getTranId())) throw BizRuntimeException.create(messageSource.getMessage("fail.api.nice.invalid.tranId")); + if(!resHeader.getGwRsltCd().equals("1200")) return t; return t; } } diff --git a/mens-api/src/main/java/kr/xit/ens/nice/service/INiceCiService.java b/mens-api/src/main/java/kr/xit/ens/nice/service/INiceCiService.java index baadb1f..e595732 100644 --- a/mens-api/src/main/java/kr/xit/ens/nice/service/INiceCiService.java +++ b/mens-api/src/main/java/kr/xit/ens/nice/service/INiceCiService.java @@ -4,10 +4,10 @@ import kr.xit.biz.ens.model.nice.NiceCiDTO.IpinCiReqEncData; import kr.xit.biz.ens.model.nice.NiceCiDTO.IpinCiResponse; import kr.xit.biz.ens.model.nice.NiceCiDTO.NiceCiInfo; import kr.xit.biz.ens.model.nice.NiceCiDTO.NiceCiRequest; +import kr.xit.biz.ens.model.nice.NiceCiDTO.NiceTokenResponse; import kr.xit.biz.ens.model.nice.NiceCiDTO.PublickeyResponse; import kr.xit.biz.ens.model.nice.NiceCiDTO.SymkeyRegInfo; import kr.xit.biz.ens.model.nice.NiceCiDTO.SymmetrickeyResponse; -import kr.xit.biz.ens.model.nice.NiceCiDTO.NiceTokenResponse; import kr.xit.biz.ens.model.nice.NiceCiDTO.TokenRevokeResponse; /** @@ -46,7 +46,8 @@ public interface INiceCiService { //-------------------------------------------------------------------------------- // 대칭키 : symmetrickey //-------------------------------------------------------------------------------- - SymmetrickeyResponse requestRegSymmetrickey(final String tranId, final SymkeyRegInfo symkeyRegInfo, final NiceCiInfo niceDTO); + SymmetrickeyResponse requestRegSymmetrickey(final NiceCiRequest reqDTO, final + SymkeyRegInfo symkeyRegInfo); //-------------------------------------------------------------------------------- // 대칭키 : symmetrickey //-------------------------------------------------------------------------------- diff --git a/mens-api/src/main/java/kr/xit/ens/nice/service/NiceCiDummyTestService.java b/mens-api/src/main/java/kr/xit/ens/nice/service/NiceCiDummyTestService.java index bf45054..5c3c138 100644 --- a/mens-api/src/main/java/kr/xit/ens/nice/service/NiceCiDummyTestService.java +++ b/mens-api/src/main/java/kr/xit/ens/nice/service/NiceCiDummyTestService.java @@ -138,7 +138,7 @@ public class NiceCiDummyTestService extends EgovAbstractServiceImpl { //-------------------------------------------------------------------------------- public SymmetrickeyResponse requestRegSymmetrickey(final NiceCiRequest reqDTO) { final NiceCiInfo niceDTO = CmmNiceCiUtils.getPublickeyNiceCiInfo(reqDTO); - final SymkeyRegInfo symkeyRegInfo = CmmNiceCiUtils.getSymkeyRegInfo(niceDTO.getSiteCode()); + final SymkeyRegInfo symkeyRegInfo = CmmNiceCiUtils.getSymkeyRegInfo(); //FIXME::NICE 연계이후 comment 제거 //String encSymkeyRegInfo = encodeString(niceDTO.getPublicKey(), JsonUtils.toJson(symkeyRegInfo)); diff --git a/mens-api/src/main/java/kr/xit/ens/nice/service/NiceCiService.java b/mens-api/src/main/java/kr/xit/ens/nice/service/NiceCiService.java index e0afcde..09ca5b4 100644 --- a/mens-api/src/main/java/kr/xit/ens/nice/service/NiceCiService.java +++ b/mens-api/src/main/java/kr/xit/ens/nice/service/NiceCiService.java @@ -4,12 +4,14 @@ import egovframework.com.cmm.EgovMessageSource; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; -import kr.xit.biz.ens.model.nice.NiceCiDTO.NiceCiInfo; import kr.xit.biz.ens.model.nice.NiceCiDTO.IpinCiReqDataBody; import kr.xit.biz.ens.model.nice.NiceCiDTO.IpinCiReqEncData; import kr.xit.biz.ens.model.nice.NiceCiDTO.IpinCiRequest; import kr.xit.biz.ens.model.nice.NiceCiDTO.IpinCiResponse; +import kr.xit.biz.ens.model.nice.NiceCiDTO.NiceCiInfo; import kr.xit.biz.ens.model.nice.NiceCiDTO.NiceCiRequest; +import kr.xit.biz.ens.model.nice.NiceCiDTO.NiceTokenRequest; +import kr.xit.biz.ens.model.nice.NiceCiDTO.NiceTokenResponse; import kr.xit.biz.ens.model.nice.NiceCiDTO.PublickeyReqDataBody; import kr.xit.biz.ens.model.nice.NiceCiDTO.PublickeyRequest; import kr.xit.biz.ens.model.nice.NiceCiDTO.PublickeyResponse; @@ -18,8 +20,6 @@ import kr.xit.biz.ens.model.nice.NiceCiDTO.SymkeyRegInfo; import kr.xit.biz.ens.model.nice.NiceCiDTO.SymmetrickeyReqDataBody; import kr.xit.biz.ens.model.nice.NiceCiDTO.SymmetrickeyRequest; import kr.xit.biz.ens.model.nice.NiceCiDTO.SymmetrickeyResponse; -import kr.xit.biz.ens.model.nice.NiceCiDTO.NiceTokenRequest; -import kr.xit.biz.ens.model.nice.NiceCiDTO.NiceTokenResponse; import kr.xit.biz.ens.model.nice.NiceCiDTO.TokenRevokeResponse; import kr.xit.core.exception.BizRuntimeException; import kr.xit.core.spring.annotation.TraceLogging; @@ -158,10 +158,11 @@ public class NiceCiService extends EgovAbstractServiceImpl implements INiceCiSer final String todayDt = DateUtils.getTodayAndNowTime("yyyyMMddHHmmss"); + final String tranId = CmmEnsUtils.generateLengthUuid(24); final PublickeyRequest pubReqDTO = PublickeyRequest.builder() .dataHeader(RequestDataHeader.builder() .cntyId(CNTY_CD) - .tranId(reqDTO.getTranId()) + .tranId(tranId) .build()) .dataBody(PublickeyReqDataBody.builder() .reqDtim(todayDt) @@ -184,7 +185,7 @@ public class NiceCiService extends EgovAbstractServiceImpl implements INiceCiSer headerMap ); - return CmmNiceCiUtils.checkTranIdAndReturn(reqDTO.getTranId(), pubResDTO.getDataHeader(), pubResDTO); + return CmmNiceCiUtils.checkTranIdAndReturn(tranId, pubResDTO.getDataHeader(), pubResDTO); } //-------------------------------------------------------------------------------- @@ -205,12 +206,15 @@ public class NiceCiService extends EgovAbstractServiceImpl implements INiceCiSer * */ @Override - public SymmetrickeyResponse requestRegSymmetrickey(final String tranId, final SymkeyRegInfo symkeyRegInfo, final NiceCiInfo niceDTO) { + public SymmetrickeyResponse requestRegSymmetrickey(final NiceCiRequest reqDTO, final SymkeyRegInfo symkeyRegInfo) { + final NiceCiInfo niceDTO = CmmNiceCiUtils.getPublickeyNiceCiInfo(reqDTO); + // siteCode set + symkeyRegInfo.setSiteCode(niceDTO.getSiteCode()); //FIXME::NICE 연계이후 comment 제거 - final String encSymkeyRegInfo = CmmEnsUtils.encodeHmacSha256(niceDTO.getPublicKey(), JsonUtils.toJson(symkeyRegInfo)); - - final SymmetrickeyRequest reqDTO = SymmetrickeyRequest.builder() + final String encSymkeyRegInfo = CmmEnsUtils.encSymkeyRegInfo(niceDTO.getPublicKey(), JsonUtils.toJson(symkeyRegInfo)); + final String tranId = CmmEnsUtils.generateLengthUuid(24); + final SymmetrickeyRequest symReqDTO = SymmetrickeyRequest.builder() .dataHeader(RequestDataHeader.builder() .cntyId(CNTY_CD) .tranId(tranId) @@ -220,7 +224,7 @@ public class NiceCiService extends EgovAbstractServiceImpl implements INiceCiSer .symkeyRegInfo(encSymkeyRegInfo) .build()) .build(); - CmmEnsUtils.validate(reqDTO); + CmmEnsUtils.validate(symReqDTO); final Map headerMap = CmmNiceCiUtils.getAuthHeaderMap( MediaType.APPLICATION_JSON_VALUE, AUTH_TYPE_BEARER, @@ -232,7 +236,7 @@ public class NiceCiService extends EgovAbstractServiceImpl implements INiceCiSer final SymmetrickeyResponse symResDTO = webClient.exchange( HOST + API_SYMMETRICKEY, HttpMethod.POST, - reqDTO, + symReqDTO, SymmetrickeyResponse.class, headerMap ); diff --git a/mens-api/src/main/java/kr/xit/ens/nice/web/NiceCiController.java b/mens-api/src/main/java/kr/xit/ens/nice/web/NiceCiController.java index 430b78e..f75a3c9 100644 --- a/mens-api/src/main/java/kr/xit/ens/nice/web/NiceCiController.java +++ b/mens-api/src/main/java/kr/xit/ens/nice/web/NiceCiController.java @@ -69,7 +69,6 @@ public class NiceCiController { @Operation(deprecated = true, summary = "공개키 요청 -> 업무(Biz)단 API 사용", description = "NICE 정보 DB update가 필요하여 업무단의 API를 사용하여야 함") @PostMapping(value = "/requestPublickey", produces = MediaType.APPLICATION_JSON_VALUE) public ApiResponseDTO requestPublickey(@RequestBody final NiceCiRequest reqDTO) { - reqDTO.setTranId(CmmEnsUtils.generateLengthUuid(24)); return ApiResponseDTO.success(service.requestPublickey(reqDTO)); } //-------------------------------------------------------------------------------- @@ -82,13 +81,7 @@ public class NiceCiController { @Operation(deprecated = true, summary = "대칭키(symmetrickey) 등록 요청 -> 업무(Biz)단 API 사용", description = "NICE 정보 DB update가 필요하여 업무단의 API를 사용하여야 함") @PostMapping(value = "/requestRegSymmetrickey", produces = MediaType.APPLICATION_JSON_VALUE) public ApiResponseDTO requestRegSymmetrickey(@RequestBody final NiceCiRequest reqDTO) { - return ApiResponseDTO.success( - service.requestRegSymmetrickey( - CmmEnsUtils.generateLengthUuid(24), - null, - CmmNiceCiUtils.getPublickeyNiceCiInfo(reqDTO) - ) - ); + return ApiResponseDTO.success(service.requestRegSymmetrickey(reqDTO, CmmNiceCiUtils.getSymkeyRegInfo())); } //-------------------------------------------------------------------------------- diff --git a/mens-core/src/main/java/kr/xit/biz/ens/model/nice/NiceCiDTO.java b/mens-core/src/main/java/kr/xit/biz/ens/model/nice/NiceCiDTO.java index 12ae157..74d4108 100644 --- a/mens-core/src/main/java/kr/xit/biz/ens/model/nice/NiceCiDTO.java +++ b/mens-core/src/main/java/kr/xit/biz/ens/model/nice/NiceCiDTO.java @@ -63,9 +63,9 @@ public class NiceCiDTO { * 고유번호 : 최대 24 * */ - @Schema(requiredMode = RequiredMode.AUTO, title = "TRAN_ID", example = "20230906120000") - @Size(max = 24, message = "TRAN_ID는 24자를 넘을 수 없습니다.") - private String tranId;; +// @Schema(requiredMode = RequiredMode.AUTO, title = "TRAN_ID", example = "20230906120000") +// @Size(max = 24, message = "TRAN_ID는 24자를 넘을 수 없습니다.") +// private String tranId;; /** * 주민Id