SpringBoot2로 Rest api 만들기(10) – Social Login 연동(kakao)

- UserService name change : KakaoService
- Social signin, signup Test
This commit is contained in:
kimyonghwa
2019-04-19 11:00:25 +09:00
parent 79dea54b73
commit 3acd2b66e0
8 changed files with 158 additions and 84 deletions

View File

@@ -1,6 +1,5 @@
package com.rest.api.controller.v1;
import com.google.gson.Gson;
import com.rest.api.advice.exception.CEmailSigninFailedException;
import com.rest.api.advice.exception.CUserExistException;
import com.rest.api.advice.exception.CUserNotFoundException;
@@ -11,15 +10,13 @@ import com.rest.api.model.response.SingleResult;
import com.rest.api.model.social.KakaoProfile;
import com.rest.api.repo.UserJpaRepo;
import com.rest.api.service.ResponseService;
import com.rest.api.service.user.UserService;
import com.rest.api.service.social.KakaoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.core.env.Environment;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.Collections;
import java.util.Optional;
@@ -34,10 +31,7 @@ public class SignController {
private final JwtTokenProvider jwtTokenProvider;
private final ResponseService responseService;
private final PasswordEncoder passwordEncoder;
private final RestTemplate restTemplate;
private final Environment env;
private final Gson gson;
private final UserService userService;
private final KakaoService kakaoService;
@ApiOperation(value = "로그인", notes = "이메일 회원 로그인을 한다.")
@PostMapping(value = "/signin")
@@ -57,7 +51,7 @@ public class SignController {
@ApiParam(value = "서비스 제공자 provider", required = true, defaultValue = "kakao") @PathVariable String provider,
@ApiParam(value = "소셜 access_token", required = true) @RequestParam String accessToken) {
KakaoProfile profile = userService.getKakaoProfile(accessToken);
KakaoProfile profile = kakaoService.getKakaoProfile(accessToken);
User user = userJpaRepo.findByUidAndProvider(String.valueOf(profile.getId()), provider).orElseThrow(CUserNotFoundException::new);
return responseService.getSingleResult(jwtTokenProvider.createToken(String.valueOf(user.getMsrl()), user.getRoles()));
}
@@ -80,12 +74,12 @@ public class SignController {
@ApiOperation(value = "소셜 계정 가입", notes = "소셜 계정 회원가입을 한다.")
@PostMapping(value = "/signup/{provider}")
public CommonResult signupProvider(@ApiParam(value = "서비스 제공자 provider", required = true, defaultValue = "kakao") @PathVariable String provider,
@ApiParam(value = "소셜 access_token", required = true) @RequestParam String accessToken,
@ApiParam(value = "이름", required = true) @RequestParam String name) {
@ApiParam(value = "소셜 access_token", required = true) @RequestParam String accessToken,
@ApiParam(value = "이름", required = true) @RequestParam String name) {
KakaoProfile profile = userService.getKakaoProfile(accessToken);
KakaoProfile profile = kakaoService.getKakaoProfile(accessToken);
Optional<User> user = userJpaRepo.findByUidAndProvider(String.valueOf(profile.getId()), provider);
if(user.isPresent())
if (user.isPresent())
throw new CUserExistException();
User inUser = User.builder()