当前位置:网站首页>对API接口或H5接口做签名认证
对API接口或H5接口做签名认证
2022-07-07 05:59:00 【有何不可0307】
参数签名认证
为了防止客户端访问的 API 接口被伪装攻击、数据泄漏等安全风险。通过与服务器端联调实现接口安全的方式,利用 API 接口签名能有效的防范这些安全问题和风险。对接口参数进行签名有如下优点:
保证请求有效性
参数变化会导致签名变化,否则将是一个无效的请求;保证请求合法性
签名算法依赖服务器分发的一对clientKey和secretKey,clientKey能区分出客户端即调用者身份,服务器端能根据secretKey判断出签名是否合法。
Android客户端签名认证的规则之一:
对各参数进行排序,拼接各参数和参数值,然后拼接密钥secretKey,最后进行MD5加密。生成一个参数sign。
实现代码
- 定义参数键值对象
public class NetParameter {
private String key;
private String value;
public NetParameter(String key, String value) {
super();
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
public void setKey(String key) {
this.key = key;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "NetParameter [key=" + key + ", value=" + value + "]";
}
}
- 签名方法
private String sign(List<NetParameter> list, String secretKey) {
StringBuffer sb = new StringBuffer();
if (list != null) {
Collections.sort(list, new Comparator<NetParameter>() {
@Override
public int compare(NetParameter lhs, NetParameter rhs) {
return lhs.getKey().compareTo(rhs.getKey());
}
});
for (int i = 0, size = list.size(); i < size; i++) {
NetParameter p = list.get(i);
if (p != null) {
sb.append(p.getKey());
sb.append("=");
sb.append(p.getValue());
}
}
}
sb.append(secretKey);
return digest(sb.toString(), "MD5");
}
- 生成公开的Url
private String createUrl() {
StringBuffer sb = new StringBuffer();
sb.append(link);
sb.append("?");
if (list != null) {
int j = -1;
for (int i = 0, size = list.size(); i < size; i++) {
NetParameter net = list.get(i);
if (net != null) {
String key = net.getKey();
String value = net.getValue();
if (key != null && value != null) {
if (j < 0) {
j = i;
}
if (j != i) {
sb.append("&");
}
sb.append(key);
sb.append("=");
sb.append(value);
}
}
}
}
sb.append("&");
sb.append("sign");
sb.append("=");
sb.append(sign(list, getSecretKey()));
String tmp = sb.toString();
tmp = tmp.replace("?&", "?");
return tmp;
}
访问接口
当客户端生成的sign与服务器端验签的sign不一致时,访问不通过,将反馈相关的message:“无效的参数签名”。
http://192.168.1.76:8806/#/card?clientKey=05380b3c3323541917a25b001d394bf8&rand=751d7b3c-a37b-4d8f-b169-96ca2b30f286&machineNo=7700943733234077224&productName=P770&sign=FC6DE7DECCBDDB5D909D7AC0CA589BC3
边栏推荐
- 测试踩坑 - 当已有接口(或数据库表中)新增字段时,都需要注意哪些测试点?
- 登山小分队(dfs)
- Give full play to the wide practicality of maker education space
- Train your dataset with swinunet
- Go write a program that runs within a certain period of time
- Explore creativity in steam art design
- Required String parameter ‘XXX‘ is not present
- Merge sort and non comparison sort
- Analysis of maker education in innovative education system
- Splunk子查询模糊匹配csv中字段值为*
猜你喜欢
Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications
单场带货涨粉10万,农村主播竟将男装卖爆单?
Golang compilation constraint / conditional compilation (/ / +build < tags>)
Coquette data completes the cloud native transformation through rainbow to realize offline continuous delivery to customers
2 - 3 arbre de recherche
[Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University
关于基于kangle和EP面板使用CDN
为什么要选择云原生数据库
AVL平衡二叉搜索树
下载和安装orcale database11.2.0.4
随机推荐
Interpreting the practical application of maker thinking and mathematics curriculum
Iptables' state module (FTP service exercise)
PVTV2--Pyramid Vision TransformerV2学习笔记
Implementation of navigation bar at the bottom of applet
調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error
AVL平衡二叉搜索树
National SMS center number inquiry
Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
Go语言中,函数是一种类型
National standard gb28181 protocol video platform easygbs adds streaming timeout configuration
23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
Basic data types and string types are converted to each other
[hard core science popularization] working principle of dynamic loop monitoring system
[Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University
[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
DeiT学习笔记
Snyk dependency security vulnerability scanning tool
IP地址的类别
快速集成认证服务-HarmonyOS平台
The field value in Splunk subquery fuzzy matching CSV is*