OAuth2.0 授权模式,基于HttpClient 实现分析

无情 阅读:282 2021-03-31 16:52:15 评论:0

功能代码如下:

package com.zzg.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(); 
			} 
		} 
	} 
} 

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号