Skip to content

Commit 449b4a4

Browse files
committed
feature : github AccessToken AES 암호화 후 저장,암호화를 담당하는 AESUtil 생성
1 parent f1d943a commit 449b4a4

File tree

5 files changed

+56
-9
lines changed

5 files changed

+56
-9
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ dependencies {
110110
//email-smtp
111111
implementation 'org.springframework.boot:spring-boot-starter-mail'
112112

113-
114-
}
113+
//AES 암호화
114+
implementation 'javax.xml.bind:jaxb-api:2.3.1'}
115115

116116
tasks.named('test') {
117117
useJUnitPlatform()

src/main/java/org/ezcode/codetest/common/security/hander/CustomSuccessHandler.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import java.util.concurrent.TimeUnit;
55

66
import org.ezcode.codetest.application.usermanagement.auth.dto.response.OAuthResponse;
7+
import org.ezcode.codetest.common.security.util.AESUtil;
8+
import org.ezcode.codetest.domain.user.exception.AuthException;
9+
import org.ezcode.codetest.domain.user.exception.code.AuthExceptionCode;
710
import org.ezcode.codetest.domain.user.model.entity.CustomOAuth2User;
811
import org.ezcode.codetest.domain.user.model.entity.User;
912
import org.ezcode.codetest.domain.user.service.UserDomainService;
@@ -32,15 +35,17 @@ public class CustomSuccessHandler extends SimpleUrlAuthenticationSuccessHandler
3235
private final RedisTemplate<String, String> redisTemplate;
3336
private final ObjectMapper objectMapper; //json직렬화
3437
private final OAuth2AuthorizedClientService authorizedClientService;
38+
private final AESUtil aesUtil;
3539

3640
public CustomSuccessHandler(JwtUtil jwtUtil, UserDomainService userDomainService,
3741
RedisTemplate<String, String> redisTemplate, ObjectMapper objectMapper,
38-
OAuth2AuthorizedClientService authorizedClientService) {
42+
OAuth2AuthorizedClientService authorizedClientService, AESUtil aesUtil) {
3943
this.jwtUtil = jwtUtil;
4044
this.userDomainService = userDomainService;
4145
this.redisTemplate = redisTemplate;
4246
this.objectMapper = objectMapper;
4347
this.authorizedClientService = authorizedClientService;
48+
this.aesUtil = aesUtil;
4449
}
4550

4651
@Override
@@ -53,18 +58,22 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
5358
User loginUser= userDomainService.getUserByEmail(customUserDetails.getEmail());
5459
log.info("loginUser Name: {}", loginUser.getUsername());
5560

56-
log.info("provider : {}", customUserDetails.getProvider().toString());
5761
if (customUserDetails.getProvider().equalsIgnoreCase("github")) {
5862
//깃허브 access-token 가져오기
59-
6063
OAuth2AuthenticationToken oauthToken = (OAuth2AuthenticationToken) authentication;
6164
OAuth2AuthorizedClient client = authorizedClientService.loadAuthorizedClient(
6265
oauthToken.getAuthorizedClientRegistrationId(),
6366
oauthToken.getName()
6467
);
65-
String githubAccessToken = client.getAccessToken().getTokenValue();
66-
log.info("--------AccessToken : {}", githubAccessToken);
67-
loginUser.setGithubAccessToken(githubAccessToken);
68+
69+
//AES 암호화
70+
try {
71+
String encodedGithubToken = aesUtil.encrypt(client.getAccessToken().getTokenValue());
72+
loginUser.setGithubAccessToken(encodedGithubToken);
73+
} catch (Exception e) {
74+
log.error(e.getMessage());
75+
throw new AuthException(AuthExceptionCode.TOKEN_ENCODE_FAIL);
76+
}
6877
userDomainService.updateUserGithubAccessToken(loginUser);
6978
}
7079

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.ezcode.codetest.common.security.util;
2+
3+
import java.util.Base64;
4+
5+
import javax.crypto.Cipher;
6+
import javax.crypto.spec.SecretKeySpec;
7+
8+
import org.springframework.beans.factory.annotation.Value;
9+
import org.springframework.stereotype.Component;
10+
11+
@Component
12+
public class AESUtil {
13+
14+
private static final String ALGORITHM = "AES";
15+
@Value("${AES_SECRET_KEY}")
16+
private String SECRET_KEY; // 반드시 16, 24, 또는 32바이트
17+
18+
public String encrypt(String input) throws Exception {
19+
SecretKeySpec keySpec = new SecretKeySpec(SECRET_KEY.getBytes(), ALGORITHM);
20+
Cipher cipher = Cipher.getInstance(ALGORITHM);
21+
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
22+
byte[] encryptedBytes = cipher.doFinal(input.getBytes());
23+
return Base64.getEncoder().encodeToString(encryptedBytes);
24+
}
25+
26+
public String decrypt(String encryptedInput) throws Exception {
27+
SecretKeySpec keySpec = new SecretKeySpec(SECRET_KEY.getBytes(), ALGORITHM);
28+
Cipher cipher = Cipher.getInstance(ALGORITHM);
29+
cipher.init(Cipher.DECRYPT_MODE, keySpec);
30+
byte[] encryptedBytes = Base64.getDecoder().decode(encryptedInput);
31+
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
32+
return new String(decryptedBytes);
33+
}
34+
}

src/main/java/org/ezcode/codetest/domain/user/exception/code/AuthExceptionCode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public enum AuthExceptionCode implements ResponseCode {
1818
ALREADY_EXIST_USER(false, HttpStatus.BAD_REQUEST, "이미 가입된 유저입니다."),
1919
NOT_EMAIL_USER(false, HttpStatus.BAD_REQUEST, "소셜 로그인 회원은 비밀번호 변경을 할 수 없습니다."),
2020
PASSWORD_IS_SAME(false, HttpStatus.BAD_REQUEST, "기존 비밀번호와 같습니다. 새로운 비밀번호는 기존 비밀번호와 달라야합니다."),
21-
ALREADY_WITHDRAW_USER(false, HttpStatus.NOT_FOUND, "탈퇴된 회원입니다.")
21+
ALREADY_WITHDRAW_USER(false, HttpStatus.NOT_FOUND, "탈퇴된 회원입니다."),
22+
TOKEN_ENCODE_FAIL(false, HttpStatus.BAD_REQUEST, "토큰 인코딩에 실패했습니다.")
2223

2324
;
2425

src/main/resources/application.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,6 @@ logging.level.org.springframework.security=DEBUG
126126
logging.level.org.springframework.security.oauth2=DEBUG
127127
logging.level.org.springframework.security.oauth2.client=TRACE
128128
logging.level.org.springframework.web.client.RestTemplate=TRACE
129+
130+
131+
aes.secret.key=${AES_SECRET_KEY}

0 commit comments

Comments
 (0)