-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat : 네이버 소셜 로그인 로직 추가 및 관련 변경 사항 반영
- 네이버 소셜 로그인 로직 구현 - CI_MVP.yml에 네이터 소셜 로그인 키 추가 - 멤버 엔티티에 소셜로그인 타입 (SocialType) 추가 - 멤버 엔티티에 소셜아이디 타입을 Long 에서 String 으로 변경 - 소셜아이디 타입 변경에 따른 JWT 로직 일부 수정
- Loading branch information
Showing
18 changed files
with
337 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/umc/kkijuk/server/auth/controller/NaverAuthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package umc.kkijuk.server.auth.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import umc.kkijuk.server.auth.service.NaverAuthService; | ||
import umc.kkijuk.server.member.domain.Member; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequiredArgsConstructor | ||
public class NaverAuthController { | ||
private final NaverAuthService naverAuthService; | ||
|
||
@GetMapping("/auth/naver/login") | ||
public ResponseEntity<Object> naverCallback(@RequestParam("code") String code, | ||
@RequestParam("state") String state){ | ||
try{ | ||
String naverAccessToken = naverAuthService.getNaverAccessToken(code, state); | ||
Member member = naverAuthService.processNaverUser(naverAccessToken); | ||
Map<String, Object> tokens = new HashMap<>(); | ||
tokens.put("Token", naverAuthService.generateTokens(member)); | ||
log.info("네이버 로그인 성공: 사용자 이름={}, 네이버 ID={}", member.getName(), member.getSocialId()); | ||
return ResponseEntity.ok(tokens); | ||
|
||
}catch (Exception e){ | ||
log.error("네이버 인증 처리 중 오류 발생 : {}",e.getMessage(),e); | ||
return ResponseEntity.internalServerError().body(Map.of("error", "네이버 인증 처리 실패")); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/umc/kkijuk/server/auth/dto/NaverTokenResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package umc.kkijuk.server.auth.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class NaverTokenResponse { | ||
@JsonProperty("access_token") | ||
private String accessToken; | ||
@JsonProperty("refresh_token") | ||
private String refreshToken; | ||
@JsonProperty("token_type") | ||
private String tokenType; | ||
@JsonProperty("expires_in") | ||
private String expiresIn; | ||
@JsonProperty("error") | ||
private String error; | ||
@JsonProperty("error_description") | ||
private String errorDescription; | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/umc/kkijuk/server/auth/dto/NaverUserResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package umc.kkijuk.server.auth.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class NaverUserResponse { | ||
@JsonProperty("resultcode") | ||
private String resultCode; | ||
@JsonProperty("message") | ||
private String message; | ||
@JsonProperty("response") | ||
private NaverUserDetail naverUserDetail; | ||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class NaverUserDetail { | ||
private String id; | ||
private String name; | ||
private String email; | ||
private String birthday; | ||
private String birthyear; | ||
private String mobile; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
src/main/java/umc/kkijuk/server/auth/service/NaverAuthService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package umc.kkijuk.server.auth.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
import umc.kkijuk.server.auth.dto.NaverTokenResponse; | ||
import umc.kkijuk.server.auth.dto.NaverUserResponse; | ||
import umc.kkijuk.server.auth.jwt.JwtUtil; | ||
import umc.kkijuk.server.member.domain.Member; | ||
import umc.kkijuk.server.member.repository.MemberRepository; | ||
import umc.kkijuk.server.member.service.MemberService; | ||
|
||
import java.time.LocalDate; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class NaverAuthService { | ||
private final MemberRepository memberRepository; | ||
private final JwtUtil jwtUtil; | ||
private final MemberService memberService; | ||
private final RestTemplate restTemplate; | ||
|
||
@Value("${spring.security.oauth2.client.registration.naver.client-id}") | ||
private String clientId; | ||
@Value("${spring.security.oauth2.client.registration.naver.client-secret}") | ||
private String clientSecret; | ||
@Value("${spring.security.oauth2.client.provider.naver.token-uri}") | ||
private String tokenUri; | ||
@Value("${spring.security.oauth2.client.provider.naver.user-info-uri}") | ||
private String userInfoUri; | ||
@Value("${spring.security.oauth2.client.registration.naver.authorization-grant-type}") | ||
private String grantType; | ||
|
||
public String getNaverAccessToken(String code, String state){ | ||
String url = UriComponentsBuilder.fromHttpUrl(tokenUri) | ||
.queryParam("grant_type",grantType) | ||
.queryParam("client_id", clientId) | ||
.queryParam("client_secret", clientSecret) | ||
.queryParam("code", code) | ||
.queryParam("state", state) | ||
.build().toUriString(); | ||
|
||
|
||
ResponseEntity<NaverTokenResponse> response = | ||
restTemplate.exchange(url, HttpMethod.GET, null, NaverTokenResponse.class); | ||
|
||
return response.getBody().getAccessToken(); | ||
|
||
} | ||
|
||
public NaverUserResponse.NaverUserDetail getNaverUserInfo(String naverAccessToken) { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setBearerAuth(naverAccessToken); | ||
|
||
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(headers); | ||
|
||
ResponseEntity<NaverUserResponse> response = | ||
restTemplate.exchange(userInfoUri, HttpMethod.GET, request, NaverUserResponse.class); | ||
|
||
return response.getBody().getNaverUserDetail(); | ||
|
||
} | ||
public Member processNaverUser(String naverAccessToken) { | ||
NaverUserResponse.NaverUserDetail naverUserInfo = getNaverUserInfo(naverAccessToken); | ||
String naverId = naverUserInfo.getId (); | ||
String email = naverUserInfo.getEmail(); | ||
String name = naverUserInfo.getName(); | ||
String phoneNumber = naverUserInfo.getMobile(); | ||
LocalDate birthDate = extractBirthDate(naverUserInfo); | ||
|
||
log.info("네이버 사용자 정보 추출 - 이메일: {}, 이름: {}, 카카오 ID: {}, 전화번호: {}, 생년월일: {}", email, name, naverId, phoneNumber, birthDate); | ||
|
||
return memberRepository.findBySocialId(naverId) | ||
.orElseGet(() -> { | ||
log.info("신규 사용자 생성 - 네이버 ID: {}", naverId); | ||
return memberService.createUserWithNaverId(naverId,naverUserInfo); | ||
}); | ||
} | ||
|
||
public Object generateTokens(Member member) { | ||
String naverId = String.valueOf(member.getSocialId()); | ||
String accessToken = jwtUtil.createAccessToken(naverId); | ||
String refreshToken = jwtUtil.createRefreshToken(naverId); | ||
|
||
log.info("JWT Token 생성 완료 - 네이버 ID : {}, accessToken : {}, refreshToken : {}", naverId, accessToken, refreshToken); | ||
|
||
Map<String, String> tokens = new HashMap<>(); | ||
tokens.put("accessToken", accessToken); | ||
tokens.put("refreshToken", refreshToken); | ||
|
||
return tokens; | ||
} | ||
|
||
public LocalDate extractBirthDate(NaverUserResponse.NaverUserDetail naverUserInfo) { | ||
String birthday = (String) naverUserInfo.getBirthday(); | ||
String birthyear = (String) naverUserInfo.getBirthyear(); | ||
log.info("Naver 생년월일 정보: {}, {}", birthday,birthyear ); | ||
|
||
|
||
if (birthday == null || birthday.isEmpty()) { | ||
return null; | ||
} | ||
|
||
int year = (birthyear != null && !birthyear.isEmpty()) | ||
? Integer.parseInt(birthyear) | ||
: LocalDate.now().getYear(); | ||
|
||
String[] dateParts = birthday.split("-"); | ||
int month = Integer.parseInt(dateParts[0]); | ||
int day = Integer.parseInt(dateParts[1]); | ||
|
||
|
||
return LocalDate.of(year, month, day); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.