Skip to content

Commit

Permalink
Merge pull request #55 from DongnaeFriend/feature/#42
Browse files Browse the repository at this point in the history
⚽Feat #42:  [OAuth로그인] 카카오 로그인
  • Loading branch information
Juhyeok0202 authored Jul 30, 2023
2 parents 06006d2 + 35c85ec commit f080e0a
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 48 deletions.
Binary file modified src/main/.DS_Store
Binary file not shown.
42 changes: 0 additions & 42 deletions src/main/java/com/umc/DongnaeFriend/KakaoTokenController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> jsonMap = objectMapper.readValue(result, new TypeReference<Map<String, Object>>() {
});

String accessToken = (String) jsonMap.get("access_token");

//-------------------------------------------------서버 로그인----------------------------------------------------

HashMap<String, Object> 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;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -18,7 +19,7 @@
import java.util.HashMap;

@RestController
@RequestMapping("/user")
@RequestMapping("")
@Slf4j
public class UserController {

Expand All @@ -31,14 +32,15 @@ public class UserController {
@Autowired
JwtTokenProvider jwtTokenProvider;


@Value("${kakao.client_id}")
private String client_id;

/**
* 유저 로그인 / 회원가입
* 인증 절차
*/
@PostMapping("/login")
public ResponseEntity<?> userLogin(@RequestParam("accessToken") String accessToken, HttpServletRequest request, HttpServletResponse httpServletResponse) {
@PostMapping("/user/login")
public ResponseEntity<?> userLogin(@RequestParam("accessToken") String accessToken) {
log.info("LoginController 진입");

// if (!type.equals("kakao")) {
Expand Down Expand Up @@ -75,6 +77,16 @@ public ResponseEntity<?> reiussnaceToken(String refreshToken) {
}
}

/**
* 인가코드를 입력 받으면, 카카오에서 access_token을 받아온다.
*/
@GetMapping("/callback")
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 userLogin(kakao_accessToken);
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,6 +12,7 @@ public interface KakaoService {

@SuppressWarnings("unchecked")
HashMap<String, Object> getUserInfo(String access_Token) throws IOException;
String getAccessTokenFromKakao(String client_id, String code) throws IOException;
}


Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,6 +22,9 @@ public class KakaoServiceimpl implements KakaoService {
// @Autowired
// public IACDao dao;

@Autowired
private UserService userService;

@SuppressWarnings("unchecked")
@Override
public HashMap<String, Object> getUserInfo(String access_Token) throws IOException {
Expand Down Expand Up @@ -81,4 +86,34 @@ public HashMap<String, Object> 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<String, Object> jsonMap = objectMapper.readValue(result, new TypeReference<Map<String, Object>>() {
});

log.info("Response Bosy : " + result);

String accessToken = (String) jsonMap.get("access_token");


return accessToken;
}

}
13 changes: 12 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,22 @@ spring:
# Settings for local
spring:
datasource:
url: jdbc:mysql://localhost:3306/dongnae?characterEncoding=UTF-8&serverTimezone=UTC&useLegacyDatetimeCode=false
url: jdbc:mysql://localhost:3306/dongnae?characterEncoding=UTF-8&serverTimezone=UTC&useLegacyDatetimeCode=false
username: dongnae
password: df1234
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate.ddl-auto: update
properties:
hibernate:
format_sql: true
show_sql: true
thymeleaf:
cache: false

kakao:
client_id: 2ec872fb1ee5979221c7148d92f86c2f

jwt:
secret-key: 6B64DCA4EA2F53EDIKU9AAB215FE7

2 changes: 1 addition & 1 deletion src/main/resources/templates/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<body>
<div class="wrapper" style="display: flex; flex-direction: column">
<h1>동네친구 카카오 로그인</h1>
<a href="https://kauth.kakao.com/oauth/authorize?response_type=code&client_id=1ad317e194df665ca44dcb82d11a7093&redirect_uri=http://localhost:8080/callback">
<a href="https://kauth.kakao.com/oauth/authorize?response_type=code&client_id=2ec872fb1ee5979221c7148d92f86c2f&redirect_uri=http://localhost:8080/callback">
<img src="https://developers.kakao.com/docs/static/image/ko/pc/kakaologin.png" alt="kakoLogin" style="cursor: pointer; width: 400px; height: 200px;">
</a>

Expand Down

0 comments on commit f080e0a

Please sign in to comment.