接口签名方式(代码)

Digest验证方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
HttpResult result = sendRequest(uri, HttpMessage.METHOD_GET, null);
int statusCode = result.getHttpResp().getStatusLine().getStatusCode();
HttpResult resulthttpConnection.sendRequest(message)
if (statusCode == HttpStatus.SC_OK) {
String resDoc = result.getHttpContent();
else if (statusCode == HttpStatus.SC_UNAUTHORIZED) {
Header header = result.getHttpResp().getFirstHeader("WWW-Authenticate");
HashMap<String, String> authMap = parse(header.getValue());
String content = buildAuthHeader(uri, authMap, account, password);
HttpHeader authHeader = new HttpHeader("Authorization", content);
List<HttpHeader> list = new ArrayList<HttpHeader>();
list.add(authHeader);
result = sendRequest(uri, HttpMessage.METHOD_GET, list);
int statusCodeSec = result.getHttpResp().getStatusLine().getStatusCode();
if (statusCodeSec == HttpStatus.SC_OK) {
}
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
public static HttpResult sendRequest(String uri, String method, List<HttpHeader> headers) {
HttpConnection httpConnection = HttpConnectionFactory.getDefaultConnection();
HttpMessage message = new HttpMessage();
message.setRequestUrl(uri);
message.setMethod(method);
message.addHeader("Content-Type", "application/json");
if (headers != null) {
for (HttpHeader header : headers) {
message.addHeader(header.getKey(), header.getValue());
}
}
return httpConnection.sendRequest(message);
}
public static HashMap<String, String> parse(String authHeader) {
HashMap<String, String> headerMap = null;
String header = authHeader.substring(authHeader.lastIndexOf("Digest") + "Digest ".length());
if (!KitUtil.isEmpty(header)) {
headerMap = new HashMap<String, String>();
String[] params = header.split(",");
if (params != null && params.length > 0) {
for (String param : params) {
String[] data = param.split("=");
String key = data[0].trim();
String value = "";
if (data.length > 1) {
value = data[1].trim();
}
headerMap.put(key, value);
}
}
}
return headerMap;
}
static String buildAuthHeader(String uri, Map<String, String> authMap, String user,
String password) {
String realm = authMap.get("realm");
realm = realm.substring(1, realm.length() - 1);
String nonce = authMap.get("nonce");
nonce = nonce.substring(1, nonce.length() - 1);
String qop = authMap.get("qop");
qop = qop.substring(1, qop.length() - 1);
String opaque = authMap.get("opaque");
String ha1 = DigestUtils.md5Hex(user + ":" + realm + ":" + password);
String ha2 = DigestUtils.md5Hex("GET:" + uri);
String cnonce = UUID.randomUUID().toString();
String hashResponse = DigestUtils
.md5Hex(ha1 + ":" + nonce + ":00000001:" + cnonce + ":" + qop + ":" + ha2);
StringBuilder challengeText = new StringBuilder("Digest username=\"");
challengeText.append(user).append("\",");
challengeText.append("realm=\"").append(realm).append("\",");
challengeText.append("nonce=\"").append(nonce).append("\",");
challengeText.append("uri=\"").append(uri).append("\",");
challengeText.append("qop=\"").append(qop).append("\",");
challengeText.append("nc=\"").append("00000001").append("\",");
challengeText.append("cnonce=\"").append(cnonce).append("\",");
challengeText.append("response=\"").append(hashResponse).append("\",");
challengeText.append("opaque=").append(opaque);
return challengeText.toString();
}

参数按字典升序后SHA1加密方式

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
/**
*SHA1加密
*/
public static String SHA1(Map<String,Object> map,String secret){
String aux = getOrderByLexicographic(map,secret);
char hexDigits [] = {'0' , '1' , '2' , '3' , '4', '5' , '6' , '7' , '8' , '9' ,
'a', 'b', 'c', 'd', 'e', 'f'};
MessageDigest mdTemp = null;
try{
mdTemp = MessageDigest.getInstance("SHA1");
mdTemp.update(aux.getBytes("UTF-8"));
}catch (NoSuchAlgorithmException e1){
e1.printStackTrace();
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}
byte[] md = mdTemp.digest();
int j = md.length;
char buf[] = new char[j * 2];
int k = 0;
for (int i =0; i<j;i++){
byte _byte = md[i];
buf[k++] = hexDigits[_byte >>> 4 & 0xf];
buf[k++] = hexDigits[_byts & 0xf];
}
return new String(buf);
}
/**
*根据键名升序排列
*/
private static String getOrderByLexicographic(Map<String,Object> map,String secret){
StringBuffer aux = new StringBuffer();
for(String parName : Collections.sort(map.keySet())){
for(Map.entry<String,Object> entry : map.entrySet()){
if(parName.equals(entry.getKey)){
aux.append(entry.getKey + "=" + String.valueOf(entry.getValue()) + "&");
}
}
}
return aux.append(secret).toString().subString(0);
}