与第三方系统做系统对接,接口认证是必不可少的,安全的认证方式可以极大的增强系统的安全性访问。
Baic 认证
一种及其简单的认证方式,调用方通过对用户名和密码做 Base64 加密然后传递到服务端进行认证。
代码如下:
## 拼接 key
String key = "app_id:app_secrect";
Map<String, String> headers = new HashMap<>();
## Base64 对 key 进行加密
headers.put("Authorization", "Basic " + Base64.encode(key));
request.addHeaders(headers);
Map<String, Object> form = new HashMap<>();
form.put("grant_type", "client_credentials");
form.put("scope", "write");
request.form(form);
request.execute().body();
如果想进一步加强认证,可以通过上一步获取 accessToken,然后每次请求携带。
JSONObject jsonObject = new JSONObject();
jsonObject.put("invoiceTypeNo","04");
HttpRequest httpRequest = HttpUtil.createPost(url);
headers = new HashMap<>();
headers.put("Authorization", "Bearer ".concat(accessToken));
headers.put("Content-Type","application/json");
httpRequest.addHeaders(headers);
httpRequest.body(jsonObject.toJSONString());
String info = httpRequest.execute().body();
安全性相对 Baic认证 有所提升,每次接口调用时都使用临时颁发的 access_token 来代替用户名和密码 减少凭证泄漏的机率。
在每次接口调用时都需要传输以下参数:
String appKey = "qwe@2023";
SortedMap<String, String> packageParams = new TreeMap<>();
packageParams.put("time", System.currentTimeMillis()+"");
packageParams.put("nonce ", RandomStringUtils.randomNumeric(8));
String sign = createSign(packageParams, appKey);
packageParams.put("sign", sign);
//发送请求附带相关参数
获取签名:
public static String createSign(SortedMap<String, String> packageParams, String appKey) {
StringBuffer sb = new StringBuffer();
Set es = packageParams.entrySet();
Iterator it = es.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String k = (String) entry.getKey();
String v = (String) entry.getValue();
if (StringUtils.isNotBlank(v)) {
sb.append(k + "=" + v + "&");
}
}
sb.append("key=" + appKey);
return HMACUtil.HMACSHA256(sb.toString(),appKey);
}
HMACSHA256 加密:
public static String HMACSHA256(String data, String key){
try {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
sha256_HMAC.init(secret_key);
byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
for (byte item : array) {
sb.append(Integer.toHexString((item & 0xFF) | 0x100), 1, 3);
}
return sb.toString().toUpperCase();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}