|
@@ -0,0 +1,174 @@
|
|
|
|
|
+package com.style24.core.support.util;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.regex.Matcher;
|
|
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
|
+
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 개인정보 마스킹 처리 Util Class
|
|
|
|
|
+ * @author gagamel
|
|
|
|
|
+ * @since 2020. 12. 29
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class MaskingUtils {
|
|
|
|
|
+
|
|
|
|
|
+ private static final char DEFAULT_REPLACE = '*';
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * ID 3자 이후 전체 마스킹 처리
|
|
|
|
|
+ * <pre>
|
|
|
|
|
+ * 예)
|
|
|
|
|
+ * abc1234 => abc****
|
|
|
|
|
+ * </pre>
|
|
|
|
|
+ * @param str - 입력문자열
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String id(String str) {
|
|
|
|
|
+ String replaceString = str;
|
|
|
|
|
+
|
|
|
|
|
+ Matcher matcher = Pattern.compile("^(...)(.*)$").matcher(str);
|
|
|
|
|
+
|
|
|
|
|
+ if (matcher.matches()) {
|
|
|
|
|
+ replaceString = "";
|
|
|
|
|
+ int gCnt = matcher.groupCount();
|
|
|
|
|
+
|
|
|
|
|
+ for (int i = 1; i <= gCnt; i++) {
|
|
|
|
|
+ String replaceTarget = matcher.group(i);
|
|
|
|
|
+
|
|
|
|
|
+ if (i == 2) {
|
|
|
|
|
+ char[] c = new char[replaceTarget.length()];
|
|
|
|
|
+ Arrays.fill(c, DEFAULT_REPLACE);
|
|
|
|
|
+ replaceString = replaceString + String.valueOf(c);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ replaceString = replaceString + replaceTarget;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return replaceString;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 이름 첫글자와 마지막글자 외 모든 글자 마스킹 처리
|
|
|
|
|
+ * <pre>
|
|
|
|
|
+ * 예)
|
|
|
|
|
+ * 환희 => 환*
|
|
|
|
|
+ * 홍길동 => 홍*동
|
|
|
|
|
+ * 남궁길동 => 남**동
|
|
|
|
|
+ * </pre>
|
|
|
|
|
+ * @param str - 입력문자열
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String name(String str) {
|
|
|
|
|
+ String regex = "^(.)(.+)(.)$";
|
|
|
|
|
+ if (str.length() == 2) {
|
|
|
|
|
+ regex = "^(.)(.+)$";
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Matcher matcher = Pattern.compile(regex).matcher(str);
|
|
|
|
|
+
|
|
|
|
|
+ if (matcher.find()) {
|
|
|
|
|
+ String replaceTarget = matcher.group(2);
|
|
|
|
|
+ char[] c = new char[replaceTarget.length()];
|
|
|
|
|
+ Arrays.fill(c, DEFAULT_REPLACE);
|
|
|
|
|
+ return str.replace(replaceTarget, String.valueOf(c));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return str;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 성별 전체 문자열로 마스킹 처리
|
|
|
|
|
+ * <pre>
|
|
|
|
|
+ * 예)
|
|
|
|
|
+ * 남 => *
|
|
|
|
|
+ * </pre>
|
|
|
|
|
+ * @param str - 입력문자열
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String sex(String str) {
|
|
|
|
|
+ return all(str);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 전화번호 중간번호 4자리 마스킹 처리
|
|
|
|
|
+ * <pre>
|
|
|
|
|
+ * 예)
|
|
|
|
|
+ * 010-1234-5678 => 010-****-5678
|
|
|
|
|
+ * 01012345678 => 010****5678
|
|
|
|
|
+ * </pre>
|
|
|
|
|
+ * @param str - 입력문자열
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String phoneNo(String str) {
|
|
|
|
|
+ Matcher matcher = Pattern.compile("^(\\d{3})-?(\\d{3,4})-?(\\d{4})$").matcher(str);
|
|
|
|
|
+
|
|
|
|
|
+ if (matcher.find()) {
|
|
|
|
|
+ String replaceTarget = matcher.group(2);
|
|
|
|
|
+ char[] c = new char[replaceTarget.length()];
|
|
|
|
|
+ Arrays.fill(c, DEFAULT_REPLACE);
|
|
|
|
|
+ return str.replace(replaceTarget, String.valueOf(c));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return str;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 이메일 3자 ~ @전 아이디에 대해 마스킹 처리
|
|
|
|
|
+ * <pre>
|
|
|
|
|
+ * 예) abc123@style24.com => abc***@style24.com
|
|
|
|
|
+ * </pre>
|
|
|
|
|
+ * @param str - 입력문자열
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String email(String str) {
|
|
|
|
|
+ Matcher matcher = Pattern.compile("^(...)(.*)([@]{1})(.*)$").matcher(str);
|
|
|
|
|
+
|
|
|
|
|
+ if (matcher.find()) {
|
|
|
|
|
+ String replaceTarget = matcher.group(2);
|
|
|
|
|
+ char[] c = new char[replaceTarget.length()];
|
|
|
|
|
+ Arrays.fill(c, DEFAULT_REPLACE);
|
|
|
|
|
+ return str.replace(replaceTarget, String.valueOf(c));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return str;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 상세주소 전체 문자열로 마스킹 처리
|
|
|
|
|
+ * <pre>
|
|
|
|
|
+ * 예)
|
|
|
|
|
+ * 일신빌딩 5,6층 => *********
|
|
|
|
|
+ * </pre>
|
|
|
|
|
+ * @param str - 입력문자열
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String address(String str) {
|
|
|
|
|
+ return all(str);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 전체 문자열 마스킹 처리
|
|
|
|
|
+ * <pre>
|
|
|
|
|
+ * 예)
|
|
|
|
|
+ * 스타일24 프로젝트 마스킹 => ***************
|
|
|
|
|
+ * </pre>
|
|
|
|
|
+ * @param str - 입력문자열
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String all(String str) {
|
|
|
|
|
+ Matcher matcher = Pattern.compile("^(.*)$").matcher(str);
|
|
|
|
|
+
|
|
|
|
|
+ if (matcher.find()) {
|
|
|
|
|
+ String replaceTarget = matcher.group(1);
|
|
|
|
|
+ char[] c = new char[replaceTarget.length()];
|
|
|
|
|
+ Arrays.fill(c, DEFAULT_REPLACE);
|
|
|
|
|
+ return str.replace(replaceTarget, String.valueOf(c));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return str;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|