OAuthController.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package com.icontrols.oauth.controller;
  2. import javax.servlet.http.HttpSession;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.http.HttpHeaders;
  6. import org.springframework.web.bind.annotation.RequestHeader;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. import org.springframework.web.bind.annotation.RequestParam;
  10. import org.springframework.web.bind.annotation.ResponseBody;
  11. import org.springframework.web.bind.annotation.RestController;
  12. import org.springframework.web.servlet.ModelAndView;
  13. import org.springframework.web.servlet.view.RedirectView;
  14. @RestController
  15. public class OAuthController {
  16. private static final Logger logger = LoggerFactory.getLogger(OAuthController.class);
  17. // http://127.0.0.1:8080/api/oauth2/authorize?client_id=clientid&redirect_uri=http://127.0.0.1:8080/api/oauth2/redirect&scope=scope&response_type=code&state=state
  18. @RequestMapping(value = "/api/oauth2/authorize", method = RequestMethod.GET)
  19. public ModelAndView Authorize(@RequestHeader HttpHeaders headers,
  20. @RequestParam(value = "response_type", required = true) String responseType,
  21. @RequestParam(value = "client_id", required = true) String clientId,
  22. @RequestParam(value = "state", required = true) String state, @RequestParam(value = "scope") String scope,
  23. @RequestParam(value = "redirect_uri", required = true) String redirectUri, HttpSession httpSession) {
  24. logger.info("/api/oauth2/authorize");
  25. ModelAndView mav = new ModelAndView();
  26. mav.setViewName("/view/authorize.html");
  27. httpSession.setAttribute("redirectUri", redirectUri);
  28. httpSession.setAttribute("state", state);
  29. logger.info(httpSession.toString());
  30. return mav;
  31. }
  32. @RequestMapping(value = "/api/oauth2/auth", method = RequestMethod.GET)
  33. public @ResponseBody RedirectView SendCode(HttpSession httpSession) {
  34. logger.info("/api/oauth2/auth");
  35. // authorize.html에서 submit 버튼 눌러서 넘어오는 페이지 redirectUrl로 리다이렉트 (코드를 전송 하는 역할)
  36. // 웹뷰에서 사용자의 정보를 입력받아서 넘어와야함
  37. // 사용자 정보를 단지서버에 전송해 사용자 확인하는 절차필요
  38. String redirectUri = (String) httpSession.getAttribute("redirectUri");
  39. String code = "SampleCode"; // Code를 생성하고 이후에 토큰 요청을 수신했을 때 검사할 수 있어야 함.(DB저장 등이 필요함.)
  40. redirectUri += "?state=" + httpSession.getAttribute("state");
  41. redirectUri += "&code=" + code;
  42. logger.info(redirectUri);
  43. RedirectView redirectView = new RedirectView();
  44. redirectView.setUrl(redirectUri);
  45. return redirectView;
  46. }
  47. // ((Test)) Redirect Destination
  48. // 여기는 리다이렉트 받는 URl임 테스트용. Client를 대신함.
  49. @RequestMapping(value = "/api/oauth2/redirect", method = RequestMethod.GET, produces = "application/json")
  50. public @ResponseBody RedirectView RedirectTester(HttpSession httpSession,
  51. @RequestParam(value = "code", required = false) String code) {
  52. logger.info("/api/oauth2/redirect");
  53. // 웹뷰에서 사용자의 정보를 입력받아서 넘어와야함
  54. // 사용자 정보를 단지서버에 전송해 사용자 확인하는 절차필요
  55. String redirectUri = "http://127.0.0.1:8080/api/oauth2/token?grant_type=authorization_code&code=" + code;
  56. RedirectView redirectView = new RedirectView();
  57. redirectView.setUrl(redirectUri);
  58. return redirectView;
  59. }
  60. @RequestMapping(value = "/api/oauth2/token", method = RequestMethod.GET)
  61. public @ResponseBody String Token(@RequestParam(value = "grant_type", required = true) String grantType,
  62. @RequestParam(value = "code", required = false) String code,
  63. @RequestParam(value = "refresh_token", required = false) String refreshToken, HttpSession httpSession) {
  64. logger.info("/api/oauth2/token");
  65. // Code유효성 검사
  66. // token 발급
  67. logger.info(code);
  68. logger.info(grantType);
  69. String newAccessToken = "ACCESSTOKEN"; // 생성해야 함.
  70. String newRefreshToken = "REFRESHTOKEN"; // 생성해야 함.
  71. String tokenType = "bearer";
  72. int expiresIn = 3600;
  73. // class 만들어서 사용하는 것으로 변경해야함.
  74. String result = "{" + "access_toke:" + newAccessToken + ", refresh_token:" + newRefreshToken + ", token_type:"
  75. + tokenType + ", expires_in:" + expiresIn + "}";
  76. return result;
  77. }
  78. }