特别备注RetType retType = new RetType(); 这个是自己写的返回类,可以去掉
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.springframework.util.DigestUtils;
import com.safeneeds.util.base.RetType;
/**
* 摘要认证 两次请求
*
* @param url
* @return 返回结果
*/
public class DoPostDigestUtil {
/**
* 摘要认证 两次请求
*
* @param url
* @return 返回结果
*/
public static RetType doPostDigest(String url, String username, String password, String json) {
RetType retType = new RetType();
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpPost httpPost = null;
String strResponse = null;
try {
httpClient = HttpClients.createDefault();
httpPost = new HttpPost(url);
// 构造请求头
httpPost.setHeader("Content-type", "application/json; charset=utf-8");
httpPost.addHeader("Cache-Control", "no-cache"); //设置缓存
httpPost.setHeader("Connection", "Close");
StringEntity entity = new StringEntity(json, "UTF-8");
httpPost.setEntity(entity);
RequestConfig.Builder builder = RequestConfig.custom();
builder.setSocketTimeout(3000); //设置请求时间
builder.setConnectTimeout(5000); //设置超时时间
builder.setRedirectsEnabled(false);//设置是否跳转链接(反向代理)
// 设置 连接 属性
httpPost.setConfig(builder.build());
// 执行请求
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
// 检验返回码
int statusCode = response.getStatusLine().getStatusCode();
if (401 == statusCode) {
strResponse = EntityUtils.toString(responseEntity, "utf-8");
// 组织参数,发起第二次请求
Header[] headers = response.getHeaders("WWW-Authenticate");
HeaderElement[] elements = headers[0].getElements();
String realm = null;
String qop = null;
String nonce = null;
String opaque = null;
String method = "POST";
String uri = url;
for (HeaderElement element : elements) {
if (element.getName().equals("Digest realm")) {
realm = element.getValue();
} else if (element.getName().equals("qop")) {
qop = element.getValue();
} else if (element.getName().equals("nonce")) {
nonce = element.getValue();
} else if (element.getName().equals("opaque")) {
opaque = element.getValue();
}
}
// 以上为 获取第一次请求后返回的 数据
String nc = "00000001";
String cnonce = "uniview";
// 后期变成可配置
String a1 = username + ":" + realm + ":" + password;
String a2 = method + ":" + uri;
String response1 = null;
// 获取 Digest 这个字符串
String backString = response.getFirstHeader("WWW-Authenticate").getValue();
try {
response1 = DigestUtils.md5DigestAsHex((DigestUtils.md5DigestAsHex(a1.getBytes("UTF-8")) + ":" + nonce + ":" + nc
+ ":" + "uniview" + ":" + qop + ":" + DigestUtils.md5DigestAsHex(a2.getBytes("UTF-8"))).getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
retType.doError("MD5异常:{"+e+"}");
return retType;
}
httpPost.addHeader("Authorization", backString + ",username=\"" + username + "\"" + ",realm=\"" + realm + "\""
+ ",nonce=\"" + nonce + "\"" + ",uri=\"" + uri + "\"" + ",qop=\"" + qop + "\"" + ",nc=\"" + nc + "\""
+ ",cnonce=\"" + cnonce + "\"" + ",response=\"" + response1 + "\"" + ",opaque=\"" + opaque);
// 发送第二次请求
response = httpClient.execute(httpPost);
int statusCode1 = response.getStatusLine().getStatusCode();
if (HttpStatus.SC_OK == statusCode1) {
strResponse = EntityUtils.toString(entity, StandardCharsets.UTF_8);
HttpEntity retEntity = response.getEntity();
retType.doSuccess(EntityUtils.toString(retEntity));
return retType;
} else {
strResponse = EntityUtils.toString(entity, StandardCharsets.UTF_8);
retType.doError("第二次鉴权认证请求非 200 返回结果:{}");
return retType;
}
} else {
strResponse = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
retType.doError("第一次鉴权认证请求非401 返回结果:{}");
return retType;
}
} catch (Exception e) {
retType.doError("摘要认证 发送请求失败:"+e);
return retType;
} finally {
if (null != httpPost) {
httpPost.releaseConnection();
}
if (null != response) {
try {
response.close();
} catch (IOException e) {
retType.doError("httpResponse流关闭异常:"+e);
return retType;
}
}
if (null != httpClient) {
try {
httpClient.close();
} catch (IOException e) {
retType.doError("httpClient 流关闭异常:"+e);
return retType;
}
}
}
}
}