From 7bb1898ba45000ca8e18519b2ee06b6429eeec27 Mon Sep 17 00:00:00 2001 From: KNKIM Date: Mon, 26 Jul 2021 10:27:03 +0900 Subject: [PATCH] =?UTF-8?q?=EC=A4=91=EA=B0=84=EB=B0=B1=EC=97=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/nlib/cmm/NlibCommonController.java | 99 ++++++++++++++++++- 1 file changed, 95 insertions(+), 4 deletions(-) diff --git a/src/main/java/nlib/cmm/NlibCommonController.java b/src/main/java/nlib/cmm/NlibCommonController.java index 55b5c029..0ec01804 100644 --- a/src/main/java/nlib/cmm/NlibCommonController.java +++ b/src/main/java/nlib/cmm/NlibCommonController.java @@ -1,5 +1,11 @@ package nlib.cmm; +import java.util.Base64; +import java.util.HashMap; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpHeaders; @@ -16,17 +22,56 @@ import nlib.restful.service.DataApiResVO; import nlib.security.SecUserVO; import nlib.user.service.NlibLoginVO; +/** + *
+ * @Class Name : NlibCommonController.java
+ * 
+ * @Description : 컨트롤러의 공통적인 기능을 제공하는 컨트롤러 상위 클래스
+ * 
+ * 
+ * @프로젝트명: 지방문화원 통합자료관리시스템 구축사업 (2021)
+ *
+ * 
+ * + * @ ------------ -------- --------------------------- + * @ 수정일 수정자 수정내용 + * @ ------------ -------- --------------------------- + * @ 2021. 7. 23. KNKIM 최초 생성 + * + * + * @author 이씨플라자 * DIGITALSHIP KNKIM + * @since 2021. 7. 23. + * @version 1.0 + * + */ public class NlibCommonController { private static final Logger log = LoggerFactory.getLogger(BoardController.class); - static final String ANONYMOUS_AUTH_KEY = "ANONYMOUS_AUTH_KEY_NONE"; + + + /** + * AuthKey 생성 시, 로그인하지 않은 사용자의 AuthKey 생성시 사용되는 접두어 + */ + static final String ANONYMOUS_AUTH_KEY_PREFIX = "GST-"; + /** + * 스프링 시큐리티 Authentication를 통해서 사용자로그인정보 NlibLoginVO를 리턴한다. + * + * @param authentication + * @return + */ public NlibLoginVO getNlibLoginVO(Authentication authentication) { if(authentication == null) return null; return (NlibLoginVO)authentication.getPrincipal(); } + /** + * 스프링 시큐리티 Authentication를 통해서 사용자로그인정보 SecUserVO를 리턴한다. + * + * @param authentication + * @return + */ public SecUserVO getSecLoginVO(Authentication authentication) { if(authentication == null) return null; @@ -34,10 +79,33 @@ public class NlibCommonController { return (SecUserVO)authentication.getPrincipal(); } - public String getAuthKey(Authentication authentication) { - if(authentication == null) return ANONYMOUS_AUTH_KEY; + /** + * 통합자료시스템 요청 시, 제공할 사용자정보 auth key 생성 + * - 로그인 한 경우 : 사용자ID를 BASE64로 인코딩한 값 + * - 로그인하지 않은 경우 : GST-세션ID + * + * @param request + * @param authentication + * @return + */ + public String createAuthKey(HttpServletRequest request, Authentication authentication) { - return ((NlibLoginVO)authentication.getPrincipal()).getAuthKey(); + String userId = null; + String authKey = null; + + if(authentication != null) { + userId = ((NlibLoginVO)authentication.getPrincipal()).getId(); + } + + if(nlib.util.StringUtil.isEmpty(userId)) { + authKey = ANONYMOUS_AUTH_KEY_PREFIX + request.getSession().getId(); + } else { + authKey = Base64.getEncoder().encodeToString(userId.getBytes()); + } + + log.debug("createAuthKey > " + authKey); + + return authKey; } public ResponseEntity makeResponseEntityJson(DataApiResVO resVO) { @@ -61,4 +129,27 @@ public class NlibCommonController { return ResponseEntity.ok().headers(headers).body(jsonStr); } + + + public ResponseEntity makeResponseEntityJson(List> list) { + + // Convert Json + ObjectMapper mapper = new ObjectMapper(); + String jsonStr = null; + + HttpHeaders headers = new HttpHeaders(); + headers.add(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8"); + + try { + jsonStr = mapper.writeValueAsString(list); + log.debug("RETURN DATA > json:" + jsonStr); + } catch (JsonProcessingException e) { + ErrorMessage eMsg = new ErrorMessage("ERR_JSON_CONVERT", "응답객체를 JSON으로 변환 처리 중 오류가 발생하였습니다. : " + e.toString()); + try { jsonStr = mapper.writeValueAsString(eMsg); } catch (JsonProcessingException ee) {} + e.printStackTrace(); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).headers(headers).body(jsonStr); + } + + return ResponseEntity.ok().headers(headers).body(jsonStr); + } }