import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
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 javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;
public class AppAuthJavaFormDemo {
private static final String MAC_NAME = "HmacSHA1";
private static final String ENCODING = "UTF-8";
private static final String HTTP_METHOD_GET = "GET";
private static final String HTTP_METHOD_POST = "POST";
private static String getGMTTime(){
Calendar cd = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
String GMTTime = sdf.format(cd.getTime());
return GMTTime;
}
private static String sortQueryParams(String queryParam){
// Parameters should be in alphabetical order
if (queryParam == null || queryParam == ""){
return "";
}
String[] queryParams = queryParam.split("&");
Map<String, String> queryPairs = new TreeMap<>();
for(String query: queryParams){
String[] kv = query.split("=");
queryPairs.put(kv[0], kv[1]);
}
StringBuilder sortedParamsBuilder = new StringBuilder();
Iterator iter = queryPairs.entrySet().iterator();
while(iter.hasNext()){
Map.Entry entry = (Map.Entry) iter.next();
sortedParamsBuilder.append(entry.getKey());
sortedParamsBuilder.append("=");
sortedParamsBuilder.append(entry.getValue());
sortedParamsBuilder.append("&");
}
String sortedParams = sortedParamsBuilder.toString();
sortedParams = sortedParams.substring(0, sortedParams.length() - 1);
return sortedParams;
}
private static byte[] HmacSHA1Encrypt(String encryptText, String encryptKey) throws Exception {
byte[] data = encryptKey.getBytes(ENCODING);
SecretKey secretKey = new SecretKeySpec(data, MAC_NAME);
Mac mac = Mac.getInstance(MAC_NAME);
mac.init(secretKey);
byte[] text = encryptText.getBytes(ENCODING);
return mac.doFinal(text);
}
private static String base64Encode(byte[] key) {
final Base64.Encoder encoder = Base64.getEncoder();
return encoder.encodeToString(key);
}
public static void main(String[] args) throws Exception {
String environment = "";
String url = "http://service-xxxxxxx-xxxxxxxxxx.cq.apigw.tencentcs.com/appParam?name=clare&password=333";
String host = "service-xxxxxxx-xxxxxxxxxx.cq.apigw.tencentcs.com";
String apiAppKey = "APIDoMSRiefxxxxxxxxxxxxGz6AEEaFB";
String apiAppSecret = "I0IDUmr6xxxxxxxxxxxxxx3C5GUsN2Rjvp";
String httpMethod = "POST";
String acceptHeader = "application/json";
String reqBody = "";
String contentType = "application/x-www-form-urlencoded";
String contentMD5 = "";
if (httpMethod.toUpperCase() == HTTP_METHOD_POST) {
// Parse form data and assemble request body
Map<String, String> reqBodyMap = new TreeMap<>();
reqBodyMap.put("type", "fruit");
reqBodyMap.put("fruitname", "apple");
StringBuffer reqBodyBuffer = new StringBuffer();
for (Map.Entry<String, String> e : reqBodyMap.entrySet()) {
reqBodyBuffer.append(e.getKey());
reqBodyBuffer.append("=");
reqBodyBuffer.append(e.getValue());
reqBodyBuffer.append("&");
}
reqBody = reqBodyBuffer.toString();
reqBody = reqBody.substring(0, reqBody.length() - 1);
}
// ContentType should be empty if request body is not present
if (reqBody.length() == 0) {
contentType = "";
reqBody = "";
}
// Parse URL and assemble string to sign
URL parsedUrl = new URL(url);
String pathAndParams = parsedUrl.getPath();
if (environment != ""){
pathAndParams = pathAndParams.substring(pathAndParams.indexOf(environment) + environment.length());
}
String queryParams = "";
if (parsedUrl.getQuery() != null) {
queryParams = parsedUrl.getQuery();
}
if (reqBody != "" && reqBody.length() > 0){
if(queryParams.length() > 0){
queryParams = queryParams + "&" + reqBody;
} else {
queryParams = reqBody;
}
}
if (queryParams != ""){
pathAndParams = pathAndParams + "?" + sortQueryParams(queryParams);
}
String xDate = getGMTTime();
String stringToSign = String.format("x-date: %s\\n%s\\n%s\\n%s\\n%s\\n%s", xDate, httpMethod, acceptHeader, contentType, contentMD5, pathAndParams);
System.out.println("stringToSign:" + stringToSign);
// Encode string with HMAC and base64
byte[] hmacStr = HmacSHA1Encrypt(stringToSign, apiAppSecret);
String signature = base64Encode(hmacStr);
String authHeader = String.format("hmac id=\\"%s\\", algorithm=\\"hmac-sha1\\", headers=\\"x-date\\", signature=\\"%s\\"", apiAppKey, signature);
// Generate request
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
// Send request
if (httpMethod.toUpperCase() == HTTP_METHOD_GET) {
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("Accept", acceptHeader);
httpGet.setHeader("Host", host);
httpGet.setHeader("x-date", xDate);
httpGet.setHeader("Authorization", authHeader);
response = httpClient.execute(httpGet);
}
if (httpMethod.toUpperCase() == HTTP_METHOD_POST) {
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Accept", acceptHeader);
httpPost.setHeader("Host", host);
httpPost.setHeader("x-date", xDate);
httpPost.setHeader("Content-Type", contentType);
httpPost.setHeader("Content-MD5", contentMD5);
httpPost.setHeader("Authorization", authHeader);
StringEntity stringEntity = new StringEntity(reqBody, ENCODING);
httpPost.setEntity(stringEntity);
response = httpClient.execute(httpPost);
}
// Receive response
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
System.out.println("Response status code: " + response.getStatusLine());
System.out.println("Response body: " + EntityUtils.toString(responseEntity));
}
}
}
Was this page helpful?