Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 2회차 코드리뷰 반영 #49

Merged
merged 15 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/main/java/com/ordertogether/team14_be/auth/JwtUtil.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
package com.ordertogether.team14_be.auth;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Date;
import javax.crypto.SecretKey;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Value;

@Component
public class JwtUtil {
private static final SecretKey key = Keys.secretKeyFor(io.jsonwebtoken.SignatureAlgorithm.HS256);
private static final int EXPIRE_TIME = 1;
private final SecretKey key;

public static String generateToken(String email) {
@Value("${jwt.expire-time}")
private int expireTime;

public JwtUtil(@Value("${key.jwt.secret-key}") String secretKey) {
this.key = Keys.hmacShaKeyFor(secretKey.getBytes(StandardCharsets.UTF_8));
}

public String generateToken(Long data) {
Date now = new Date();
Date exp = new Date(now.getTime() + Duration.ofHours(EXPIRE_TIME).toMillis());
Date exp = new Date(now.getTime() + Duration.ofHours(expireTime).toMillis());

return Jwts.builder()
.setSubject(email)
.setSubject(data.toString())
.setIssuedAt(now)
.setExpiration(exp)
.signWith(key)
.signWith(key, SignatureAlgorithm.HS256)
.compact();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import org.springframework.util.LinkedMultiValueMap;

@ConfigurationProperties(prefix = "kakao")
public record KakaoProperties(String clientId, String redirectUrl) {
public record KakaoProperties(
String clientId, String redirectUrl, String tokenUrl, String userApiUrl) {
public LinkedMultiValueMap<String, String> createBody(String code) {
LinkedMultiValueMap<String, String> body = new LinkedMultiValueMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ public class AuthService {

private final KakaoClient kakaoClient;
private final MemberService memberService;
private static JwtUtil jwtUtil;

public String kakaoLogin(String authorizationCode) {
String kakaoToken = kakaoClient.getAccessToken(authorizationCode); // 인가코드로부터 카카오토큰 발급
KakaoUserInfo kakaoUserInfo = kakaoClient.getUserInfo((kakaoToken));
String userKakaoEmail = kakaoUserInfo.kakaoAccount().email();
memberService.findOrCreateMember(userKakaoEmail);
String serviceToken = JwtUtil.generateToken(userKakaoEmail);
String serviceToken = jwtUtil.generateToken(memberService.getMemberId(userKakaoEmail));

return serviceToken;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import java.net.URI;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
Expand All @@ -18,22 +16,16 @@
@RequiredArgsConstructor
public class KakaoClient {

@Autowired private final RestClient restClient;
private final RestClient restClient;
private final KakaoProperties kakaoProperties;

@Value("${kakao.auth.token.url}")
private String kakaoTokenUrl;

@Value("${kakao.user.api.url}")
private String kakaoUserApiUrl;

public String getAccessToken(String authorizationCode) {
LinkedMultiValueMap<String, String> body = kakaoProperties.createBody(authorizationCode);

var response =
restClient
.post()
.uri(URI.create(kakaoTokenUrl))
.uri(URI.create(kakaoProperties.tokenUrl()))
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.body(body)
.retrieve()
Expand All @@ -45,7 +37,7 @@ public String getAccessToken(String authorizationCode) {
public KakaoUserInfo getUserInfo(String accessToken) {
return restClient
.get()
.uri(kakaoUserApiUrl)
.uri(kakaoProperties.userApiUrl())
.header(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.ordertogether.team14_be.member.persistence.MemberRepository;
import com.ordertogether.team14_be.member.persistence.entity.Member;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -11,6 +12,7 @@ public class MemberService {

private final MemberRepository memberRepository;

@Transactional
public void findOrCreateMember(String email) {
Member member =
memberRepository
Expand All @@ -21,4 +23,9 @@ public void findOrCreateMember(String email) {
return memberRepository.saveAndFlush(newMember);
});
}

public Long getMemberId(String email) {
Member member = memberRepository.findByEmail(email).get();
return member.getId();
}
}
3 changes: 3 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ kakao:
key:
jwt:
secret-key: ${JWT_SECRET_KEY}

jwt:
expire-time: 1