当前位置:网站首页>对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
边栏推荐
- 关于基于kangle和EP面板使用CDN
- 一种适用于应用频繁测试下快速查看Pod的日志的方法(grep awk xargs kuberctl)
- 23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
- 调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error
- AVL平衡二叉搜索树
- Automatic upgrading of database structure in rainbow
- [Chongqing Guangdong education] audio visual language reference materials of Xinyang Normal University
- MES系統,是企業生產的必要選擇
- 数据库存储---表分区
- Compilation and linking of programs
猜你喜欢

Teach you how to select PCB board by hand (II)

The field value in Splunk subquery fuzzy matching CSV is*

Deit learning notes

The single value view in Splunk uses to replace numeric values with text

All about PDF crack, a complete solution to meet all your PDF needs

Opencv learning note 4 - expansion / corrosion / open operation / close operation

A method for quickly viewing pod logs under frequent tests (grep awk xargs kuberctl)

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

21 general principles of wiring in circuit board design_ Provided by Chengdu circuit board design

数据分片介绍
随机推荐
Deit learning notes
[hard core science popularization] working principle of dynamic loop monitoring system
Go write a program that runs within a certain period of time
Explore creativity in steam art design
A single game with goods increased by 100000, and the rural anchor sold men's clothes on top of the list?
2-3查找树
单元测试报告成功率低
打通法律服务群众“最后一公里”,方正璞华劳动人事法律自助咨询服务平台频获“点赞”
A method for quickly viewing pod logs under frequent tests (grep awk xargs kuberctl)
Componentspace2022, assertions, protocols, bindings, and configuration files
Golan idea IntelliJ cannot input Chinese characters
National SMS center number inquiry
Mock.js用法详解
Ebpf cilium practice (2) - underlying network observability
iptables 之 state模块(ftp服务练习)
Laravel8 uses passport login and JWT (generate token)
[IELTS speaking] Anna's oral learning records part2
Practice of implementing cloud native Devops based on rainbow library app
路由信息协议——RIP
Grpc, oauth2, OpenSSL, two-way authentication, one-way authentication and other column directories