Java+HttpClient学习笔记04-基础认证

没有找到关于基础认证的API,就自己写了一下。

HTTP的部分认证需要使用Authorization请求头字段

 

基础认证

HTTP的基础认证需要通过请求头Authorization:一定规则后加密后的值

1、将用户名和密码已 用户名:密码的形式进行Base64加密

2、将Basic+空格+加密后的值形式拼接

3、Authorization:Basic 加密后的值

这种加密方式应该很少用,因为很容易破解。

 

示例的接口是GitHub的查询用户邮箱接口,运行需要使用GitHub的用户名和密码 GitHu邮箱接口

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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 java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Base64;

public class LessonPost {
        //返回Authorization所需要的值
	public static String getAuthorization(String username, String password) throws UnsupportedEncodingException {
		//拼接成用户名:密码形式
		String str = username +":"+ password;
		//经过Base64编码
		String str1 = Base64.getEncoder().encodeToString(str.getBytes("UtF-8"));
		String str2 = "Basic "+ str1;
		return str2;
	}

	public static void main(String[] args) throws IOException {
		//获得经过编码的内容,需要GitHub的用户名和密码
		String str = getAuthorization("username","password");
		//创建内置httpclient对象
		CloseableHttpClient client = HttpClients.createDefault();
		//创建GET请求对象
		HttpGet httpGet = new HttpGet("https://api.github.com/user/emails");
		//添加请求头
		httpGet.addHeader("Authorization",str);
		//发送请求,获得响应内容
		CloseableHttpResponse response = client.execute(httpGet);
		try {
			System.out.println(response.getStatusLine());
			//响应实体
			HttpEntity entity = response.getEntity();
			//实体内容
			System.out.println(EntityUtils.toString(entity));
		}finally {
			response.close();
		}


	}
}

 

 

还有一种OAUTH认证的方式。

需要使用用户的token来进行认证,拿来测试的是GitHub的私人token
如图,登录GIthub,然后用户下面选择Settings/Developer settings/Personal access tokens,点击Generate new token,然后随便输个描述,勾选user这块,然后生成token。这个token可以理解为身份令牌。将生成的token保存下来。

使用方式

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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 java.io.IOException;

public class LessonAuthorization2 {
	public static void main(String[] args) throws IOException {
		CloseableHttpClient client = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet("https://api.github.com/user");
		httpGet.addHeader("Authorization","token 对应的token");
		CloseableHttpResponse response = client.execute(httpGet);
		try {
			System.out.println(response.getStatusLine());
			HttpEntity entity = response.getEntity();
			System.out.println(EntityUtils.toString(entity));
		} finally {
			response.close();
		}

	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值