본문 바로가기
스프링 게시판

스프링 카카오 로그인

by 뇽꾸리 2020. 5. 16.
반응형

 

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
반응형

댓글