關注WC:CodingTechWork,一起學習進步。
@[toc]
引言
??在開發(fā)Java API
時容诬,有可能需要一些安全認證來保證API
的安全性。下面沿腰,我們通過timestamp
以及sign簽名認證
來實現(xiàn)基本的安全的開發(fā)接口API
览徒。
API接口
curl {ip}:{port}/demo/test/app -X POST -H "Content-Type:application/json" -d '
{
"appId":"app01",
"key01": "value01",
"key02": "value02,
"key03": "value03",
"sign": "77f4d9119d43c41491ab8a7c0530a618d6e0823560eb6dd028f62ae56b1ab7bb",
"timestamp":165820977
}'
timestamp保證唯一性
通過timestamp校驗,保證約定時間內請求的唯一性
shell生成timestamp
- shell中生成秒級timestamp
echo $(date '+%s')
- shell中生成毫秒級timestamp
# 通過納秒除以1000000的方式實現(xiàn)
echo $[$(date '+%s%N')/1000000]
java生成timestamp模板
// 得到秒級
long currentTimeStamp = System.currentTimeMillis() / 1000;
sign簽名認證
實現(xiàn)方式
- app服務端配置對應appSecret, 例如app123456
- 所有請求參數按照字母排序, 配裝字符串, 采用
key=value
并用&
鏈接 - 添加簽名
appSecret
, 字符末尾加上appSecret=app123456
- 采用
SHA-256
生產簽名 - 例:
appId=app01¶ms=key01=value01;key02=value02;key03=value03&appSecret=app123456
shell生成sha-256值
echo -n "appId=app01¶ms=key01=value01;key02=value02;key03=value03&appSecret=app123456" | sha256sum | awk '{print $1}'
結果
77f4d9119d43c41491ab8a7c0530a618d6e0823560eb6dd028f62ae56b1ab7bb
java生成sha-256模板
package com.demo.test
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import org.apache.commons.lang.StringUtils;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class Encrypt {
/**
* 返回 SHA-256 串
*
* @param strText
* @return
*/
public static String SHA256(final String strText) {
return SHA(strText, "SHA-256");
}
/**
* 返回 SHA-512 串
*
* @param strText
* @return
*/
public static String SHA512(final String strText) {
return SHA(strText, "SHA-512");
}
public static String MD5(final String strText) {
return SHA(strText, "MD5");
}
/**
* SHA 加密
*
* @param strText
* @return
*/
private static String SHA(final String strText, final String type) {
String ret = null;
if (strText != null && strText.length() > 0) {
try {
MessageDigest messageDigest = MessageDigest.getInstance(type);
messageDigest.update(strText.getBytes());
byte[] buffer = messageDigest.digest();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buffer.length; i++) {
String hex = Integer.toHexString(0xff & buffer[i]);
if (hex.length() == 1) {
sb.append('0');
}
sb.append(hex);
}
ret = sb.toString();
} catch (NoSuchAlgorithmException e) {
log.error("{} encrypt failed.{}", type, e);
}
}
return ret;
}
/**
* 手機號脫敏處理
*
* @param phone
* @return
*/
public static String desensitizedPhone(String phone) {
if (StringUtils.isNotEmpty(phone)) {
phone = phone.replaceAll("(\\w{3})\\w*(\\w{4})", "$1****$2");
}
return phone;
}
/**
* base64加密
*
* @param content
* @return
*/
public static String base64Encoder(String content) {
try {
String base64encodedString = Base64.getEncoder().encodeToString(content.getBytes("utf-8"));
return base64encodedString;
} catch (UnsupportedEncodingException e) {
log.error("base64Encoder failed", e);
}
return null;
}
/**
* base64解密
*
* @param content
* @return
*/
public static String base64Decoder(String content) {
try {
// 解碼
byte[] base64decodedBytes = Base64.getDecoder().decode(content);
return new String(base64decodedBytes, "utf-8");
} catch (UnsupportedEncodingException e) {
log.error("base64Decoder failed", e);
}
return null;
}
}