使用HttpClient获取oAuth2.0中的code、token及refreshToken

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lhc_makefunny/article/details/81171194

授权服务器使用授权码模式(authorization_code)

添加依赖

        <!--对用户名和密码进行base64加密-->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
        <!--解析json数据-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>

第一步(获取code)

    public String getCode(String client_id, String response_type, String redirect_uri) {
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // 将用户名和密码放入header中
        String plainClientCredentials = "user:user";
        String base64ClientCredentials = new String(Base64.encodeBase64(plainClientCredentials.getBytes()));
        String url = "http://127.0.0.1:8081/authoriza/oauth/authorize?client_id=" + client_id + "&"
                + "response_type=" + response_type + "&" + "redirect_uri=" + redirect_uri;

        RequestConfig config = RequestConfig.custom().setRedirectsEnabled(false).setConnectionRequestTimeout(5000)
                .build();
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("Authorization", "Basic " + base64ClientCredentials);
        httpGet.setConfig(config);
        String result = "";
        try {

            HttpResponse response = httpClient.execute(httpGet);
            // 从从定向地址中取得code
            if (response.getStatusLine().getStatusCode() == 302) {
                Header header = response.getFirstHeader("Location");
                String location = header.getValue();
                String code = location.substring(location.indexOf("=") + 1, location.length());
                return code;
            }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "error";
    }

第二步(获取token及refreshToken)

public AccessToken getToken(String client_id, String grant_type, String code, String redirect_uri,
            String client_secret) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String url = "http://127.0.0.1:8081/authoriza/oauth/token?client_id=" + client_id + "&grant_type="
                + grant_type + "&code=" + code + "&redirect_uri=" + redirect_uri + "&client_secret=" + client_secret;
        HttpPost httpPost = new HttpPost(url);
        HttpResponse response = null;
        try {
            response = httpClient.execute(httpPost);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String result = "";
        if (response.getStatusLine().getStatusCode() == 200) {
            try {
                result = EntityUtils.toString(response.getEntity(), "UTF-8");
                // 解析token的json数据
                JSONObject jsonObject = JSONObject.parseObject(result);
                AccessToken accessToken = new AccessToken();
                accessToken.setAccess_token(jsonObject.getString("access_token"));
                accessToken.setToken_type(jsonObject.getString("token_type"));
                accessToken.setRefresh_token(jsonObject.getString("refresh_token"));
                accessToken.setExpires_in(jsonObject.getString("expires_in"));
                accessToken.setScope(jsonObject.getString("scope"));
                return accessToken;
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return new AccessToken();
    }

源码下载地址:https://download.csdn.net/download/lhc_makefunny/10559161

真的想吐槽一下国内的网络环境,其实真的没有必要把一片博客转来转去的,翻来覆去就一片文章真的解决不了问题;还有有些问题的确是国内搜不到的,还是要懂得科学上网

猜你喜欢

转载自blog.csdn.net/lhc_makefunny/article/details/81171194