OAuth2.0 授权模式,基于HttpClient 实现
程序员文章站
2022-04-28 18:32:16
...
功能代码如下:
package com.digipower.ucas.config;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
* oauth2 客户端工具类
*
* @author zzg
*
*/
public class OAuthClientUtil {
static String accessTokenURL = "http://cas.example.org:8099/cas/oauth2.0/accessToken";
static String client_id = "key";
static String client_secret = "secret";
static String username = "admin";
static String password = "123456";
// 基于password 模式
public static void main1(String[] args) {
String grant_type = "password";
CloseableHttpClient httpClient = HttpClients.createDefault();
String url = "http://cas.example.org:8099/cas/oauth2.0/accessToken?client_id=" + client_id + "&grant_type="
+ grant_type + "&username=" + username + "&password=" + password;
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数据
System.out.println("result:" + result);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 基于client_credentials
public static void main2(String[] args) {
String grant_type = "client_credentials";
CloseableHttpClient httpClient = HttpClients.createDefault();
String url = "http://cas.example.org:8099/cas/oauth2.0/accessToken?client_id=" + client_id + "&grant_type="
+ grant_type + "&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数据
System.out.println("result:" + result);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 基于refresh_token
public static void main3(String[] args) {
String grant_type = "refresh_token";
String refresh_token = "RT-1-W7-9xisPcnQqLfyH2RIpAnnc7aWVPOtP";
CloseableHttpClient httpClient = HttpClients.createDefault();
String url = "http://cas.example.org:8099/cas/oauth2.0/accessToken?client_id=" + client_id + "&grant_type="
+ grant_type + "&client_secret=" + client_secret + "&refresh_token=" + refresh_token;
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数据
System.out.println("result:" + result);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 基于/oauth2.0/profile, 获取用户信息
public static void main(String[] args) {
String access_token = "AT-2-ArvJnVOylh-qizLsOBv0UrqCzEEjyJuP";
CloseableHttpClient httpClient = HttpClients.createDefault();
String url = "http://cas.example.org:8099/cas/oauth2.0/profile?access_token=" + access_token;
HttpGet httpGet = new HttpGet(url);
HttpResponse response = null;
try {
response = httpClient.execute(httpGet);
} 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数据
System.out.println("result:" + result);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
上一篇: RestTemplate使用详解
下一篇: 程序员成长之路