-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[feat] 구글 로그인 구현
- Loading branch information
Showing
9 changed files
with
198 additions
and
2 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/main/java/com/example/comma/domain/user/controller/OauthController.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,32 @@ | ||
package com.example.comma.domain.user.controller; | ||
|
||
|
||
import com.example.comma.domain.user.dto.response.LoginResponseDto; | ||
import com.example.comma.domain.user.dto.response.UserInfoResponseDto; | ||
import com.example.comma.domain.user.service.OauthService; | ||
import com.example.comma.domain.user.service.UserService; | ||
import com.example.comma.global.common.SuccessResponse; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping(value = "/login/oauth2", produces = "application/json") | ||
public class OauthController { | ||
|
||
private final OauthService oauthService; | ||
private final UserService userService; | ||
|
||
@GetMapping("/code/{registrationId}") | ||
ResponseEntity<SuccessResponse<?>> googleLogin(@RequestParam (name = "code") String code, @PathVariable (name = "registrationId") String registrationId) { | ||
|
||
Long userId = oauthService.socialLogin(code, registrationId); | ||
String accessToken = userService.issueNewAccessToken(userId); | ||
String randomNickname = userService.generateNickname(); | ||
|
||
LoginResponseDto response = new LoginResponseDto(accessToken, randomNickname); | ||
|
||
return SuccessResponse.ok(response); | ||
} | ||
} |
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
7 changes: 7 additions & 0 deletions
7
src/main/java/com/example/comma/domain/user/dto/response/LoginResponseDto.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,7 @@ | ||
package com.example.comma.domain.user.dto.response; | ||
|
||
public record LoginResponseDto( | ||
String accessToken, | ||
String RandomNickname | ||
) { | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/example/comma/domain/user/dto/response/UserInfoResponseDto.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,10 @@ | ||
package com.example.comma.domain.user.dto.response; | ||
|
||
public record UserInfoResponseDto( | ||
Long id, | ||
String socialId, | ||
String name, | ||
String email, | ||
String profileImage | ||
) { | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ | |
|
||
public interface UserRepository extends JpaRepository<User, Long> { | ||
|
||
User findBySocialId(String socialId); | ||
} |
104 changes: 104 additions & 0 deletions
104
src/main/java/com/example/comma/domain/user/service/OauthService.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,104 @@ | ||
package com.example.comma.domain.user.service; | ||
import com.example.comma.domain.user.dto.response.UserInfoResponseDto; | ||
import com.example.comma.domain.user.dto.response.UserTokenResponseDto; | ||
import com.example.comma.domain.user.entity.User; | ||
import com.example.comma.domain.user.repository.UserRepository; | ||
import com.example.comma.global.config.auth.jwt.JwtProvider; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.env.Environment; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class OauthService { | ||
|
||
private final Environment env; | ||
private final UserRepository userRepository; | ||
private final JwtProvider jwtProvider; | ||
|
||
private final RestTemplate restTemplate = new RestTemplate(); | ||
|
||
@Value("${spring.security.oauth2.client.registration.google.client-id}") | ||
private String clientId; | ||
|
||
@Value("${spring.security.oauth2.client.registration.google.client-secret}") | ||
private String clientSecret; | ||
|
||
@Value("${spring.security.oauth2.client.registration.google.redirect-uri}") | ||
private String redirectUri; | ||
|
||
public Long socialLogin(String code, String registrationId) { | ||
String accessToken = getOauthToken(code, registrationId); | ||
JsonNode userResourceNode = getUserResource(accessToken, registrationId); | ||
String socialId = userResourceNode.get("id").asText(); | ||
String email = userResourceNode.get("email").asText(); | ||
String nickname = userResourceNode.get("name").asText(); | ||
String profileImage = userResourceNode.get("picture").asText(); | ||
UserInfoResponseDto user = saveMember(socialId, nickname, email, profileImage); | ||
Long userId = user.id(); | ||
return userId; | ||
|
||
} | ||
|
||
public String getOauthToken(String authorizationCode, String registrationId) { | ||
String tokenUri = env.getProperty("oauth2." + registrationId + ".token-uri"); | ||
|
||
MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); | ||
params.add("code", authorizationCode); | ||
params.add("client_id", clientId); | ||
params.add("client_secret", clientSecret); | ||
params.add("redirect_uri", redirectUri); | ||
params.add("grant_type", "authorization_code"); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); | ||
|
||
HttpEntity entity = new HttpEntity(params, headers); | ||
|
||
ResponseEntity<JsonNode> responseNode = restTemplate.exchange(tokenUri, HttpMethod.POST, entity, JsonNode.class); | ||
JsonNode accessTokenNode = responseNode.getBody(); | ||
String accessToken = accessTokenNode.get("access_token").asText(); | ||
return accessToken; | ||
} | ||
|
||
private JsonNode getUserResource(String accessToken, String registrationId) { | ||
String resourceUri = env.getProperty("oauth2."+registrationId+".resource-uri"); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.set("Authorization", "Bearer " + accessToken); | ||
HttpEntity entity = new HttpEntity(headers); | ||
return restTemplate.exchange(resourceUri, HttpMethod.GET, entity, JsonNode.class).getBody(); | ||
} | ||
|
||
public UserInfoResponseDto saveMember(String socialId, String name, String email, String profileImage) { | ||
User existMember = userRepository.findBySocialId(socialId); | ||
|
||
if (existMember == null) { | ||
User user = User.builder() | ||
.socialId(socialId) | ||
.name(name) | ||
.email(email) | ||
.profileImage(profileImage) | ||
.build(); | ||
userRepository.save(user); | ||
|
||
return new UserInfoResponseDto(user.getId(), user.getSocialId(), user.getName(), user.getEmail(), user.getProfileImage()); | ||
} | ||
|
||
return new UserInfoResponseDto(existMember.getId(), existMember.getSocialId(), existMember.getName(), existMember.getEmail(), existMember.getProfileImage()); | ||
} | ||
|
||
|
||
} |
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