当前位置:网站首页>对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
边栏推荐
- Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error
- Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
- GFS分布式文件系统
- Componentspace2022, assertions, protocols, bindings, and configuration files
- AVL平衡二叉搜索树
- Exercise arrangement 2.10, 11
- 如何在HarmonyOS应用中集成App Linking服务
- SSM integration
- In go language, function is a type
- 調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error
猜你喜欢
iptables 之 state模块(ftp服务练习)
Rsync remote synchronization
Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
Laravel8 uses passport login and JWT (generate token)
Appeler l'interface du moteur de création du service multimédia de jeu Huawei renvoie le Code d'erreur 1002, le message d'erreur: les paramètres sont l'erreur
Opencv learning note 5 - gradient calculation / edge detection
Download and install orcale database11.2.0.4
单元测试报告成功率低
如何在快应用中实现滑动操作组件
联想混合云Lenovo xCloud:4大产品线+IT服务门户
随机推荐
Qt Charts使用(重写QChartView,实现一些自定义功能)
How to integrate app linking services in harmonyos applications
Teach you how to select PCB board by hand (II)
快速集成认证服务-HarmonyOS平台
Compilation and linking of programs
Train your dataset with swinunet
Interpreting the practical application of maker thinking and mathematics curriculum
A method for quickly viewing pod logs under frequent tests (grep awk xargs kuberctl)
Analysis of maker education in innovative education system
JEditableTable的使用技巧
2-3查找樹
Give full play to the wide practicality of maker education space
Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error
Analyzing the influence of robot science and technology development concept on Social Research
Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
National SMS center number inquiry
Input and output of floating point data (C language)
Obsidan之数学公式的输入
The truth of robot education in hands-on practice
Using helm to install rainbow in various kubernetes