From cb7b9d376c356f550e0a8ab76040219a8ab479ac Mon Sep 17 00:00:00 2001 From: Limwngur Date: Sat, 29 Jul 2023 14:05:32 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9A=BDFeat=20#42:=20=EC=9D=B8=EA=B0=80?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EB=B0=9B=EA=B8=B0=20=EB=B0=8F=EC=B9=B4?= =?UTF-8?q?=EC=B9=B4=EC=98=A4=20=ED=86=A0=EA=B7=BC=20=EB=B0=9C=EA=B8=89=20?= =?UTF-8?q?api=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DongnaeFriend/KakaoTokenController.java | 42 ------------------- .../user/contorller/UserController.java | 17 ++++++-- .../domain/user/service/KakaoService.java | 2 + .../domain/user/service/KakaoServiceimpl.java | 35 ++++++++++++++++ src/main/resources/application.yml | 9 ++-- src/main/resources/templates/html/index.html | 2 +- 6 files changed, 58 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/umc/DongnaeFriend/KakaoTokenController.java b/src/main/java/com/umc/DongnaeFriend/KakaoTokenController.java index d9668b9..6568995 100644 --- a/src/main/java/com/umc/DongnaeFriend/KakaoTokenController.java +++ b/src/main/java/com/umc/DongnaeFriend/KakaoTokenController.java @@ -40,46 +40,4 @@ public String kakologin(Model model, HttpServletResponse response) { return "html/index"; } - - @GetMapping("/callback") - public String callback(Model model, @RequestParam("code") String code) throws IOException { - - try { -//------kakao POST 요청------ - String reqURL = "https://kauth.kakao.com/oauth/token?grant_type=authorization_code&client_id=1ad317e194df665ca44dcb82d11a7093&code=" + code; - URL url = new URL(reqURL); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setRequestMethod("POST"); - - - BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); - - String line = ""; - String result = ""; - - while ((line = br.readLine()) != null) { - result += line; - } - - ObjectMapper objectMapper = new ObjectMapper(); - Map jsonMap = objectMapper.readValue(result, new TypeReference>() { - }); - - String accessToken = (String) jsonMap.get("access_token"); - - //-------------------------------------------------서버 로그인---------------------------------------------------- - - HashMap userInfo = kakaoService.getUserInfo(accessToken); - UserDto.Response response = userService.userValidation(userInfo); - - model.addAttribute("token","Bearer "+ response.getAccessToken()); - - return "html/token"; - } catch (Exception e) { - e.printStackTrace(); - } - return null; - - } - } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/user/contorller/UserController.java b/src/main/java/com/umc/DongnaeFriend/domain/user/contorller/UserController.java index cc30211..4e8bdaa 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/user/contorller/UserController.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/user/contorller/UserController.java @@ -8,6 +8,7 @@ import com.umc.DongnaeFriend.global.util.JwtTokenProvider; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.data.repository.query.Param; import org.springframework.http.*; import org.springframework.web.bind.annotation.*; @@ -18,7 +19,7 @@ import java.util.HashMap; @RestController -@RequestMapping("/user") +@RequestMapping("") @Slf4j public class UserController { @@ -31,13 +32,14 @@ public class UserController { @Autowired JwtTokenProvider jwtTokenProvider; - + @Value("${kakao.client_id}") + private String client_id; /** * 유저 로그인 / 회원가입 * 인증 절차 */ - @PostMapping("/login") + @PostMapping("/user/login") public ResponseEntity userLogin(@RequestParam("accessToken") String accessToken, HttpServletRequest request, HttpServletResponse httpServletResponse) { log.info("LoginController 진입"); @@ -75,6 +77,15 @@ public ResponseEntity reiussnaceToken(String refreshToken) { } } + /** + * 인가코드를 입력 받으면, 카카오에서 access_token을 받아온다. + */ + @GetMapping("/callback") + public @ResponseBody ResponseEntity callback(@RequestParam("code") String code) throws IOException { + log.info("code : "+code); + String kakao_accessToken = kakaoService.getAccessTokenFromKakao(client_id, code);//kakao_access_token + return ResponseEntity.ok(kakao_accessToken); + } } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/user/service/KakaoService.java b/src/main/java/com/umc/DongnaeFriend/domain/user/service/KakaoService.java index d3cd29f..a4245fc 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/user/service/KakaoService.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/user/service/KakaoService.java @@ -1,6 +1,7 @@ package com.umc.DongnaeFriend.domain.user.service; +import com.umc.DongnaeFriend.domain.user.dto.UserDto; import org.springframework.beans.factory.annotation.Value; import java.io.IOException; @@ -11,6 +12,7 @@ public interface KakaoService { @SuppressWarnings("unchecked") HashMap getUserInfo(String access_Token) throws IOException; + String getAccessTokenFromKakao(String client_id, String code) throws IOException; } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/user/service/KakaoServiceimpl.java b/src/main/java/com/umc/DongnaeFriend/domain/user/service/KakaoServiceimpl.java index 047549b..0354032 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/user/service/KakaoServiceimpl.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/user/service/KakaoServiceimpl.java @@ -2,7 +2,9 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import com.umc.DongnaeFriend.domain.user.dto.UserDto; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.BufferedReader; @@ -20,6 +22,9 @@ public class KakaoServiceimpl implements KakaoService { // @Autowired // public IACDao dao; + @Autowired + private UserService userService; + @SuppressWarnings("unchecked") @Override public HashMap getUserInfo(String access_Token) throws IOException { @@ -81,4 +86,34 @@ public HashMap getUserInfo(String access_Token) throws IOExcepti return userInfo; } + @Override + public String getAccessTokenFromKakao(String client_id, String code) throws IOException { + + //------kakao POST 요청------ + String reqURL = "https://kauth.kakao.com/oauth/token?grant_type=authorization_code&client_id=" + client_id + "&code=" + code; + URL url = new URL(reqURL); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("POST"); + + BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); + + String line = ""; + String result = ""; + + while ((line = br.readLine()) != null) { + result += line; + } + + ObjectMapper objectMapper = new ObjectMapper(); + Map jsonMap = objectMapper.readValue(result, new TypeReference>() { + }); + + log.info("Response Bosy : " + result); + + String accessToken = (String) jsonMap.get("access_token"); + + + return accessToken; + } + } \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 6826cbf..13bf0a8 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -8,9 +8,9 @@ logging: spring: datasource: - url: jdbc:mysql://localhost:3306/dongnae?characterEncoding=UTF-8&serverTimezone=UTC&useLegacyDatetimeCode=false - username: - password: + url: jdbc:mysql://localhost:3306/security?characterEncoding=UTF-8&serverTimezone=UTC&useLegacyDatetimeCode=false + username: root + password: Wngurdl1! driver-class-name: com.mysql.cj.jdbc.Driver jpa: hibernate.ddl-auto: create @@ -21,5 +21,8 @@ spring: thymeleaf: cache: false +kakao: + client_id: 2ec872fb1ee5979221c7148d92f86c2f jwt: secret-key: 6B64DCA4EA2F53EDIKU9AAB215FE7 + diff --git a/src/main/resources/templates/html/index.html b/src/main/resources/templates/html/index.html index 39a3ed4..a353e8a 100644 --- a/src/main/resources/templates/html/index.html +++ b/src/main/resources/templates/html/index.html @@ -6,7 +6,7 @@

