diff --git a/src/main/java/com/developer/wiki/oauth/controller/OauthController.java b/src/main/java/com/developer/wiki/oauth/controller/OauthController.java index ec10cd8..2a9a43b 100644 --- a/src/main/java/com/developer/wiki/oauth/controller/OauthController.java +++ b/src/main/java/com/developer/wiki/oauth/controller/OauthController.java @@ -1,22 +1,85 @@ package com.developer.wiki.oauth.controller; +import com.developer.wiki.common.exception.BadRequestException; +import com.developer.wiki.oauth.dto.GitHubOauthToken; +import com.developer.wiki.oauth.dto.GoogleOAuthToken; import com.developer.wiki.oauth.dto.GoogleResponseDto; import com.developer.wiki.oauth.service.OauthService; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.*; +import org.springframework.util.MultiValueMap; 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 org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + @RequiredArgsConstructor @RestController @RequestMapping("/api/v1/oauth") public class OauthController { private final OauthService oauthService; + + private final RestTemplate restTemplate; + + private final ObjectMapper objectMapper; + @Value("${custom.github.client-id}") + private String GITHUB_CLIENT_ID; + + @Value("${custom.github.client-secret}") + private String GITHUB_CLIENT_SECRET; + + @GetMapping() + public String Code(@RequestParam(value = "code")String code) throws JsonProcessingException { + System.out.println("코드 값임당 "+code); + String GOOGLE_TOKEN_REQUEST_URL="https://github.com/login/oauth/access_token"; + RestTemplate restTemplate=new RestTemplate(); + Map params = new HashMap<>(); + params.put("code", code); + params.put("client_id", GITHUB_CLIENT_ID); + params.put("client_secret", GITHUB_CLIENT_SECRET); + params.put("redirect_uri", "http://localhost:8080/api/v1/oauth"); + ResponseEntity responseEntity; + try { + responseEntity=restTemplate.postForEntity(GOOGLE_TOKEN_REQUEST_URL,params,String.class); + System.out.println("엑세스 토큰 임당 "+responseEntity.getBody()); + }catch (RestClientException e){ + e.printStackTrace(); + throw new BadRequestException(String.format("인가코드로 구글의 AccessToken을 발급하지 못했습니다. code : %s, redirectUrl : %s, 오류 내용 : %s",code,"redirectUrl",e.getMessage())); + } + String[] resList=responseEntity.getBody().split("&"); + String[] token=resList[0].split("="); + System.out.println("토큰의 값은 : "+token[1]); + String GOOGLE_USERINFO_REQUEST_URL="https://api.github.com/user"; + //header에 accessToken을 담는다. + HttpHeaders headers = new HttpHeaders(); + headers.add("Authorization","Bearer "+token[1]); + + //HttpEntity를 하나 생성해 헤더를 담아서 restTemplate으로 구글과 통신하게 된다. + HttpEntity> request = new HttpEntity(headers); + try { + ResponseEntity response=restTemplate.exchange(GOOGLE_USERINFO_REQUEST_URL, HttpMethod.GET,request,String.class); + System.out.println("response.getBody() = " + response.getBody()); + return response.getBody(); + }catch (RestClientException e){ + throw new BadRequestException("구글 AccessToken을 으로 사용자 정보를 가져오지 못했습니다."); + } + } + private GitHubOauthToken getAccessToken(ResponseEntity response) throws JsonProcessingException { + System.out.println("response.getBody() = " + response.getBody()); + GitHubOauthToken gitHubOauthToken= objectMapper.readValue(response.getBody(),GitHubOauthToken.class); + return gitHubOauthToken; + } @GetMapping("/google/userinfo") public ResponseEntity getGoogleUserInfo(@RequestParam("code") String code,@RequestParam("redirectUrl")String redirectUrl) throws IOException { System.out.println(">> 소셜 로그인 API 서버로부터 받은 code :"+ code); @@ -30,4 +93,11 @@ public ResponseEntity getGoogleUrl(@RequestParam String url, HttpServlet //response.sendRedirect(redirectUrl); return ResponseEntity.ok(redirectUrl); } + @GetMapping("/github/userinfo") + public ResponseEntity getGitHubUserInfo(@RequestParam("code") String code,@RequestParam("redirectUrl")String redirectUrl) throws IOException { + System.out.println(">> 소셜 로그인 API 서버로부터 받은 code :"+ code); + System.out.println(">> 소셜 로그인 API 서버로부터 받은 url :"+ redirectUrl); + //GoogleResponseDto GoogleUser = oauthService.oAuthLogin(code,redirectUrl); + return new ResponseEntity<>(code, HttpStatus.OK); + } } diff --git a/src/main/java/com/developer/wiki/oauth/dto/GitHubOauthToken.java b/src/main/java/com/developer/wiki/oauth/dto/GitHubOauthToken.java new file mode 100644 index 0000000..d55da79 --- /dev/null +++ b/src/main/java/com/developer/wiki/oauth/dto/GitHubOauthToken.java @@ -0,0 +1,14 @@ +package com.developer.wiki.oauth.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +@AllArgsConstructor +@Getter +@Setter +public class GitHubOauthToken { + private String access_token; + private String scope; + private String token_type; +} diff --git a/src/main/java/com/developer/wiki/oauth/service/GoogleOauthService.java b/src/main/java/com/developer/wiki/oauth/service/GoogleOauthService.java index 627632d..1e4b1b3 100644 --- a/src/main/java/com/developer/wiki/oauth/service/GoogleOauthService.java +++ b/src/main/java/com/developer/wiki/oauth/service/GoogleOauthService.java @@ -58,7 +58,6 @@ public String googleInitUrl(String url) { public ResponseEntity requestAccessToken(String code,String redirectUrl) { String GOOGLE_TOKEN_REQUEST_URL="https://oauth2.googleapis.com/token"; - RestTemplate restTemplate=new RestTemplate(); Map params = new HashMap<>(); params.put("code", code); params.put("client_id", GOOGLE_SNS_CLIENT_ID); @@ -79,7 +78,6 @@ public GoogleOAuthToken getAccessToken(ResponseEntity response) throws J System.out.println("response.getBody() = " + response.getBody()); GoogleOAuthToken googleOAuthToken= objectMapper.readValue(response.getBody(),GoogleOAuthToken.class); return googleOAuthToken; - } public ResponseEntity requestUserInfo(GoogleOAuthToken oAuthToken) { diff --git a/src/main/resources/application-oauth.yaml b/src/main/resources/application-oauth.yaml index a9823dd..2f0baf7 100644 --- a/src/main/resources/application-oauth.yaml +++ b/src/main/resources/application-oauth.yaml @@ -14,5 +14,8 @@ custom: scope : https://www.googleapis.com/auth/userinfo.profile+https://www.googleapis.com/auth/userinfo.email client-id: ENC(Stk5BNqlTPDUykrnPM0g7bOZ+T/c4kAa191scAucdBOZHq9Eq8eLFTbtFhoBeNRKoMMEXGfUC6MHHFxk4DqReqRiSNpue7u7XblRjluFdL9VKbQxsRH0Aw==) client-secret: ENC(zJ/hEIdsY55cmXyEk8M2SYJ6RUGhxJByLc1whUj6IAfh9ZwWmYSDkdtI5k8jQPE9) + github: + client-id : ENC(PlT5DdeWhTqT/jyP81Ao4w5z53UDZ2t6cN7V0O+xtaI=) + client-secret : ENC(+xbNlumfURi7aixRrtMODILbhQY800HSVSz6/nIiOYLuTbbhcRvesFJsj45GhxA3HddjrOsUaCQ=) jwt: secretKey: ENC(4eL6R6/CT+2w1ss8z9kbLCGT+TLF4JrAw4HATFWQGA0ZVg53roc2Ej/OOKsI5G/9kBSzPV8dZ5l84UNnKMF8Iw==) \ No newline at end of file