-
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.
- Loading branch information
Showing
15 changed files
with
114 additions
and
6 deletions.
There are no files selected for viewing
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
Binary file not shown.
Binary file not shown.
79 changes: 79 additions & 0 deletions
79
src/main/java/com/umc/DongnaeFriend/KakaoTokenController.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,79 @@ | ||
package com.umc.DongnaeFriend; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.umc.DongnaeFriend.domain.user.dto.UserDto; | ||
import com.umc.DongnaeFriend.domain.user.service.KakaoService; | ||
import com.umc.DongnaeFriend.domain.user.service.UserService; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
@Controller | ||
@RequestMapping("") | ||
public class KakaoTokenController { | ||
|
||
@Autowired | ||
private UserService userService; | ||
|
||
@Autowired | ||
private KakaoService kakaoService; | ||
|
||
@GetMapping("/kakao") | ||
public String kakologin(Model model, HttpServletResponse response) { | ||
response.setContentType(MediaType.TEXT_HTML_VALUE); | ||
|
||
return "html/index"; | ||
} | ||
|
||
@GetMapping("/callback") | ||
public String callback(Model model, @RequestParam("code") String code) throws IOException { | ||
|
||
//------kakao POST 요청------ | ||
String reqURL = "https://kauth.kakao.com/oauth/token?grant_type=authorization_code&client_id=8427ba9114a5ecb09621710469748441&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"; | ||
} | ||
|
||
} |
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
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
Binary file not shown.
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
Binary file not shown.
Binary file not shown.
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,15 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>My Page</title> | ||
</head> | ||
<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=8427ba9114a5ecb09621710469748441&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> | ||
|
||
</div> | ||
</body> | ||
</html> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Access Token</title> | ||
</head> | ||
<body> | ||
<h1 th:text="${token}">ERROR...</h1> | ||
</body> | ||
</html> |