xuhy
2025-01-09 712f70b2936079a131ecb1e63c6d337171618cad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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;
    }
}