@Test public void github() throws Exception { MvcResult result = mockMvc.perform(MockMvcRequestBuilders .get("https: .param("client_id", SystemConstant.github_client_id) .param("state", "test github oauth") .param("redirect_uri", SystemConstant.github_oauth_url) ) .andDo(print()) .andExpect(status().isOk()) .andReturn(); }
@RequestMapping(value = "github", method = {RequestMethod.GET, RequestMethod.POST}) @ResponseBody public Result github(HttpServletRequest request) throws IOException { log.debug(request.getParameter("code")); String code = request.getParameter("code"); if (StringUtils.hasText(code)) { String access_token_url = "https: CloseableHttpClient client = HttpClients.createDefault(); RequestConfig config = RequestConfig.custom() .setSocketTimeout(5000) .setConnectTimeout(5000) .build(); HttpGet getAccessToken = new HttpGet(access_token_url); getAccessToken.setConfig(config); String access_token_info = EntityUtils.toString(client.execute(getAccessToken).getEntity()); log.debug("access_token_response:{}", access_token_info); String access_token = access_token_info.subSequence(access_token_info.indexOf("=") + 1, access_token_info.indexOf("&")).toString(); log.debug("acccess_token:{}", access_token); String user_info_url = "https: HttpGet getUserInfo = new HttpGet(user_info_url); getUserInfo.setConfig(config); String user_info = EntityUtils.toString(client.execute(getUserInfo).getEntity()); log.debug("user_info:{}", user_info); client.close(); SecurityUtils.getSubject().login(new UsernamePasswordToken("admin", "111111")); } return Result.success(); }
OauthController extends BaseController { @RequestMapping(value = "github", method = {RequestMethod.GET, RequestMethod.POST}) @ResponseBody public Result github(HttpServletRequest request) throws IOException { log.debug(request.getParameter("code")); String code = request.getParameter("code"); if (StringUtils.hasText(code)) { String access_token_url = "https: CloseableHttpClient client = HttpClients.createDefault(); RequestConfig config = RequestConfig.custom() .setSocketTimeout(5000) .setConnectTimeout(5000) .build(); HttpGet getAccessToken = new HttpGet(access_token_url); getAccessToken.setConfig(config); String access_token_info = EntityUtils.toString(client.execute(getAccessToken).getEntity()); log.debug("access_token_response:{}", access_token_info); String access_token = access_token_info.subSequence(access_token_info.indexOf("=") + 1, access_token_info.indexOf("&")).toString(); log.debug("acccess_token:{}", access_token); String user_info_url = "https: HttpGet getUserInfo = new HttpGet(user_info_url); getUserInfo.setConfig(config); String user_info = EntityUtils.toString(client.execute(getUserInfo).getEntity()); log.debug("user_info:{}", user_info); client.close(); SecurityUtils.getSubject().login(new UsernamePasswordToken("admin", "111111")); } return Result.success(); } }
OauthController extends BaseController { @RequestMapping(value = "github", method = {RequestMethod.GET, RequestMethod.POST}) @ResponseBody public Result github(HttpServletRequest request) throws IOException { log.debug(request.getParameter("code")); String code = request.getParameter("code"); if (StringUtils.hasText(code)) { String access_token_url = "https: CloseableHttpClient client = HttpClients.createDefault(); RequestConfig config = RequestConfig.custom() .setSocketTimeout(5000) .setConnectTimeout(5000) .build(); HttpGet getAccessToken = new HttpGet(access_token_url); getAccessToken.setConfig(config); String access_token_info = EntityUtils.toString(client.execute(getAccessToken).getEntity()); log.debug("access_token_response:{}", access_token_info); String access_token = access_token_info.subSequence(access_token_info.indexOf("=") + 1, access_token_info.indexOf("&")).toString(); log.debug("acccess_token:{}", access_token); String user_info_url = "https: HttpGet getUserInfo = new HttpGet(user_info_url); getUserInfo.setConfig(config); String user_info = EntityUtils.toString(client.execute(getUserInfo).getEntity()); log.debug("user_info:{}", user_info); client.close(); SecurityUtils.getSubject().login(new UsernamePasswordToken("admin", "111111")); } return Result.success(); }  }
OauthController extends BaseController { @RequestMapping(value = "github", method = {RequestMethod.GET, RequestMethod.POST}) @ResponseBody public Result github(HttpServletRequest request) throws IOException { log.debug(request.getParameter("code")); String code = request.getParameter("code"); if (StringUtils.hasText(code)) { String access_token_url = "https: CloseableHttpClient client = HttpClients.createDefault(); RequestConfig config = RequestConfig.custom() .setSocketTimeout(5000) .setConnectTimeout(5000) .build(); HttpGet getAccessToken = new HttpGet(access_token_url); getAccessToken.setConfig(config); String access_token_info = EntityUtils.toString(client.execute(getAccessToken).getEntity()); log.debug("access_token_response:{}", access_token_info); String access_token = access_token_info.subSequence(access_token_info.indexOf("=") + 1, access_token_info.indexOf("&")).toString(); log.debug("acccess_token:{}", access_token); String user_info_url = "https: HttpGet getUserInfo = new HttpGet(user_info_url); getUserInfo.setConfig(config); String user_info = EntityUtils.toString(client.execute(getUserInfo).getEntity()); log.debug("user_info:{}", user_info); client.close(); SecurityUtils.getSubject().login(new UsernamePasswordToken("admin", "111111")); } return Result.success(); }  @RequestMapping(value = "github", method = {RequestMethod.GET, RequestMethod.POST}) @ResponseBody Result github(HttpServletRequest request); }
OauthController extends BaseController { @RequestMapping(value = "github", method = {RequestMethod.GET, RequestMethod.POST}) @ResponseBody public Result github(HttpServletRequest request) throws IOException { log.debug(request.getParameter("code")); String code = request.getParameter("code"); if (StringUtils.hasText(code)) { String access_token_url = "https: CloseableHttpClient client = HttpClients.createDefault(); RequestConfig config = RequestConfig.custom() .setSocketTimeout(5000) .setConnectTimeout(5000) .build(); HttpGet getAccessToken = new HttpGet(access_token_url); getAccessToken.setConfig(config); String access_token_info = EntityUtils.toString(client.execute(getAccessToken).getEntity()); log.debug("access_token_response:{}", access_token_info); String access_token = access_token_info.subSequence(access_token_info.indexOf("=") + 1, access_token_info.indexOf("&")).toString(); log.debug("acccess_token:{}", access_token); String user_info_url = "https: HttpGet getUserInfo = new HttpGet(user_info_url); getUserInfo.setConfig(config); String user_info = EntityUtils.toString(client.execute(getUserInfo).getEntity()); log.debug("user_info:{}", user_info); client.close(); SecurityUtils.getSubject().login(new UsernamePasswordToken("admin", "111111")); } return Result.success(); }  @RequestMapping(value = "github", method = {RequestMethod.GET, RequestMethod.POST}) @ResponseBody Result github(HttpServletRequest request);  }