package com.stylefeng.guns.modular.account.unionpay;
|
|
import java.io.Serializable;
|
import java.io.UnsupportedEncodingException;
|
import java.text.SimpleDateFormat;
|
|
import com.alibaba.fastjson.JSONObject;
|
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.HttpPost;
|
import org.apache.http.entity.StringEntity;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.HttpClientBuilder;
|
import org.apache.http.util.EntityUtils;
|
|
/**
|
* 文档:认证流程.pdf
|
* <p>
|
* 作用:用于获取OPEN-ACCESS-TOKEN认证方式的AccessToken
|
*/
|
public class GetAccessToken implements Serializable {
|
/*public static void main(String args[]) {
|
GetAccessToken getToken = new GetAccessToken();
|
String token = getToken.getToken2();
|
System.out.println(token);
|
}*/
|
|
/*得到token*/
|
public static String getToken() {
|
JSONObject jsonObject = new JSONObject();
|
final String appID = "10037e6f68e462980169043abe98002a";
|
String Timestamp = gettime();
|
String Nonce = getnonce();
|
String appKey = "fb5d5fe2594446ce8a914f878b28b0ad";
|
String all = appID + Timestamp + Nonce + appKey;
|
String signature = ccsha256(all);
|
jsonObject.put("appId", appID);
|
jsonObject.put("timestamp", Timestamp);
|
jsonObject.put("nonce", Nonce);
|
jsonObject.put("signature", signature);
|
jsonObject.put("signMethod","SHA256");
|
System.out.println("请求数据:" + jsonObject);
|
JSONObject response = doPost("https://test-api-open.chinaums.com/v1/token/access", jsonObject);
|
//JSONObject response = doPost("http://api-mop.chinaums.com/v1/token/access", jsonObject);
|
System.out.println("响应数据:" + response);
|
String accessToken = response.get("accessToken").toString();
|
return accessToken;
|
}
|
|
/*获取系统当前时间*/
|
public static String gettime() {
|
SimpleDateFormat date = new SimpleDateFormat("yyyyMMddHHmmss");
|
String time = date.format(System.currentTimeMillis());
|
|
return time;
|
}
|
|
/*获取随机数*/
|
public static String getnonce() {
|
int a = (int) (1 + Math.random() * 10000);
|
String nonce = String.valueOf(a);
|
return nonce;
|
}
|
|
/**
|
* @Description: 加密算法sh1
|
* @Param: [message]
|
* @return: java.lang.String
|
* @Author: Mr.YS
|
* @Date: 2020/6/4/004
|
*/
|
public static String ccsha1(String message) {/*加密算法实现*/
|
String c = DigestUtils.sha1Hex(message);
|
return c;
|
}
|
|
/**
|
* @Description: 加密算法sha256
|
* @Param: [message]
|
* @return: java.lang.String
|
* @Author: Mr.YS
|
* @Date: 2020/6/4/004
|
*/
|
public static String ccsha256(String message) {
|
return DigestUtils.sha256Hex(getMessageBytes(message));
|
}
|
|
/**
|
* @Description: 根据编码类型获得签名内容byte[]
|
* @Param: [message]
|
* @return: byte[]
|
* @Author: Mr.YS
|
* @Date: 2020/6/4/004
|
*/
|
public static byte[] getMessageBytes(String message) {
|
try {
|
return message.getBytes("UTF-8");
|
} catch (UnsupportedEncodingException e) {
|
throw new RuntimeException("签名过程中出现错误");
|
}
|
}
|
|
/*发送post请求得到返回的数据*/
|
public static JSONObject doPost(String url, JSONObject json) {
|
|
CloseableHttpClient httpclient = HttpClientBuilder.create().build();
|
HttpPost post = new HttpPost(url);
|
|
JSONObject response = null;
|
try {
|
StringEntity s = new StringEntity(json.toString());
|
s.setContentEncoding("UTF-8");
|
s.setContentType("application/json");//发送json数据需要设置contentType
|
post.setEntity(s);
|
CloseableHttpResponse res = httpclient.execute(post);
|
HttpEntity entity = res.getEntity();
|
String result = EntityUtils.toString(entity);// 返回json格式:
|
response = JSONObject.parseObject(result);
|
res.close();
|
} catch (Exception e) {
|
throw new RuntimeException(e);
|
}
|
return response;
|
}
|
}
|