Browse Source

앱로그인 api 추가

dwkim 2 years ago
parent
commit
5fc7f88e02

+ 1 - 0
src/main/java/com/oqpo/api/config/WebMvcConfig.java

@@ -34,6 +34,7 @@ public class WebMvcConfig implements WebMvcConfigurer {
34 34
             public void addInterceptors(InterceptorRegistry registry) {
35 35
                 registry.addInterceptor(tokenCheckInterceptor).addPathPatterns("/api/**")
36 36
                         .excludePathPatterns("/api/member/sign-in")
37
+                        .excludePathPatterns("/api/member/sign-in-app")
37 38
                         .excludePathPatterns("/api/member/sign-in-swagger")
38 39
                         .excludePathPatterns("/api/session/hashed")
39 40
                         .excludePathPatterns("/api/session/expire")

+ 49 - 0
src/main/java/com/oqpo/api/util/CryptoUtil.java

@@ -8,7 +8,9 @@ import javax.crypto.Cipher;
8 8
 import javax.crypto.spec.IvParameterSpec;
9 9
 import javax.crypto.spec.SecretKeySpec;
10 10
 
11
+import org.apache.commons.codec.binary.Hex;
11 12
 import org.apache.tomcat.util.codec.binary.Base64;
13
+import org.springframework.util.Assert;
12 14
 
13 15
 public class CryptoUtil {
14 16
 	public static String decrypt(String ciphertext, String passphrase) {
@@ -61,4 +63,51 @@ public class CryptoUtil {
61 63
         System.arraycopy(derivedBytes, keySize * 4, resultIv, 0, ivSize * 4);
62 64
         return derivedBytes;
63 65
     }
66
+
67
+
68
+    /**
69
+     * AES, CBC, PKCS5Padding, encodeHexString
70
+     * Key 길이가 128, 256 이냐에 따라 알고리즘은 AES-128, AES-256 이 됨
71
+     */
72
+    public static String doDecrypt(String encryptString, String strKey) {
73
+        Assert.notNull(encryptString, "The encryptString must not be null!");
74
+        Assert.notNull(strKey, "The strKey must not be null!");
75
+
76
+        if ("".equals(encryptString)) {
77
+            return "";
78
+        }
79
+
80
+        SecretKeySpec key = new SecretKeySpec(strKey.getBytes(), "AES");
81
+
82
+        try {
83
+            // Hash 변환
84
+            byte[] encryptCombineBytes = Hex.decodeHex(encryptString.toCharArray());
85
+
86
+            int encryptSize = encryptCombineBytes.length;
87
+            // 암호화 문자열에 iv 가 포함되어 있지 않을 경우 0.
88
+            int ivSize = 0;
89
+            byte[] ivBytes = new byte[16];
90
+            System.arraycopy(encryptCombineBytes, 0, ivBytes, 0, ivBytes.length);
91
+            encryptSize -= 16;
92
+            // 암호화 문자열에 iv 가 포함되어 있을 경우 IV_SIZE
93
+            ivSize = 16;
94
+
95
+            IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
96
+
97
+            // encryptBytes 분리
98
+            byte[] encryptBytes = new byte[encryptSize];
99
+            System.arraycopy(encryptCombineBytes, ivSize, encryptBytes, 0, encryptSize);
100
+
101
+            // Decrypt
102
+            Cipher cipherDecrypt = Cipher.getInstance("AES/CBC/PKCS5Padding");
103
+            cipherDecrypt.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
104
+            byte[] decryptBytes = cipherDecrypt.doFinal(encryptBytes);
105
+
106
+            String decryptResultString = new String(decryptBytes);
107
+
108
+            return decryptResultString;
109
+        } catch (Exception ex) {
110
+            throw new RuntimeException("Decrypt Exception!", ex);
111
+        }
112
+    }
64 113
 }

+ 16 - 0
src/main/java/com/oqpo/api/web/controller/SigninController.java

@@ -54,6 +54,22 @@ public class SigninController {
54 54
         return ResponseEntity.ok(signinService.signin(userId, password));
55 55
     }
56 56
 
57
+    /**
58
+     * 설명 : APP 로그인
59
+     *
60
+     * @param signinRequest
61
+     * @return
62
+     * @throws Exception
63
+     */
64
+    @ApiOperation(value = "로그인")
65
+    @PostMapping("/sign-in-app")
66
+    public ResponseEntity<JwtResponse> signinApp(@RequestBody @Valid SigninRequest signinRequest) throws Exception {
67
+        hashed = StringEscapeUtils.unescapeJava(hashed);
68
+        String userId = CryptoUtil.doDecrypt(signinRequest.getUserId(), hashed);
69
+        String password = CryptoUtil.doDecrypt(signinRequest.getPassword(), hashed);
70
+        return ResponseEntity.ok(signinService.signin(userId, password));
71
+    }
72
+
57 73
     /**
58 74
      * 설명 : Swagger 로그인
59 75
      *