123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- 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 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) {
- logger.info("/api/oauth2/authorize");
- ModelAndView mav = new ModelAndView();
- mav.setViewName("/view/authorize.html");
- httpSession.setAttribute("redirectUri", redirectUri);
- httpSession.setAttribute("state", state);
- logger.info(httpSession.toString());
- return mav;
- }
- @RequestMapping(value = "/api/oauth2/auth", method = RequestMethod.GET)
- public @ResponseBody RedirectView SendCode(HttpSession httpSession) {
- logger.info("/api/oauth2/auth");
- // authorize.html에서 submit 버튼 눌러서 넘어오는 페이지 redirectUrl로 리다이렉트 (코드를 전송 하는 역할)
- // 웹뷰에서 사용자의 정보를 입력받아서 넘어와야함
- // 사용자 정보를 단지서버에 전송해 사용자 확인하는 절차필요
- 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
- // 여기는 리다이렉트 받는 URl임 테스트용. Client를 대신함.
- @RequestMapping(value = "/api/oauth2/redirect", method = RequestMethod.GET, produces = "application/json")
- public @ResponseBody RedirectView RedirectTester(HttpSession httpSession,
- @RequestParam(value = "code", required = false) String code) {
- logger.info("/api/oauth2/redirect");
- // 웹뷰에서 사용자의 정보를 입력받아서 넘어와야함
- // 사용자 정보를 단지서버에 전송해 사용자 확인하는 절차필요
- String redirectUri = "http://127.0.0.1:8080/api/oauth2/token?grant_type=authorization_code&code=" + code;
- RedirectView redirectView = new RedirectView();
- redirectView.setUrl(redirectUri);
- return redirectView;
- }
- @RequestMapping(value = "/api/oauth2/token", method = RequestMethod.GET)
- 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) {
- logger.info("/api/oauth2/token");
- // Code유효성 검사
- // token 발급
- logger.info(code);
- logger.info(grantType);
- String newAccessToken = "ACCESSTOKEN"; // 생성해야 함.
- String newRefreshToken = "REFRESHTOKEN"; // 생성해야 함.
- String tokenType = "bearer";
- int expiresIn = 3600;
- // class 만들어서 사용하는 것으로 변경해야함.
- String result = "{" + "access_toke:" + newAccessToken + ", refresh_token:" + newRefreshToken + ", token_type:"
- + tokenType + ", expires_in:" + expiresIn + "}";
- return result;
- }
- }
|