package com.icontrols.oauth.controller; import javax.servlet.http.HttpSession; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpHeaders; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.view.RedirectView; @RestController public class OAuthController { private static final Logger logger = LoggerFactory.getLogger(OAuthController.class); // 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 @RequestMapping(value = "/api/oauth2/authorize", method = RequestMethod.GET) public @ResponseBody ModelAndView Authorize(@RequestHeader HttpHeaders headers, @RequestParam(value = "response_type", required = true) String responseType, @RequestParam(value = "client_id", required = true) String clientId, @RequestParam(value = "state", required = true) String state, @RequestParam(value = "scope") String scope, @RequestParam(value = "redirect_uri", required = true) String redirectUri, HttpSession httpSession) { ModelAndView mav = new ModelAndView(); mav.setViewName("/view/authorize.html"); httpSession.setAttribute("redirectUri", redirectUri); httpSession.setAttribute("state", state); return mav; } @RequestMapping(value = "/api/oauth2/auth", method = RequestMethod.GET) public @ResponseBody RedirectView Redirect(HttpSession httpSession) { // 웹뷰에서 사용자의 정보를 입력받아서 넘어와야함 // 사용자 정보를 단지서버에 전송해 사용자 확인하는 절차필요 String redirectUri = (String) httpSession.getAttribute("redirectUri"); String code = "SampleCode"; // Code를 생성하고 이후에 토큰 요청을 수신했을 때 검사할 수 있어야 함.(DB저장 등이 필요함.) redirectUri += "?state=" + httpSession.getAttribute("state"); redirectUri += "&code=" + code; logger.info(redirectUri); RedirectView redirectView = new RedirectView(); redirectView.setUrl(redirectUri); return redirectView; } @RequestMapping(value = "/api/oauth2/token", method = RequestMethod.GET) public @ResponseBody RedirectView Token(HttpSession httpSession) { // 웹뷰에서 사용자의 정보를 입력받아서 넘어와야함 // 사용자 정보를 단지서버에 전송해 사용자 확인하는 절차필요 String redirectUri = (String) httpSession.getAttribute("redirectUri"); String code = "SampleCode"; // Code를 생성하고 이후에 토큰 요청을 수신했을 때 검사할 수 있어야 함.(DB저장 등이 필요함.) redirectUri += "?state=" + httpSession.getAttribute("state"); redirectUri += "&code=" + code; logger.info(redirectUri); RedirectView redirectView = new RedirectView(); redirectView.setUrl(redirectUri); return redirectView; } // ((Test)) Redirect Destination @RequestMapping(value = "/api/oauth2/redirect", method = RequestMethod.GET, produces = "application/json") public @ResponseBody String Token(@RequestParam(value = "grant_type", required = true) String grantType, @RequestParam(value = "code", required = false) String code, @RequestParam(value = "refresh_token", required = false) String refreshToken, HttpSession httpSession) { // Code유효성 검사 // token 발급 String newAccessToken = "ACCESSTOKEN"; // 생성해야 함. String newRefreshToken = "REFRESHTOKEN"; // 생성해야 함. String tokenType = "bearer"; int expiresIn = 3600; String result = "{" + "access_toke:" + newAccessToken + ", refresh_token:" + newRefreshToken + ", token_type:" + tokenType + ", expires_in:" + expiresIn + "}"; return result; } }