동네친구 카카오 로그인

- + kakoLogin From 23162e31cfcc720ab95c06b7d335d760c973be83 Mon Sep 17 00:00:00 2001 From: DDonghyeo Date: Sun, 30 Jul 2023 15:10:13 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8FFeat=20:=20[OAuth2]=20?= =?UTF-8?q?=EC=B9=B4=EC=B9=B4=EC=98=A4=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/.DS_Store | Bin 6148 -> 8196 bytes .../user/contorller/UserController.java | 7 ++++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/.DS_Store b/src/main/.DS_Store index 60811adf66699995359111af33e30b914e54ed8a..dbe34412c74c48ff1f1d4c7b1da6fcba7429981a 100644 GIT binary patch literal 8196 zcmeHMU2GIp6h3EKU}m6ji~Mv}p-WeUV3D@`$ggbM1uZ{m?Y8t6*4dpAMy4}mXLc89 znl#3r1T;R0|Bsp&4G$(ncrY5DG|?!A5W&P3eeeN&@C8jYdhWebLQDH(;!l{H+s{HW^$VN=r;RH~}y%$-MV{=CE4 zV}2zYnqetuGmAr<>+yXzKRGw^ha9_-V@uk7FEky`E(!z3vPosQ&+#n3(&m>uD-gC> zDbT3pSl!sz#+IhWxC=*}H?M?C+ubOxgX0cxf>cV=eK z+@RbpJ*!GgdiG7^nX2jYjlO<;u-_^8Ue-Bf=h&ivT0*AR&e72Ez2J~t42Y)E zP0|7F+=8Vm8t%KlY4i5onT~^xUancVh}ACE^+Ae}7dnqScCokOSmCh&Gbq@e<#{MK^j~GjF@h z(xlmz2X&*bEWKv?qs1LNb)&!FkBpFuK`k~$%js6bIKp>6UN zgJWjV?yL~L*r(C@K3W&gjU?P`kL84Zk%S^7^+uPf4)9C1T9$2Hu328UtUJpsnB$cl zLNq-L32gSYs;a5d3XuV!dq|-F2YCf8C-%Z za22k>x9|g8hhN}#_yhh#Kn)jTJ+8tAOkpE##cjA9+wl?Hhx_pW_TnH8<6%64M{xvw z9K{oO3ZKHK@fmy;U&L4Nb$kQg#JBN0et;M7B7TBj;FtInUczg59e=`~@do~izbRG9 z0;OJAr8FohHcmE|A%B z*OH|xQ)}0^Y}t{%i7E-hN#xy%X940V&=(*UPkb8sIEakI-TI2z@~W56Z=X~-gxNf) zozxptLO+`$+12_Qm2l7IN_JhMQKd{{ieU8(iDr%RiB(BWWM?4wL|Y=GQg*Pv0or%qeYgN0!{-Fr@8Em*5pEDrRa}U5xRSuip}HC$zzx`p zEw~AH;X|0lR)XyTJcymxh21zruswn~H1P>MjxKsw!IK2tF&ro0K8L6A48DM8@nw7! zUyC68F1|MnlKZAY54;89N-Na< Y@gD-BFVFb>KhsBR{Qei9yaR^60BWr)6951J delta 143 zcmZp1XfcprU|?W$DortDU=RQ@Ie-{MGjUEV6q~50$jG!YU^nAr0~wad`~q@}J16T2 z6tYQHR~s4`OkN_O$k;vkn1DQE&*rUSL5v$4&M_}$=im@z1{wea0^C5t6=c@N!tczJ a`DGkImN76gFtC7VCWvidvCZ*3bC>}Ny&6ma diff --git a/src/main/java/com/umc/DongnaeFriend/domain/user/contorller/UserController.java b/src/main/java/com/umc/DongnaeFriend/domain/user/contorller/UserController.java index 4e8bdaa..93604b6 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/user/contorller/UserController.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/user/contorller/UserController.java @@ -40,7 +40,7 @@ public class UserController { * 인증 절차 */ @PostMapping("/user/login") - public ResponseEntity userLogin(@RequestParam("accessToken") String accessToken, HttpServletRequest request, HttpServletResponse httpServletResponse) { + public ResponseEntity userLogin(@RequestParam("accessToken") String accessToken) { log.info("LoginController 진입"); // if (!type.equals("kakao")) { @@ -81,11 +81,12 @@ public ResponseEntity reiussnaceToken(String refreshToken) { * 인가코드를 입력 받으면, 카카오에서 access_token을 받아온다. */ @GetMapping("/callback") - public @ResponseBody ResponseEntity callback(@RequestParam("code") String code) throws IOException { + public @ResponseBody ResponseEntity callback(@RequestParam("code") String code, HttpServletResponse response) throws IOException { log.info("code : "+code); String kakao_accessToken = kakaoService.getAccessTokenFromKakao(client_id, code);//kakao_access_token - return ResponseEntity.ok(kakao_accessToken); + + return userLogin(kakao_accessToken); } }