// Path: hunt-common/src/main/java/com/hunt/util/Result.java
// public class Result {
//     private int code;
//     private String msg;
//     private Object data = "";
// 
// 
//     public Result(int code, String msg, Object data) {
//         this.code = code;
//         this.msg = msg;
//         this.data = data;
//     }
// 
//     public Result(int code, String msg) {
//         this.code = code;
//         this.msg = msg;
//     }
// 
//     public Result() {
//     }
// 
//     public static Result instance(int code, String msg) {
//         return new Result(code, msg);
//     }
// 
//     public static Result success() {
//         return new Result(ResponseCode.success.getCode(), ResponseCode.success.getMsg());
//     }
// 
//     public static Result success(Object data) {
//         return new Result(ResponseCode.success.getCode(), ResponseCode.success.getMsg(), data);
//     }
// 
//     public static Result error() {
//         return new Result(ResponseCode.error.getCode(), ResponseCode.error.getMsg());
//     }
// 
//     public static Result error(String msg) {
//         return new Result(ResponseCode.error.getCode(), msg);
//     }
// 
//     @Override
//     public String toString() {
//         return "Result{" +
//                 "code=" + code +
//                 ", msg='" + msg + '\'' +
//                 ", data=" + data +
//                 '}';
//     }
// 
//     public int getCode() {
//         return code;
//     }
// 
//     public void setCode(int code) {
//         this.code = code;
//     }
// 
//     public String getMsg() {
//         return msg;
//     }
// 
//     public void setMsg(String msg) {
//         this.msg = msg;
//     }
// 
//     public Object getData() {
//         return data;
//     }
// 
//     public void setData(Object data) {
//         this.data = data;
//     }
// }
// 
// Path: hunt-common/src/main/java/com/hunt/util/SystemConstant.java
// public class SystemConstant {
//     public static final String shiro_cache_prefix = "shiro-cache-";
//     public static final String shiro_cache_prefix_keys = "shiro-cache-*";
//     public static final String github_client_id = "f4b35940357e82596645";
//     public static final String github_client_secret = "6d11170f89e0b831d20a6ccc6d10c7e99a4963b3";
//     public static final String github_oauth_url = "http://127.0.0.1:8080/oauth/github";
// 
// }


import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hunt.util.Result;
import com.hunt.util.SystemConstant;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
public class OauthController extends BaseController {
    private static Logger log = LoggerFactory.getLogger(OauthController.class);

    //https://github.com/login/oauth/authorize?client_id=f4b35940357e82596645&state=111111&redirect_uri=http://127.0.0.1:8080/oauth/github
    @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://github.com/login/oauth/access_token?client_id=" + SystemConstant.github_client_id + "&client_secret=" + SystemConstant.github_client_secret + "&code=" + code + "&redirect_uri=" + SystemConstant.github_oauth_url;
            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://api.github.com/user?access_token=" + access_token;
            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"));
        }
// Path: hunt-common/src/main/java/com/hunt/util/Result.java
// public class Result {
//     private int code;
//     private String msg;
//     private Object data = "";
// 
// 
//     public Result(int code, String msg, Object data) {
//         this.code = code;
//         this.msg = msg;
//         this.data = data;
//     }
// 
//     public Result(int code, String msg) {
//         this.code = code;
//         this.msg = msg;
//     }
// 
//     public Result() {
//     }
// 
//     public static Result instance(int code, String msg) {
//         return new Result(code, msg);
//     }
// 
//     public static Result success() {
//         return new Result(ResponseCode.success.getCode(), ResponseCode.success.getMsg());
//     }
// 
//     public static Result success(Object data) {
//         return new Result(ResponseCode.success.getCode(), ResponseCode.success.getMsg(), data);
//     }
// 
//     public static Result error() {
//         return new Result(ResponseCode.error.getCode(), ResponseCode.error.getMsg());
//     }
// 
//     public static Result error(String msg) {
//         return new Result(ResponseCode.error.getCode(), msg);
//     }
// 
//     @Override
//     public String toString() {
//         return "Result{" +
//                 "code=" + code +
//                 ", msg='" + msg + '\'' +
//                 ", data=" + data +
//                 '}';
//     }
// 
//     public int getCode() {
//         return code;
//     }
// 
//     public void setCode(int code) {
//         this.code = code;
//     }
// 
//     public String getMsg() {
//         return msg;
//     }
// 
//     public void setMsg(String msg) {
//         this.msg = msg;
//     }
// 
//     public Object getData() {
//         return data;
//     }
// 
//     public void setData(Object data) {
//         this.data = data;
//     }
// }
// 
// Path: hunt-common/src/main/java/com/hunt/util/SystemConstant.java
// public class SystemConstant {
//     public static final String shiro_cache_prefix = "shiro-cache-";
//     public static final String shiro_cache_prefix_keys = "shiro-cache-*";
//     public static final String github_client_id = "f4b35940357e82596645";
//     public static final String github_client_secret = "6d11170f89e0b831d20a6ccc6d10c7e99a4963b3";
//     public static final String github_oauth_url = "http://127.0.0.1:8080/oauth/github";
// 
// }


// Path: hunt-web/src/main/java/com/hunt/controller/OauthController.java
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hunt.util.Result;
import com.hunt.util.SystemConstant;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class OauthController extends BaseController {
    private static Logger log = LoggerFactory.getLogger(OauthController.class);

    //https://github.com/login/oauth/authorize?client_id=f4b35940357e82596645&state=111111&redirect_uri=http://127.0.0.1:8080/oauth/github
    @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://github.com/login/oauth/access_token?client_id=" + SystemConstant.github_client_id + "&client_secret=" + SystemConstant.github_client_secret + "&code=" + code + "&redirect_uri=" + SystemConstant.github_oauth_url;
            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://api.github.com/user?access_token=" + access_token;
            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();