-
OAuth 구글 인증 로그인(Google login) #2(스프링 작성)OAuth 2019. 6. 5. 22:52
@RequestMapping(value = "/member/googleSignIn", method = RequestMethod.POST) public void doGoogleSignInActionPage(HttpServletResponse response) { OAuth2Operations oauthOperations = googleConnectionFactory.getOAuthOperations(); String url = oauthOperations.buildAuthorizeUrl(GrantType.AUTHORIZATION_CODE, googleOAuth2Parameters); //System.out.println("/member/googleSignIn, url : " + url); PrintWriter out; try { out = response.getWriter(); out.write(url); out.flush(); out.close(); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } }
@RequestMapping("/member/googleSignInCallback") public String doSessionAssignActionPage(HttpServletRequest request){ System.out.println("/member/googleSignInCallback"); String code = request.getParameter("code"); OAuth2Operations oauthOperations = googleConnectionFactory.getOAuthOperations(); AccessGrant accessGrant = oauthOperations.exchangeForAccess(code , googleOAuth2Parameters.getRedirectUri(), null); String accessToken = accessGrant.getAccessToken(); Long expireTime = accessGrant.getExpireTime(); if (expireTime != null && expireTime < System.currentTimeMillis()) { accessToken = accessGrant.getRefreshToken(); System.out.printf("accessToken is expired. refresh token = {}", accessToken); } Connection connection = googleConnectionFactory.createConnection(accessGrant); Google google = connection == null ? new GoogleTemplate(accessToken) : connection.getApi(); PlusOperations plusOperations = google.plusOperations(); Person person = plusOperations.getGoogleProfile(); MemberVO member = new MemberVO(); member.setNickName(person.getDisplayName()); member.setAuth("USR"); HttpSession session = request.getSession(); session.setAttribute("_MEMBER_", member ); System.out.println(person.getDisplayName()); return "redirect:/"; /*System.out.println(person.getAccountEmail()); System.out.println(person.getAboutMe()); System.out.println(person.getDisplayName()); System.out.println(person.getEtag()); System.out.println(person.getFamilyName()); System.out.println(person.getGender()); */ }
'OAuth' 카테고리의 다른 글
OAuth 구글 인증 로그인(Google login) #1(환경설정) (0) 2019.05.08