当前位置:网站首页>对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
边栏推荐
- PVTV2--Pyramid Vision TransformerV2学习笔记
- Exercise arrangement 2.10, 11
- Golan idea IntelliJ cannot input Chinese characters
- Laravel8 uses passport login and JWT (generate token)
- Virtual address space
- 23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
- [IELTS speaking] Anna's oral learning records Part3
- Golang compilation constraint / conditional compilation (/ / +build < tags>)
- The truth of robot education in hands-on practice
- Analyzing the influence of robot science and technology development concept on Social Research
猜你喜欢

Deit learning notes

A single game with goods increased by 100000, and the rural anchor sold men's clothes on top of the list?

Implement your own dataset using bisenet

Open3d ISS key points

Xcit learning notes

23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP

2 - 3 arbre de recherche

Practice of implementing cloud native Devops based on rainbow library app

Arm GIC (IV) GIC V3 register class analysis notes.

One click deployment of highly available emqx clusters in rainbow
随机推荐
Opencv learning note 3 - image smoothing / denoising
[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error
iptables 之 state模块(ftp服务练习)
A method for quickly viewing pod logs under frequent tests (grep awk xargs kuberctl)
[Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University
Le système mes est un choix nécessaire pour la production de l'entreprise
Snyk 依赖性安全漏洞扫描工具
Input and output of floating point data (C language)
Deit learning notes
Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
mysql分区讲解及操作语句
One click installation of highly available Nacos clusters in rainbow
Merge sort and non comparison sort
All about PDF crack, a complete solution to meet all your PDF needs
Interpreting the practical application of maker thinking and mathematics curriculum
字符串操作
Practice of implementing cloud native Devops based on rainbow library app
IP guard helps energy enterprises improve terminal anti disclosure measures to protect the security of confidential information
SSM 整合