반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
@Controller
public class LoginController {
@Inject
CrazyLoginService service;
private final static String id = "rest api";
private final static String url = "redirect uri";
@RequestMapping(value = "/login.do")
public String loginview(Model model, HttpSession session) {
String kakaoUrl ="https://kauth.kakao.com/oauth/authorize?"
+"client_id="+id + "&redirect_uri="+url+"&response_type=code";
model.addAttribute("kakaoUrl",kakaoUrl);
return "login";
}
public static JsonNode getAccessToken(String autorize_code){
final String RequestUrl = "https://kauth.kakao.com/oauth/token";
final List<BasicNameValuePair> postParams = new ArrayList<BasicNameValuePair>();
postParams.add(new BasicNameValuePair("grant_type", "authorization_code"));
postParams.add(new BasicNameValuePair("client_id", id)); // REST API KEY
postParams.add(new BasicNameValuePair("redirect_uri", url)); // 리다이렉트 URI
postParams.add(new BasicNameValuePair("code", autorize_code)); // 로그인 과정중 얻은 code 값
final HttpClient client = HttpClientBuilder.create().build();
final HttpPost post = new HttpPost(RequestUrl);
JsonNode returnNode = null;
try {
post.setEntity(new UrlEncodedFormEntity(postParams));
final HttpResponse response = client.execute(post);
final int responseCode = response.getStatusLine().getStatusCode();
System.out.println("\nSending 'POST' request to URL : " + RequestUrl);
System.out.println("Post parameters : " + postParams);
System.out.println("Response Code : " + responseCode);
//JSON 형태 반환값 처리
ObjectMapper mapper = new ObjectMapper();
returnNode = mapper.readTree(response.getEntity().getContent());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
return returnNode;
}
public static JsonNode getKakaoUserInfo(String access_token) {
final String RequestUrl = "https://kapi.kakao.com/v2/user/me";
final HttpClient client = HttpClientBuilder.create().build();
final HttpPost post = new HttpPost(RequestUrl);
String accessToken = access_token;
// add header
post.addHeader("Authorization", "Bearer " + accessToken);
JsonNode returnNode = null;
try {
final HttpResponse response = client.execute(post);
final int responseCode = response.getStatusLine().getStatusCode();
System.out.println("\nSending 'POST' request to URL : " + RequestUrl);
System.out.println("Response Code : " + responseCode);
//JSON 형태 반환값 처리
ObjectMapper mapper = new ObjectMapper();
returnNode = mapper.readTree(response.getEntity().getContent());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// clear resources
}
return returnNode;
}
@RequestMapping(value="/kakaologin.do",method=RequestMethod.GET)
public String kakaologin(@RequestParam("code") String code,HttpSession session)throws Exception{
JsonNode jsonToken = getAccessToken(code);
String access_token = jsonToken.get("access_token").toString();
JsonNode userInfo = getKakaoUserInfo(access_token);
String id = userInfo.get("id").toString();
String nickName = userInfo.get("properties").get("nickname").toString();
session.setAttribute("userid", nickName);
System.out.println(id+nickName);
return "redirect:crazyboard.do";
}
}
|
cs |
반응형
'스프링 게시판' 카테고리의 다른 글
스프링게시판 만들기 리스트 , 페이지 나누기 (0) | 2021.01.12 |
---|---|
Spring json 데이터 통으로 받기 (0) | 2020.12.21 |
스프링 시큐리티 로그인 비밀번호 암호화 (0) | 2020.06.20 |
스프링 게시판 만들기 1 글쓰기 / 스프링 파일 업로드 / 스프링 다중파일 업로드/마이바티스 collection사용 (0) | 2020.04.26 |
스프링 설정 pom.xml / applicationContext.xml (database설정) /controller.xml (4) | 2020.04.26 |
댓글